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
cv2→opencv-python,PIL→Pillow - 🌐 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
- Parse Imports: Uses Python's AST to find all
importandfrom ... importstatements - Filter Standard Library: Removes built-in Python modules (e.g.,
os,sys,json) - Filter Local Modules: Excludes your project's own modules
- Map Module Names: Converts import names to PyPI package names (e.g.,
cv2→opencv-python) - Check Installation: Queries what's already installed to avoid redundant work
- Install Packages: Uses pip to install missing packages
- 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.txtbut 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file requirements_installer-1.0.1.tar.gz.
File metadata
- Download URL: requirements_installer-1.0.1.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d37fb203aba4b4b188ecae4d9c3621f8ead078ca1ac36f306dab2f104ec4950e
|
|
| MD5 |
0a11c85cd0bc6bb1ba469782845d1220
|
|
| BLAKE2b-256 |
becbe84f17ef0cfdc20654aaf97239577c2a748b6708024e306f3393285e6a8d
|
File details
Details for the file requirements_installer-1.0.1-py3-none-any.whl.
File metadata
- Download URL: requirements_installer-1.0.1-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8540f9f47f7252345e07c642d776a3ca7431c80e3da6ab43b55f59b1d22fa30f
|
|
| MD5 |
cd7f2761296be6ed174ed15575db9b0d
|
|
| BLAKE2b-256 |
645874b35c059cb76f8308e266ad9fd234cd902c3c093f7a6578684f15dd4e92
|