删除requirements.txt的内容,但保留空白文件.加载项目,PyCharm转到Code -> Inspect code....在对话框中选择Whole project选项,然后单击OK.在检查结果面板中,找到Python下的Package requirements部分(请注意,只有存在任何requirements.txt或setup.py文件时,才会显示此部分).该部分将包含以下消息之一: Package requirement '<package>' is not satisfied,如果requirements.txt中列出了任何软件包,但未在任何.py文件中使用. Package '<package>' is not listed in project requirements,如果.py文件中使用了任何软件包,但requirements.txt中未列出.您对第二次检查感兴趣.通过右键单击Package requirements部分并选择Apply Fix 'Add requirements '<package>' to requirements.txt',可以将所有使用过的软件包添加到requirements.txt.请注意,它只会显示一个软件包名称,但是如果需要进行调用,它将实际上将所有使用过的软件包添加到requirements.txt中.如果需要,可以将它们一个接一个地添加,只需右键单击与某些包装相对应的检验,然后选择Apply Fix 'Add requirements '<package>' to requirements.txt',对每种此类检验重复上述操作.之后,您可以创建干净的虚拟环境并从新的requirements.txt安装软件包.还请注意,PyCharm具有导入优化功能,请参见优化导入... .在上面列出的任何其他步骤之前使用此功能可能会很有用. Is there any easy way to delete no-more-using packages from requirements file?I wrote a bash script for this task but, it doesn't work as I expected. Because, some packages are not used following their PyPI project names. For example;dj-database-urlpackage is used as dj_database_urlMy project has many packages in its own requirements file, so, searching them one-by-one is too messy, error-prone and takes too much time. As I searched, IDEs don't have this property, yet. 解决方案 You can use Code Inspection in PyCharm.Delete the contents of your requirements.txt but keep the empty file.Load your project in, PyCharm go to Code -> Inspect code.... Choose Whole project option in dialog 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 '<package>' is not satisfied if there is any package that is listed in requirements.txt but not used in any .py file.Package '<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 are interested in the second inspection.You can add all used packages to requirements.txt by right clicking the Package requirements section and selecting Apply Fix 'Add requirements '<package>' to requirements.txt'. Note that it will show only one package name, but it will actually add all used packages to requirements.txt if called for section.If you want, you can add them one by one, just right click the inspection corresponding to certain package and choose Apply Fix 'Add requirements '<package>' to requirements.txt', repeat for each inspection of this kind.After that you can create clean virtual environment and install packages from new requirements.txt.Also note that PyCharm has import optimisation feature, see Optimize imports.... It can be useful to use this feature before any other steps listed above. 这篇关于从需求文件中删除未使用的软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 05:12