Python 从需求文件中删除未使用的包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25376213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:08:16  来源:igfitidea点击:

Delete unused packages from requirements file

pythonpiprefactoringpycharmrequirements.txt

提问by myildirim

Is there any easy way to delete no-more-using packages from requirementsfile?

有没有简单的方法可以从需求文件中删除不再使用的包?

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;

我为此任务编写了一个 bash 脚本,但是它没有按我预期的那样工作。因为,有些包没有跟在它们的 PyPI 项目名称之后使用。例如;

dj-database-url

package is used as

包用作

dj_database_url

My 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.

我的项目在它自己的需求文件中有很多包,所以,一个一个地搜索它们太混乱、容易出错并且需要太多时间。正如我搜索的那样,IDE 还没有这个属性。

回答by Steve Barnes

The best bet is to use a (fresh) python virtual-env with nopackages, or only those you definitely know you need, test your package - installing missing packages with pipas you hit problems which should be quite quick for most software then use the pip freezecommand to list the packages you really need. Better you you could use pip wheelto create a wheel with the packages in.
The other approach would be to:

最好的办法是使用一个(新鲜的)python virtual-env没有包,或者只有那些你肯定知道你需要的包,测试你的包 -pip当你遇到问题时安装丢失的包,这对于大多数软件来说应该很快,然后使用pip freeze命令列出你真正需要的包。更好的是,您可以使用pip wheel其中的包来创建一个轮子。
另一种方法是:

  1. Use pylintto check each file for unused imports and delete them, (you should be doing this anyway),
  2. Run your tests to make sure that it was right,
  3. Use a tool like snakefoodto generate your new list of dependencies
  1. 使用pylint检查每个文件中未使用的导入并删除它们,(无论如何你应该这样做),
  2. 运行你的测试以确保它是正确的,
  3. 使用诸如snakefood 之类的工具来生成新的依赖项列表

Note that for any dependency checking to work well it is advisable to avoid conditional import and import within functions.

请注意,要使任何依赖项检查正常工作,建议避免在函数内进行条件导入和导入。

Also note that to be sureyou have everything then it is a good idea to build a new virtual-env and install from your dependencies list then re-test your code.

另请注意,为了确保您拥有一切,最好构建一个新的虚拟环境并从您的依赖项列表中安装,然后重新测试您的代码。

回答by NickAb

You can use Code Inspection in PyCharm.

您可以在PyCharm 中使用代码检查。

Delete the contents of your requirements.txtbut keep the empty file. Load your project in, PyCharm go to Code -> Inspect code.... Choose Whole projectoption in dialog and click OK. In inspection results panel locate Package requirementssection under Python(note that this section will be showed only if there is any requirements.txtor setup.pyfile). The section will contain one of the following messages:

删除您的内容requirements.txt但保留空文件。加载您的项目,PyCharm 转到Code -> Inspect code.... Whole project在对话框中选择选项并单击OK。在检查结果面板中找到下面的Package requirements部分Python(请注意,只有在有任何文件requirements.txtsetup.py文件时才会显示此部分)。该部分将包含以下消息之一:

  • Package requirement '<package>' is not satisfiedif there is any package that is listed in requirements.txtbut not used in any .py file.
  • Package '<package>' is not listed in project requirementsif there is any package that is used in .py files, but not listed in requirements.txt.
  • Package requirement '<package>' is not satisfied如果有任何包requirements.txt在任何 .py 文件中列出但未使用。
  • Package '<package>' is not listed in project requirements如果有任何包在 .py 文件中使用,但未在requirements.txt.

You are interested in the second inspection. You can add all used packages to requirements.txtby right clicking the Package requirementssection 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.txtif called for section.

您对第二次检查感兴趣。您可以requirements.txt通过右键单击该Package requirements部分并选择来添加所有使用过的包Apply Fix 'Add requirements '<package>' to requirements.txt'。请注意,它只会显示一个包名称,但它实际上会将所有使用的包添加到requirements.txtif 调用部分。

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.

如果需要,您可以将它们一个一个添加,只需右键单击某个包对应的检查并选择Apply Fix 'Add requirements '<package>' to requirements.txt',每次此类检查重复一次。

After that you can create clean virtual environment and install packages from new requirements.txt.

之后,您可以创建干净的虚拟环境并从新的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.

还要注意的是PyCharm拥有自营优化功能,请参阅优化进口...。在上面列出的任何其他步骤之前使用此功能会很有用。

Apply fix for all packages not listed in requirements.txt

对requirements.txt 中未列出的所有包应用修复

回答by siulkilulki

I've used with success pip-check-reqs.

我已经成功使用pip-check-reqs

With command pip-extra-reqs your_directoryit will check for all unused dependencies in your_directory

使用命令pip-extra-reqs your_directory它将检查所有未使用的依赖项your_directory

Install it with pip install pip-check-reqs.

安装它pip install pip-check-reqs