Auto-Dep is a command-line tool and Python package that scans Python projects to automatically detect dependencies. It then generates dependency files such as requirements.txt and environment.yml to facilitate easy installation of third-party packages.
- Scans Python files in a specified directory (recursively) to detect imported modules.
- Filters out standard library modules to identify third-party packages only.
- Optionally ignores specific folders or directories, such as virtual environments or build artifacts.
- Writes output files:
requirements.txt(pip-style dependencies)environment.yml(conda-style environment specification)dependencies_summary.txt(optional summary of detected packages)
- Offers a command-line interface (
auto-dep) with various options for customization.
auto_dep/
βββ auto_dep/
β βββ __init__.py # Package definition with version
β βββ cli.py # Command-line interface definitions
β βββ scanner.py # Scans directories for Python files
β βββ parser.py # Parses Python files (AST) to detect imports
β βββ filter.py # Filters out standard library modules
β βββ writer.py # Writes requirements.txt, environment.yml, or summaries
βββ tests/
β βββ test_cli.py # Unit tests for CLI
β βββ test_edge_cases.py
β βββ test_filter.py
β βββ test_parser.py
β βββ test_scanner.py
β βββ test_writer.py
βββ ...
- Conda Users
- (Optional) Create a clean environment:
conda create -n auto-dep python=3.10 conda activate auto-dep - Install dependencies using
requirements.txt:pip install -r requirements.txt - Alternatively, install dependencies via
environment.yml(if present):conda env create -f environment.yml conda activate autoenv
- (Optional) Create a clean environment:
- Pip Users
- Run:
pip install -r requirements.txt
- Run:
From the terminal, navigate to the directory containing your Python project or specify it via the --project-dir argument:
auto-dep --project-dir /path/to/python/projectBy default, this will:
- Recursively scan
/path/to/python/projectfor.pyfiles. - Parse each
.pyfile for imports. - Exclude standard library modules.
- Write out a
requirements.txtandenvironment.ymlfile in the same directory (unless overridden).
Use auto-dep --help to view all available options. Some of the key arguments include:
--project-dir, -p Root directory of the Python project (default: current)
--output-dir, -o Directory to write output files (default: same as project)
--env-name, -e Environment name for environment.yml (default: autoenv)
--python-version, -v Python version to use (e.g., 3.8)
--verbose Enable verbose logging
--quiet Suppress all output except errors
--confirm, -c Ask for confirmation before writing files
--skip-environment-yml Skip generating environment.yml
--skip-requirements-txt
Skip generating requirements.txt
--generate-summary, -s Generate a dependencies_summary.txt file
--ignore-dir, -i Additional directories to ignore (repeatable)
-
Scanner
- Recursively walks through the project directory structure, respecting ignored directories (e.g.,
.git,__pycache__,node_modules, etc.). - Collects all
.pyfiles.
- Recursively walks through the project directory structure, respecting ignored directories (e.g.,
-
Parser
- For each
.pyfile, uses Python'sastmodule to parse and identify import statements (import xorfrom x import y). - Skips or safely handles files with syntax errors, encoding issues, etc.
- For each
-
Filter
- Compares detected imports against lists of standard library modules for the configured Python version.
- Excludes standard library imports, leaving only third-party packages.
-
Writer
- Generates
requirements.txtlisting sorted package names. - Generates
environment.ymlwith a conda environment specification. - (Optional) Generates a summary text file with all detected third-party packages.
- Generates
-
CLI
- Manages argument parsing, logging levels, confirmation prompts (
--confirm), and orchestrates the scanning, parsing, filtering, and writing processes.
- Manages argument parsing, logging levels, confirmation prompts (
Contributions are welcome! Feel free to:
- Submit pull requests for new features, bug fixes, or improvements.
- Add or improve test cases in the
tests/directory. - Suggest new or updated docstrings and documentation.
The tests/ folder uses unittest (and can be run via pytest or unittest). For example:
pytestThis executes all test modules, including:
test_scanner.pytest_parser.pytest_filter.pytest_writer.pytest_cli.pytest_edge_cases.py
(If applicable, choose an open-source license. Currently no explicit license is provided in the codebase.)
Happy coding with Auto-Dep! If you have questions or issues, please open an issue or submit a pull request.