Python: How to detect unused packages and remove them

22,264

Solution 1

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.

Solution 2

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

Share:
22,264

Related videos on Youtube

user2492364
Author by

user2492364

Updated on August 07, 2020

Comments

  • user2492364
    user2492364 over 2 years

    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?

  • Khushhal over 2 years
    It gives error: ModuleNotFoundError: No module named 'pip._internal.download' It doesn't work in June 2020 because PYPA have changed how pip behaves so few pip APIs are not available for direct use, making this library completely useless.
  • Janaka Peiris
    Janaka Peiris about 1 year
    @Khushhalm Use the latest version 2.3.0 to get rid of this error. But still I'm not sure of its pip-extra-reqs output, as it listed almost all packages