1

I have Python project and have installed hundreds of packages using pip over time. However, I have since improved my code and suspect that some of these packages are no longer in use. I want to remove all of the unused packages.

I am already familiar with the pip uninstall package-name command and have seen suggestions to use pip-autoremove package-name, but it's not considered to be the optimal option for my case as there are hundreds of unused packages, and I do not want to manually uninstall each one.

Additionally, I do not want to accidentally remove packages that are still being used in my project.

I am wondering if there is a better solution, or a way to look for python imports inside files and detect for unused. Any latest or alternative approaches would be highly appreciated. Thank you.

Here's my requirements.txt with thousands of packages: enter image description here

iihsan
  • 134
  • 1
  • 9
  • 4
    thats why you should use venv and use only needed packages for the project. – robotiaga Apr 19 '23 at 06:19
  • did you look on this answer? https://stackoverflow.com/questions/25376213/delete-unused-packages-from-requirements-file it looks like it solve your problem – Dump Eldor Apr 19 '23 at 06:21
  • 2
    In addition to @DumpEldor 's linked question, I also recommend having unit tests in CI running with both a minimum version constrained `pkg >= version` set of directly used packages, as well as a pinned set `pkg == version` from pip-compile/pip-freeze. This way you have a frozen set of versions to use in production while also ensuring that the latest versions of dependencies will work when you go to update the pip freeze requirements . – flakes Apr 19 '23 at 06:33
  • Yes, I have tried that as well, but Package requirements section is not showing for me. – iihsan Apr 19 '23 at 07:52
  • It's better to look at it from the other direction: find the packages that you _do_ need to run your app/project. Create a venv with _no_ packages installed. Run, importerror, install missing packages, run again, importerror, ... repeat until you have all the packages. Then `pip freeze` your new requirements.txt. Much _much_ better if you already have tests you can just keep running. Similar to the answer here: https://stackoverflow.com/a/42237193/2745495 – Gino Mempin Apr 25 '23 at 12:55

1 Answers1

0

Dependency management in Python is a painful topic. Check out this excellent overview: https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/

I highly recommend you check out Poetry or Hatch for dependency and packaging management. In general you should at least always use virtual environment. This allows you to completely delete it and create new, if you need.

You have your requirements.txt so you can do python -m venv <venv> where <venv> is the name you want to give to your virtual environment. It creates a directory where you will install the packages. Activating the virtual environment goes like this: source <venv>/bin/activate which executes a shell script. Among other things it defines a command deactivate which you can call if you wish to exit from the virtual environment. With the activated venv everything you install will be installed there.

adam
  • 159
  • 9