This script scans a directory for Python files, extracts the imported modules, and generates a requirements.txt
file with the necessary packages and their minimum versions.
To use this script, place it in the root directory of your project and run it. The script will generate a requirements.txt
file with the required packages.
python get_requirements.py
Finds all imported modules in a given Python file.
file_path (str): The path to the Python file.
set: A set of imported modules.
Returns a dictionary of currently installed packages and their versions. ### Returns: dict: A dictionary where the keys are package names and the values are versions.
Recursively scans the directory for .py files and extracts imports. ### Parameters: directory (str): The path to the directory to scan. ### Returns: set: A set of all found imported modules.
Filters out installed Python packages to avoid unnecessary entries.
### Parameters:
imports (set): A set of imported modules.
### Returns:
dict: A dictionary of installed packages and their versions.
Writes the requirements to a file.
### Parameters:
requirements (dict): A dictionary of packages and their versions.
Main function that scans Python files, filters installed packages, and writes the requirements to a file.