从需求文件中删除未使用的软件包
是否有任何简单的方法可以从要求文件中删除无需使用的软件包?
我为此任务编写了一个bash脚本,但是,它没有我预期的。因为,某些软件包不会按照其PYPI项目名称使用。例如;
dj-database-url
软件包被用作
dj_database_url
我自己的需求文件中有许多软件包,因此,逐一搜索它们太凌乱,容易出错并且需要太多时间。当我搜索时,IDE还没有此属性。
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-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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最好的选择是使用( Fresh )python venv/virtual -env, no 套件,或者只有您绝对知道需要的python venv/virtual -env,请测试包裹 - 安装缺失的包装使用
pip
,当您点击问题时,对于大多数软件来说应该很快,然后使用pip冻结
命令列出您真正需要的软件包。更好的是,您可以使用pip wheel
在包装中创建轮子。另一种方法是:
请注意,对于任何依赖性检查的工作良好在功能中导入。
另请注意,要成为确定您拥有一切,那么最好构建一个新的VENV/VIRTUAL-ENV并从依赖项列表中安装,然后重新测试您的代码。
The best bet is to use a (fresh) python venv/virtual-env with no packages, or only those you definitely know you need, test your package - installing missing packages with
pip
as you hit problems which should be quite quick for most software then use thepip freeze
command to list the packages you really need. Better you you could usepip wheel
to create a wheel with the packages in.The other approach would be to:
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 sure you have everything then it is a good idea to build a new venv/virtual-env and install from your dependencies list then re-test your code.
在 pycharm 中转到工具 - >同步Python要求。有一个“删除未使用的要求”复选框。
In pycharm go to Tools -> Sync Python Requirements. There's a 'Remove unused requirements' checkbox.
我已经成功地使用了 pip-check-reqs> pip-check-reqs 。
使用命令
pip-extra-reqs your_directory
它将在your_directory
中检查所有未使用的依赖项使用
pip pip install pip-check-receck-reqs
安装。I've used with success pip-check-reqs.
With command
pip-extra-reqs your_directory
it will check for all unused dependencies inyour_directory
Install it with
pip install pip-check-reqs
.您可以在
unignts.txt
的内容,但请保留空文件。代码 - >检查代码...
。整个项目
选项,然后单击ok
。在检查结果中,面板位置
软件包要求
python
下的部分(请注意,只有在有任何sumplions.txt
或setup.py 文件)。
本节将包含以下消息之一:
软件包要求'如果在
,但在任何.py文件中未使用。unignts.txt
中列出的任何软件包,则不满意软件包'< package>'如果在.py文件中使用的任何软件包,但未在
中列出。unigess.txt
中列出的任何软件包,则不在项目需求您对第二次检查感兴趣。
您可以通过右键单击
软件包需求
部分,然后选择应用程序fix fix fix'添加要求'>'''>' tosuent.txt'
。请注意,它将仅显示一个软件包名称,但实际上会将所有使用的软件包添加到unignts.txt
(如果拨款)中。如果愿意,可以一一添加它们,只需右键单击与某些软件包相对应的检查,然后选择“添加要求”>'对于需求。
之后,您可以创建清洁的虚拟环境,并通过新的
unignts.txt
安装软件包。另请注意,Pycharm具有导入优化功能,请参见
You can use Code Inspection in PyCharm.
requirements.txt
but keep the empty file.Code -> Inspect code...
.Whole project
option in dialog and clickOK
.In inspection results panel locate
Package requirements
section underPython
(note that this section will be showed only if there is anyrequirements.txt
orsetup.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 inrequirements.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 inrequirements.txt
.You are interested in the second inspection.
You can add all used packages to
requirements.txt
by right clicking thePackage requirements
section and selectingApply Fix 'Add requirements '<package>' to requirements.txt'
. Note that it will show only one package name, but it will actually add all used packages torequirements.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.
您可以使用 deptry ,命令行实用程序检查项目依赖性各种问题,例如未使用,缺失或传递依赖性。
将其添加到您的项目中
,然后运行
示例输出:
请注意,要获得最佳结果,您应该为项目使用虚拟环境,请参阅EG 在这里。
免责声明:我是
deptry
的作者。You can find obsolete dependencies by using deptry, a command line utility that checks for various issues with a project's dependencies, such as unused, missing or transitive dependencies.
Add it to your project with
and then run
Example output:
Note that for the best results, you should be using a virtual environment for your project, see e.g. here.
Disclaimer: I am the author of
deptry
.