Home > OS >  Python - View & remove all unused dependencies in requirements.txt
Python - View & remove all unused dependencies in requirements.txt

Time:01-19

Somewhere along the way, the requirements.txt in my Python application has become extremely bloated. There are 100 dependencies listed and iteratively going through it and removing each dependency until the application breaks is not an option.

Does anyone know of a tool that can show which packages in the requirements.txt are being used during runtime? If no tool exists, how has anyone solved this problem in a more efficient way than deleting packages one-by-one?

CodePudding user response:

A Python application's requirements.txt file may be analysed for unneeded dependencies using many tools.

pip-tools: Python dependency management tools. The pip-check programme can find packages in a requirements.txt file that the application is not using.

pip install pip-tools
pip check

pip-autoremove: Removes superfluous dependencies from requirements.txt files. It removes required file packages that are not imported in any source code.

pip install pip-autoremove
pip-autoremove -r requirements.txt

pip-updater: Automatically updates and removes dependencies. This utility can update and delete packages.

pip install pip-updater
pip-updater -r requirements.txt

pyupgrade: Automatically upgrades and removes dependencies. It may upgrade and delete packages.

pip install pyupgrade
pyupgrade requirements.txt

Pyflakes can examine imports and dependencies in contemporary Python files.

Pipdeptree can produce a dependency tree for all your dependencies and show where they are utilised in your project.

pip install pipdeptree
pipdeptree

CodePudding user response:

You can see all requirements by:

pip freeze # python2
pip3 freeze # python3

Then you should compare these with requirements.txt and remove unused dependencies.

Also you can use pipreqs module. It's explained here.

  • Related