Skip to main content

Automatically detect and install Python dependencies from your code

Project description

Requirements Installer 🚀

Tired of hunting down missing Python dependencies? Requirements Installer automatically detects and installs all third-party packages your Python script needs—no requirements.txt needed!

Simply point it at your Python file, and it handles the rest. Perfect for running unfamiliar scripts, quick prototyping, or setting up development environments.


✨ Features

  • 🔍 Smart Import Detection - Automatically scans your Python files for all third-party imports
  • 📦 One-Command Install - Detects and installs dependencies in a single step
  • 🎯 Interactive Version Selection - Optionally choose specific versions for each package
  • 🔒 Virtual Environment Support - Create and install into isolated environments
  • 🧠 Intelligent Filtering - Excludes stdlib and local modules automatically
  • 📝 Module-to-Package Mapping - Handles tricky cases like cv2opencv-python, PILPillow
  • 🌐 One-Hop Local Import Scanning - Follows local imports to catch all dependencies
  • ⚡ Fast & Lightweight - Pure Python with no external dependencies

📥 Installation

pip install requirements-installer

Or install from source:

git clone https://github.com/FH-Prevail/requirements_installer.git
cd requirements-installer
pip install -e .

🚀 Quick Start

Basic Usage

# Scan and install dependencies for a Python file
python requirements_installer.py --file mycode.py

With Virtual Environment

# Create a venv and install dependencies there
python requirements_installer.py --file mycode.py --use-venv --venv-path .venv

Interactive Version Selection

# Choose specific versions for each package
python requirements_installer.py --file mycode.py --ask-version

Example interaction:

==================================================
Version Selection
==================================================

Install latest version of 'numpy'? [Y/n]: n
Enter version for 'numpy' (e.g., 1.2.3): 1.24.0

Install latest version of 'pandas'? [Y/n]: y

Install latest version of 'requests'? [Y/n]: 

Print Requirements Only

# Just show what would be installed, don't install
python requirements_installer.py --file mycode.py --print-requirements

📖 Usage Examples

Example 1: Quick Script Setup

You clone a repo without a requirements.txt:

python requirements_installer.py --file app.py

Output:

Detected environment: current Python at /usr/bin/python3
Entry file: /home/user/project/app.py
Installing: numpy pandas requests

==================================================
Installation Summary
==================================================
Installed: numpy, pandas, requests
Already satisfied: (none)
All requested packages are now present.

Example 2: Isolated Development Environment

python requirements_installer.py --file main.py --use-venv

Creates a .venv folder and installs all dependencies there, keeping your system Python clean.

Example 3: Precise Version Control

python requirements_installer.py --file analysis.py --ask-version

Prompts you for each package, letting you pin versions for reproducibility.


🔧 Command-Line Options

usage: requirements_installer.py [-h] --file FILE [--use-venv] 
                                 [--venv-path VENV_PATH]
                                 [--print-requirements] [--ask-version]

options:
  -h, --help            Show this help message and exit
  --file FILE           Entry Python file to scan (e.g., mycode.py)
  --use-venv            Create and use a virtual environment
  --venv-path VENV_PATH 
                        Where to create the venv (default: .venv)
  --print-requirements  Only print inferred packages and exit
  --ask-version         Interactively ask for version preference for each package

🎯 How It Works

  1. Parse Imports: Uses Python's AST to find all import and from ... import statements
  2. Filter Standard Library: Removes built-in Python modules (e.g., os, sys, json)
  3. Filter Local Modules: Excludes your project's own modules
  4. Map Module Names: Converts import names to PyPI package names (e.g., cv2opencv-python)
  5. Check Installation: Queries what's already installed to avoid redundant work
  6. Install Packages: Uses pip to install missing packages
  7. Verify: Confirms all required packages are present

📊 Comparison with Other Tools

Feature requirements-installer pipreqs pigar pythonrunscript
Auto-detect imports
Auto-install packages
Interactive version selection
Virtual environment support
No source file modification
One-command operation

Why requirements-installer?

  • pipreqs/pigar: Generate requirements.txt but don't install (requires 2 steps)
  • pythonrunscript: Installs but requires metadata comments in your code
  • requirements-installer: Does both detection and installation, no modifications needed!

🧪 Module-to-Package Mappings

The tool includes smart mappings for common cases where the import name differs from the PyPI package name:

cv2           opencv-python
PIL           Pillow
sklearn       scikit-learn
yaml          PyYAML
bs4           beautifulsoup4
dotenv        python-dotenv
# ... and many more!

⚠️ Limitations

  • One-hop scanning: Only follows local imports one level deep (for speed)
  • Dynamic imports: Limited detection of importlib.import_module() calls
  • Conditional imports: Treats all imports equally (no context analysis)
  • Version conflicts: Doesn't resolve complex dependency conflicts (delegates to pip)

These are deliberate trade-offs for simplicity and speed. For complex projects with intricate dependency trees, consider using Poetry or Pipenv.


🤝 Contributing

Contributions are welcome! Here are some ways you can help:

  • 🐛 Report bugs and issues
  • 💡 Suggest new features or improvements
  • 📝 Improve documentation
  • 🔧 Submit pull requests
  • ⭐ Add more module-to-package mappings

⭐ Star History

If you find this tool useful, please consider giving it a star on GitHub! It helps others discover the project.


Made with ❤️ by developers, for developers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requirements_installer-1.0.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

requirements_installer-1.0.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file requirements_installer-1.0.0.tar.gz.

File metadata

  • Download URL: requirements_installer-1.0.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for requirements_installer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b0c0660eeb3ee29efe500523306a242dd327e1f1b544e73b3a45b776d6fa0c3a
MD5 ea24e9824e86d306b0c381729b9f387e
BLAKE2b-256 80586967187559d865ff635a84fb45b889800277ed65c17344ad09e3746b1c9e

See more details on using hashes here.

File details

Details for the file requirements_installer-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for requirements_installer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 858d78104bf4cdb9cd9ad44a847f15cf45b8092679e217e8688a9268bba5c61e
MD5 f51a2d8c90608ac2602f815aa40834de
BLAKE2b-256 ef034559d351065ba736c69ada619ebb40fd8af5b636828da84ef849536a6777

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page