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

发布于 2025-01-22 23:21:52 字数 283 浏览 1 评论 0原文

是否有任何简单的方法可以从要求文件中删除无需使用的软件包?

我为此任务编写了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

み青杉依旧 2025-01-29 23:21:53

最好的选择是使用( Fresh )python venv/virtual -env, no 套件,或者只有您绝对知道需要的python venv/virtual -env,请测试包裹 - 安装缺失的包装使用pip,当您点击问题时,对于大多数软件来说应该很快,然后使用pip冻结命令列出您真正需要的软件包。更好的是,您可以使用pip wheel在包装中创建轮子。
另一种方法是:

  1. 使用 pylint 检查每个文件是否未使用导入并删除它们,(您应该是无论如何这样做),
  2. 运行测试以确保其正确,
  3. 使用 snakefood3 要生成您的新依赖性列表,

请注意,对于任何依赖性检查的工作良好在功能中导入。

另请注意,要成为确定您拥有一切,那么最好构建一个新的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 the pip freeze command to list the packages you really need. Better you you could use pip wheel to create a wheel with the packages in.
The other approach would be to:

  1. Use pylint to 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 snakefood or snakefood3 to generate your new list of dependencies

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.

错々过的事 2025-01-29 23:21:53

pycharm 中转到工具 - >同步Python要求。有一个“删除未使用的要求”复选框。

In pycharm go to Tools -> Sync Python Requirements. There's a 'Remove unused requirements' checkbox.

蓝眼睛不忧郁 2025-01-29 23:21:53

我已经成功地使用了 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 in your_directory

Install it with pip install pip-check-reqs.

归途 2025-01-29 23:21:52

您可以在

  1. 删除unignts.txt的内容,但请保留空文件。
  2. 加载您的项目,
  3. pycharm转到代码 - >检查代码...
  4. 在对话框中选择整个项目选项,然后单击ok
    在检查结果中,面板位置软件包要求 python下的部分(请注意,只有在有任何sumplions.txtsetup.py 文件)。
    本节将包含以下消息之一:
  • 软件包要求'如果在unignts.txt中列出的任何软件包,则不满意,但在任何.py文件中未使用。
  • 软件包'< package>'如果在.py文件中使用的任何软件包,但未在unigess.txt中列出的任何软件包,则不在项目需求中列出。

您对第二次检查感兴趣。
您可以通过右键单击软件包需求部分,然后选择应用程序fix fix fix'添加要求'>'''>' tosuent.txt'。请注意,它将仅显示一个软件包名称,但实际上会将所有使用的软件包添加到unignts.txt(如果拨款)中。

如果愿意,可以一一添加它们,只需右键单击与某些软件包相对应的检查,然后选择“添加要求”>'对于需求。

之后,您可以创建清洁的虚拟环境,并通过新的unignts.txt安装软件包。

另请注意,Pycharm具有导入优化功能,请参见

“应用程序中未列出的所有软件包的应用程序.txt”

You can use Code Inspection in PyCharm.

  1. Delete the contents of your requirements.txt but keep the empty file.
  2. Load your project in,
  3. PyCharm go to Code -> Inspect code....
  4. 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.

Apply fix for all packages not listed in requirements.txt

抽个烟儿 2025-01-29 23:21:52

您可以使用 deptry ,命令行实用程序检查项目依赖性各种问题,例如未使用,缺失或传递依赖性。

将其添加到您的项目中

pip install deptry

,然后运行

deptry .

示例输出:

Scanning 2 files...

requirements.txt: DEP002 'pandas' defined as a dependency but not used in the codebase
Found 1 dependency issue.

请注意,要获得最佳结果,您应该为项目使用虚拟环境,请参阅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

pip install deptry

and then run

deptry .

Example output:

Scanning 2 files...

requirements.txt: DEP002 'pandas' defined as a dependency but not used in the codebase
Found 1 dependency issue.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文