Python: How to detect unused packages and remove them

扶醉桌前 提交于 2021-01-26 20:36:34

问题


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?


回答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.




回答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




回答3:


I doubt there can be a fully automatic way to do this. "Unused packages" is a very ambiguous statement: unused by whom? The only way for a system utility to figure out whether a package is used somewhere, or not, is to parse every python script installed anywhere in the system; a rather impractical solution. So, what you could do, is to look in every python script and module you created; find what is being imported and then, if you have two different requirements.txt files from before you installed the packages and one after, it may be possible to figure out which ones you can uninstall without breaking anything. I do not recommend this though. A much better way is to use virtual environments, but you must do this before you start developing and installing new packages.



来源:https://stackoverflow.com/questions/36562873/python-how-to-detect-unused-packages-and-remove-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!