Auto‑Clean Python Dependencies with pipreqs and Pre‑Commit

Learn how to automatically remove unused Python packages from your virtual environment by generating an accurate requirements.txt with pipreqs and enforcing clean dependencies on every commit using a pre‑commit hook, improving build size, speed, and security.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Auto‑Clean Python Dependencies with pipreqs and Pre‑Commit

After working on a Python project for a while, your .venv often accumulates libraries you no longer use, inflating the environment and slowing down builds. Your requirements.txt may be filled with unnecessary packages.

Increases environment size

Slows deployment and CI/CD pipelines

Causes version conflicts

Introduces unnecessary security vulnerabilities

In the example, three libraries— pandas, polars and requests —are installed, but only requests is actually needed.

Your code's actual dependencies

pipreqs

scans your source code and generates a new requirements.txt that contains only the packages you really import.

Installation is simple: pip install pipreqs Then run: pipreqs . --force This command overwrites the existing requirements.txt with a clean version.

Example of the original requirements.txt versus the cleaned one:

Requests==2.32.5
certifi==2025.10.5
charset-normalizer==3.4.4
idna==3.11
numpy==2.3.4
pandas==2.3.3
polars==1.34.0
polars-runtime-32==1.34.0
python-dateutil==2.9.0.post0
pytz==2025.2
requests==2.32.5
six==1.17.0
tzdata==2025.2
urllib3==2.5.0

Now you can recreate a clean virtual environment and install only the needed packages:

python -m venv venv
source venv/bin/activate   # on Windows: venv\Scripts\activate
pip install -r requirements.txt

Before committing, ensure the requirements.txt is clean.

Use Pre‑Commit for dependency cleanup

Automate the cleanup with a Git pre‑commit hook that runs pipreqs before each commit.

Set up Pre‑Commit

First install the tool: pip install pre-commit Initialize the hook in your repository: pre-commit install Create a .pre-commit-config.yaml file with the following content:

repos:
- repo: local
  hooks:
  - id: clean-python-deps
    name: Clean Python Dependencies
    entry: bash -c "pip install pipreqs && pipreqs . --force"
    language: system
    pass_filenames: false
    stages: [commit]

Test the hook:

git add .
git commit -m "test cleanup hook"

You will see output similar to:

(venv) PS C:\test> git commit -m "test cleanup hook"
Clean Python Dependencies................................................

With this setup, your dependency list self‑repairs on every commit.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

pipreqsrequirements.txtvirtualenvpre-commitdependency-cleanup
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.