This Python script automates the management of Python dependencies for a project by:
- Recursively scanning all Python files in the project directory.
- Extracting all imported packages.
- Checking for missing packages that are not installed in the current environment.
- Generating a
requirements.txt
file with all detected packages. - Installing any missing packages using
pip
.
- Recursive File Scanning: Automatically identifies all Python files in the specified project directory.
- Dependency Detection: Extracts imported packages from the identified Python files.
- Requirements File Generation: Creates or updates a
requirements.txt
file listing all detected dependencies. - Package Installation: Installs any missing dependencies using
pip
.
- Python 3.x installed on your system.
pip
installed for package management.
-
Run the Script:
- Save the script in the root directory of your project.
- Execute the script:
python script_name.py
-
Output:
- The script performs the following steps:
- Lists all Python files in the directory and subdirectories.
- Extracts a list of all imported packages.
- Writes the packages into a
requirements.txt
file. - Checks for missing packages and installs them if necessary.
- The script performs the following steps:
- Step 1: You run the script in your project directory.
- Example: Your project contains
app.py
andutils/helpers.py
.
- Example: Your project contains
- Step 2: The script scans all
.py
files and detects imports such asnumpy
,pandas
, etc. - Step 3: It checks if these packages are installed in your environment.
- Step 4: If any package is missing, it installs them using
pip
. - Step 5: A
requirements.txt
file is generated, containing:numpy pandas
requirements.txt
: A text file listing all detected dependencies, suitable for use withpip install -r requirements.txt
.
- Skips files with encoding issues and notifies the user.
- Handles
ImportError
for missing packages and attempts to install them.
- The script may detect packages that are not explicitly needed if they are dynamically imported or unused in the project.
- Use the generated
requirements.txt
file as a starting point and manually refine it as necessary.
This script is free to use and modify. No warranty is provided.