A smart Python tool that automatically scans your Python projects, detects imports, generates requirements.txt, and sets up a virtual environment. It's especially useful for handling imports where package names differ from import names (like cv2
→ opencv-python
).
- 🔍 Smart Import Detection: Recognizes common packages like
cv2
,mediapipe
,tensorflow
even if not installed - 🗺️ Import Name Mapping: Handles cases where import names differ from package names
- 📝 Requirements Generation: Creates
requirements.txt
in your project directory - 🔧 Virtual Environment: Automatically creates/reuses virtual environment
- 📦 Package Installation: Installs all required packages with confirmation
- 🔄 Encoding Support: Automatically handles different file encodings
# Install required package
pip install chardet
# Clone the repository
git clone https://github.com/science64/python-dependency-scanner.git
cd python-dependency-scanner
# Run the scanner (interactive mode)
python dependency_scanner.py
# Or specify directory directly
python dependency_scanner.py -i /path/to/your/project
- Clone the repository:
git clone https://github.com/science64/python-dependency-scanner.git
cd python-dependency-scanner
- Install required package:
pip install chardet
# Scan current directory
python dependency_scanner.py
# Scan specific project
python dependency_scanner.py -i /path/to/project
When you run the scanner, it will:
- Recursively scan all Python files in the directory
- Detect imports like:
- Standard imports (
import numpy
) - Aliased imports (
import pandas as pd
) - From imports (
from PIL import Image
)
- Standard imports (
- Create requirements.txt
- Set up virtual environment
- Install packages after confirmation
Scanning directory: /path/to/project
Found imports in main.py:
- opencv-python (from import cv2)
- mediapipe
- numpy
- python-dotenv
Creating requirements.txt...
Requirements file created at: /path/to/project/requirements.txt
Creating new virtual environment: .venv
The following modules will be installed:
- opencv-python
- mediapipe
- numpy
- python-dotenv
Do you want to proceed with the installation? (yes/no):
The scanner recognizes packages in three ways:
- 📗 Through the package mapping file
- 🔍 By checking installed packages
- 📦 Through a built-in list of common packages
Common packages that are automatically recognized include:
cv2
(opencv-python)mediapipe
tensorflow
torch
pandas
numpy
PIL
- And more...
The tool includes a package_mapping.json
file that maps import names to package names. For example:
{
"PIL": "Pillow",
"cv2": "opencv-python",
"sklearn": "scikit-learn",
"bs4": "beautifulsoup4"
}
You can add your own mappings:
- Open
package_mapping.json
- Add new entries:
{ "your_import": "package-name" }
After running the scanner, your project will look like this:
your-project/
├── .venv/ # Virtual environment
├── requirements.txt # Generated requirements
├── your_python_files.py
└── ...
If certain imports aren't being detected:
- Run with
-i
flag to specify directory explicitly - Check if the Python file has proper
.py
extension - Verify file permissions
- Ensure the file isn't in an excluded directory (like
.venv
)
Contributions are welcome! Areas for improvement:
- Additional package mappings
- Support for more virtual environment configurations
- Enhanced import detection patterns
- Performance optimizations
This project is licensed under the MIT License - see the LICENSE file for details.
- Thanks to the
chardet
library for encoding detection - All contributors to the package mapping database
- Python community for package naming standards