Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to detect unused packages and remove them

I use pip freeze > requirements.txt to gather all packages I installed. But after developing many days, some packages are now unused. How can I find these unused packages and remove them, to make my project more clear?

like image 586
user2492364 Avatar asked Apr 12 '16 03:04

user2492364


People also ask

Can I delete Python site packages?

You could open the Glitch terminal and navigate to this directory, and delete all files using the rm command. Be careful, this will remove all packages. Maybe you could use the terminal to just delete the ones you want to remove. This recreates the site-packages folder, for your app to (hopefully) start up again.

How do you check what all packages are installed in Python?

The Pip Package Manager can be used to list both globally and locally installed Python packages. The Pipenv, Anaconda Navigator and Conda package managers can also be used to list locally installed packages in their respective environments.

Does PIP uninstall remove dependencies?

For example, on my system this script eventually failed because the target package had dependencies in common with pip, so pip uninstalled its own dependencies before the script could finish, and then failed. Beware this removes only the next level down dependencies, but not the dependencies of those dependencies.

Where are Python packages stored?

If the Python installation was done from sources or from Python installation mechanisms (like easy_install or Python setup.py) and not from a packages manager like apt-get or aptitude among others, Python packages are stored under the /usr/local/lib/python<version>/ directory.


2 Answers

Inside Pycharm Go to code > inspect code Select Whole project option and click OK.

In inspection results panel locate Package requirements section under Python (note that this section will be showed only if there is any requirements.txt or setup.py file). The section will contain one of the following messages:

  • Package requirement '' is not satisfied if there is any package that is listed in requirements.txt but not used in any .py file.

  • Package '' is not listed in project requirements if there is any package that is used in .py files, but not listed in requirements.txt.

You have all your required packages remove/add them accordingly.

like image 101
MrSoloDolo Avatar answered Sep 29 '22 07:09

MrSoloDolo


I just found this: https://pypi.python.org/pypi/pip_check_reqs/2.0.

Find packages that should or should not be in requirements for a project

like image 26
Brad Rhoads Avatar answered Sep 29 '22 06:09

Brad Rhoads