1

I've been working on a project for a while now and have installed various packages over the time I've worked with it, some of which I suspect are no longer required to run the code, and I don't want to include them in the requirements.txt. Is there any way to know which installed packages in my venv are either imported somewhere in my Python project or are a dependency of a package that is imported, so that I can remove the unused packages from the requirements.txt?

  • My question is related to How can I 'clean up' a virtualenv? but is not the same, because in that case the OP knew which top-level package (of several he installed) he wanted to keep, and was trying to figure out how to uninstall the others along with their dependencies, while in my case I don't even know which top-level packages I need.
  • My question is similar to this one regarding the R programming language.
Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
  • Just comment out the import and see whether the program runs smoothly. If it does, the import was not necessary. And as a general rule: Only import packages you use at the moment you start using them. – Martin Wettstein Jul 29 '21 at 08:45

1 Answers1

3

I found a solution to my problem here: use pipreqs to get a list of the top-level packages actually required by a project:

pip install pipreqs and then pipreqs . (from the top level of your project).

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95