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.
๐ฏ Two Ways to Use
1๏ธโฃ Inside Your Code (Easiest!)
from requirements_installer import auto_install
auto_install()
2๏ธโฃ Command Line
requirements_installer --file mycode.py
โจ Features
- โก Auto-Install Inside Code - Just
from requirements_installer import auto_install; auto_install()and you're done! - ๐ Jupyter/Colab Support - Scans and installs from
.ipynbfiles automatically - ๐ 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 100+ tricky cases like
cv2โopencv-python,pptxโpython-pptx - ๐ One-Hop Local Import Scanning - Follows local imports to catch all dependencies
- ๐ช Pure Python - No external dependencies required
๐ฅ 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
โญ NEW: Auto-Install Inside Your Code (Recommended!)
The easiest way - just add 2 lines to your script:
from requirements_installer import auto_install
auto_install()
# Now use any packages - they'll be auto-installed if missing!
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
Perfect for Jupyter/Colab notebooks:
# Cell 1: Your imports and code
import pandas as pd
import torch
from transformers import AutoModel
# ... your code ...
# Cell 2: Install everything (run this if imports fail)
!pip install -q requirements_installer
from requirements_installer import auto_install
auto_install() # Scans all cells above and installs missing packages!
# Now re-run Cell 1 and it will work!
Or install dependencies first:
# Cell 1: Setup (run first)
!pip install -q requirements_installer
from requirements_installer import auto_install
auto_install() # If you have imports in other cells, it will find them
# Cell 2+: Your code with any imports
import torch
import transformers
Basic Command-Line Usage
After pip install requirements_installer, use it as a command:
# Scan and install dependencies for a Python file
requirements_installer --file mycode.py
# Works with Jupyter notebooks too!
requirements_installer --file notebook.ipynb
With Virtual Environment
# Create a venv and install dependencies there
requirements_installer --file mycode.py --use-venv --venv-path .venv
Interactive Version Selection
# Choose specific versions for each package
requirements_installer --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
requirements_installer --file mycode.py --print-requirements
๐ Usage Examples
Example 1: Auto-Install in Script (NEW! ๐)
Make your scripts self-installing:
# my_analysis.py
from requirements_installer import auto_install
auto_install()
# Now use any packages!
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
plt.plot(df['x'], df['y'])
plt.show()
Just run it:
python my_analysis.py # Auto-installs pandas, numpy, matplotlib!
Example 2: Jupyter/Colab Notebook
Option A - Fix import errors after they happen:
# Cell 1: Try to run your code
import pandas as pd
import torch
# ImportError: No module named 'torch'
# Cell 2: Install missing dependencies
!pip install -q requirements_installer
from requirements_installer import auto_install
auto_install() # Scans ALL cells and installs what's needed!
# Cell 3: Re-run Cell 1 - now it works!
Option B - Set up first (recommended for sharing):
# Cell 1: One-time setup
!pip install -q requirements_installer
from requirements_installer import auto_install
auto_install() # Will scan entire notebook session
# Cell 2+: Your code
import torch
from transformers import AutoModel
model = AutoModel.from_pretrained('bert-base-uncased')
How it works in Colab:
- Reads all executed cells from your notebook session
- Finds all import statements
- Installs missing packages automatically
- No need to save the notebook as a file!
Example 3: Quick Script Setup
You clone a repo without a requirements.txt:
requirements_installer --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 4: Isolated Development Environment
requirements_installer --file main.py --use-venv
Creates a .venv folder and installs all dependencies there, keeping your system Python clean.
Example 5: Precise Version Control
requirements_installer --file analysis.py --ask-version
Prompts you for each package, letting you pin versions for reproducibility.
๐ง Command-Line Options
After installation, use the requirements_installer command:
usage: requirements_installer [-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 or Jupyter notebook (.ipynb) to scan
--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
Examples:
# Basic usage
requirements_installer --file mycode.py
# With virtual environment
requirements_installer --file mycode.py --use-venv
# Interactive version selection
requirements_installer --file mycode.py --ask-version
# Scan Jupyter notebook
requirements_installer --file notebook.ipynb
Note: If you cloned the repository and want to run directly without installation:
python requirements_installer.py --file mycode.py
๐ Python API
from requirements_installer import auto_install
# Auto-detect and install (recommended)
auto_install()
# Specify file explicitly
auto_install(file_path='script.py')
auto_install(file_path='notebook.ipynb')
# Use virtual environment
auto_install(use_venv=True, venv_path='.venv')
# Quiet mode (only show errors)
auto_install(quiet=True)
๐ฏ 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 | โ | โ | โ | โ |
| Call from inside code | โ | โ | โ | โ ๏ธ |
| Jupyter notebook support | โ | โ ๏ธ | โ | โ |
| 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:
- โจ Call
auto_install()directly in your code - ๐ Full Jupyter/Colab support
- ๐ Self-installing scripts
- ๐ช 100+ module-to-package mappings
- โจ Call
๐งช Module-to-Package Mappings
The tool includes smart mappings for 100+ common cases where the import name differs from the PyPI package name:
cv2 โ opencv-python
PIL โ Pillow
sklearn โ scikit-learn
yaml โ PyYAML
bs4 โ beautifulsoup4
pptx โ python-pptx
docx โ python-docx
MySQLdb โ mysqlclient
psycopg2 โ psycopg2-binary
telegram โ python-telegram-bot
discord โ discord.py
serial โ pyserial
git โ GitPython
# ... and 90+ 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)
- Jupyter/Colab: Scans all executed cells in the session. Make sure to run cells with imports before calling
auto_install()
These are deliberate trade-offs for simplicity and speed. For complex projects with intricate dependency trees, consider using Poetry or Pipenv.
๐ Troubleshooting
Google Colab: "Could not detect file to scan"
Problem: You get this error in Colab:
โ Could not detect file to scan. Please provide file_path argument.
Solution: In Colab, auto_install() reads from your executed cells. Make sure you:
- Run cells with imports first, then run
auto_install():
# Cell 1: Your code with imports
import pandas as pd
import numpy as np
# Cell 2: Run this cell
!pip install -q requirements_installer
from requirements_installer import auto_install
auto_install() # This reads Cell 1 and installs pandas, numpy
- Or, if you already ran your code and got import errors, just run
auto_install()- it will scan all previous cells!
Command not found
# If 'requirements_installer' command not found, try:
python -m requirements_installer --file mycode.py
# Or check if installed:
pip show requirements_installer
Imports not detected
Make sure you've executed the cells containing your imports. In Jupyter/Colab, only executed cells are scanned.
๐ Command Line vs auto_install()
Command Line (traditional):
pip install requirements_installer
requirements_installer --file mycode.py
python mycode.py
auto_install() (modern - recommended):
python mycode.py # That's it! Self-installing
๐ค 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
Development Setup
git clone https://github.com/FH-Prevail/requirements_installer.git
cd requirements_installer
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -e .
Running Tests
python -m pytest tests/
๐ License
MIT License - see LICENSE file for details.
๐ Acknowledgments
Inspired by:
- pipreqs - For the import scanning approach
- pigar - For AST parsing techniques
- pythonrunscript - For auto-installation workflows
๐ฌ Contact & Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Repository: github.com/FH-Prevail/requirements_installer
- Author: Sina
๐ Perfect For:
- ๐ Jupyter/Colab notebooks - Make them truly self-contained
- ๐ Quick scripts - No setup, just run
- ๐ฅ Sharing code - Recipients don't need to know about dependencies
- ๐ Teaching - Students can run examples immediately
- ๐ฌ Research - Reproducible notebooks that "just work"
- ๐ค Automation - Scripts that set themselves up
Made with โค๏ธ by Sina
Star this repo if you find it useful! โญ
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.4.tar.gz.
File metadata
- Download URL: requirements_installer-1.0.4.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b90f707f0f5fd466d765bcb1a83e1b382b252d55c3b4eda914542d0211c33db1
|
|
| MD5 |
169721a8325cbe592d20010b0c8b856c
|
|
| BLAKE2b-256 |
66787bd0aba91a9a265968ac4f0ec3e20a7f095d58621e2a713bda3a8ecd55be
|
File details
Details for the file requirements_installer-1.0.4-py3-none-any.whl.
File metadata
- Download URL: requirements_installer-1.0.4-py3-none-any.whl
- Upload date:
- Size: 15.9 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 |
573d2ce12ca480daaff68eef351bd9e68b3d6a581d2b8b52359b8ee47cf704b7
|
|
| MD5 |
dc4115496c763692c3ac15c2014bc2a2
|
|
| BLAKE2b-256 |
49478bfe0106dc29485d72e81e0ee5f9682ca4261e9b75b56f1fa5a37c153c21
|