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.

๐ŸŽฏ 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 .ipynb files 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: Setup
!pip install -q requirements-installer
from requirements_installer import auto_install
auto_install()

# 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

# Cell 1: Setup
!pip install -q requirements-installer
from requirements_installer import auto_install
auto_install()  # Scans entire notebook and installs everything!

# Cell 2+: Your code
import torch
from transformers import AutoModel
# ... rest of your notebook

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

  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., cv2 โ†’ opencv-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 โœ… โŒ โŒ โœ…
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.txt but 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

๐Ÿงช 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)

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


๐Ÿ†š 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:


๐Ÿ“ฌ Contact & Support


๐ŸŽ“ 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! โญ

Report Bug ยท Request Feature ยท Contribute

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.2.tar.gz (14.8 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.2-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: requirements_installer-1.0.2.tar.gz
  • Upload date:
  • Size: 14.8 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.2.tar.gz
Algorithm Hash digest
SHA256 9562c984edb1aafae8695e91de2dbdab24e37f4baab00f2116b986cafdd0c6b3
MD5 746a23214dd294c1dc84c73099755a96
BLAKE2b-256 764ad790112cb62eb5a217852705a548ccfe6a3b3882321220f6577264f99c4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for requirements_installer-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe5020ea696049e2bfd1337bfdab65ff4ccdb1230198dce2a906714d63fc59b7
MD5 48f42ff024e99ee4f1f9a07310282e1a
BLAKE2b-256 65e0f01128ce35c076f3565b38724593dee93f186f32badc77bda66d1555fe46

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