A Python tool for trimming of unused imports and dependencies from Python projects. PyTrim helps keep your codebase clean by automatically removing unused dependencies from both source code and configuration files.
- Auto-Detection: Automatically finds unused dependencies without manual specification
- Multi-format Support: Handles Python files, requirements.txt, pyproject.toml, setup.py, poetry.lock, Pipfile, YAML files, Dockerfiles, and more
- Modular Architecture: Extensible design with separate extractors and removers for different file types
- CLI Interface: Easy-to-use command line interface with smart defaults
- Report Generation: Creates detailed Markdown reports of changes
- Git Integration: Automatic branch creation and PR generation
-
Install Docker
Follow the official instructions: https://docs.docker.com/engine/install/ -
Navigate to Your Project's Source Code Directory
In your terminal, change to the root directory of the source code you want to analyze.cd /path/to/your/source -
Run the PyTrim Container
For Linux/macOS:
docker run --rm -it -v "$(pwd):/project" karakatsanis/pytrimFor Windows (PowerShell):
docker run --rm -it -v "${PWD}:/project" karakatsanis/pytrimThis command:
- Downloads the PyTrim image from Docker Hub (first time only)
- Mounts your current directory into the container at
/project - Drops you into a shell inside the container
-
Run PyTrim inside the container
pytrim
You can use any PyTrim commands and options. For example:
pytrim --help pytrim -r pytrim -d src/
-
Exit the container
exit
pip install pytrimNote: Call graph analysis (the default detection method) requires the external tool PyCG. For the simplest experience with all features enabled, we recommend using the Docker image.
Call graph analysis is the default detection method, but it requires the external tool PyCG.
If you install PyTrim via PyPI, you must install PyCG manually for this feature to work.
- Install PyCG from source:
git clone https://github.com/gdrosos/PyCG.git cd PyCG pip install .
- Ensure the PyCG entrypoint is in PATH:
For the change to be permanent, add the following line to your shell's configuration file (e.g.,
~/.bashrc,~/.zshrc):After saving the file, restart your terminal or runexport PATH="$HOME/.local/bin:$PATH"
source ~/.bashrc(or your respective config file) to apply the changes.
After installation, use the pytrim command:
pytrim [-h] [-f FILE] [-d DIRECTORY] [-u UNUSED_IMPORTS [UNUSED_IMPORTS ...]] [-r] [-o] [-pr] [-dp | -fd] [-v] [-V]
[-gm [GENERATE_MAPPINGS ...]] [-mf MAPPINGS_FILE] [-e EXCLUDE [EXCLUDE ...]]
[project]-h, --help: Show help message and exit-f FILE, --file FILE: Process a single Python file-d DIRECTORY, --directory DIRECTORY: Process all.pyfiles in a directory-u UNUSED_IMPORTS [...], --unused-imports UNUSED_IMPORTS [...]: List of unused imports/dependencies to remove (optional - will auto-detect if not specified)-r, --report: Generate reports about trimmed packages in thepytrim_reportsfolder-o, --output: Create new debloated files in folderpytrim_outputinstead of overwriting originals-pr, --pull-request: Create a Git branch and GitHub Pull Request with changes-v, --verbose: Show detailed information about the trimming process.-V, --version: Show version information-dp, --deptry: Use deptry to find unused imports (requires deptry installed).-fd, --fawltydeps: Use fawltydeps to find unused dependencies (requires fawltydeps installed).-gm, --generate-mappings: Generate import mappings JSON file for specified packages (or use discovered packages if none specified), then run the full PyTrim analysis using the generated mappings.-mf, --mappings-file MAPPINGS_FILE: Use custom import mappings JSON file instead of built-in mappings.-e, --exclude: Exclude specific dependencies from the removal process. E.g., a transitive dependency that needs its version pinned.PROJECT: Project root directory (default: current directory)
Show help and all available options:
pytrim --helpTrim current project:
pytrimAuto-detect and remove unused dependencies from a project:
pytrim path/to/project/Remove unused imports from a single file:
pytrim -f src/main.py -u os sys pandasProcess all Python files in a directory:
pytrim -d src/ -u requests json numpyTrim current project with reporting:
pytrim -rAuto-detect specific project with reporting:
pytrim project/ -rClean an entire project with specific packages:
pytrim project/ -u pandas matplotlib seaborn -rCreate a Pull Request for current project:
pytrim -prFiles are modified in place. Only files that need changes are updated.
Creates new debloated files in the pytrim_output/ directory instead of overwriting the original files.
Generates detailed Markdown reports in the pytrim_reports/ directory with analysis of trimmed packages and dependencies.
- Files modified in place
- Creates Git branch with timestamp (format:
debloat/YYYYMMDDHHMMSS) - Generates
project_report.mdin project root - Automatically creates a GitHub Pull Request with the changes (requires
ghCLI) - Requirements: Git repository,
gitandghCLI tools installed
You can combine output modes:
-r -o: Generate reports AND save debloated files topytrim_output/-r -pr: Generate reports AND create pull request- All modes can be combined as needed
- Python 3.10+
git- Required for version control operationsghCLI - Required for pull request creation (-prflag)
Contributions are welcome! Please see CONTRIBUTING.md for development setup, contribution guidelines, and project architecture details.
- Issues: GitHub Issues
- Source Code: GitHub
MIT License - see LICENSE file for details.