列出 python 包依赖项而不加载它们?

发布于 2024-09-02 10:22:21 字数 401 浏览 7 评论 0原文

假设Python包A需要B、C和D; 有没有办法列出 A → BCD 而不加载它们?
元数据 (yolk -M A) 中的 Requires 通常不完整,grr。
可以下载A.tar / A.egg,然后查看A/setup.py, 但其中一些非常血腥。

(我原以为至少可以机械化获得第一级依赖关系; 即使是 98% 的解决方案也比雪崩般的下载更好。)

一个相关问题: pip-upgrade-package-without-upgrading-dependencies

Say that python package A requires B, C and D;
is there a way to list A → B C D without loading them ?
Requires in the metadata (yolk -M A) are often incomplete, grr.
One can download A.tar / A.egg, then look through A/setup.py,
but some of those are pretty gory.

(I'd have thought that getting at least first-level dependencies could be mechanized;
even a 98 % solution would be better than avalanching downloads.)

A related question:
pip-upgrade-package-without-upgrading-dependencies

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

装纯掩盖桑 2024-09-09 10:22:21

Snakefood

sfood -fuq package.py | sfood-target-files 

将列出依赖项。

`-f` tells sfood to follow dependencies recursively
`-u` tells sfood to ignore unused imports
`-q` tells sfood to be quiet about debugging information

要从标准库中过滤掉模块,您可以使用

sfood -fuq package.py | sfood-filter-stdlib | sfood-target-files 

正如您已经注意到的,如果您想忽略其他目录,您还可以使用 sfood -I 标志。

Snakefood

sfood -fuq package.py | sfood-target-files 

will list the dependencies.

`-f` tells sfood to follow dependencies recursively
`-u` tells sfood to ignore unused imports
`-q` tells sfood to be quiet about debugging information

To filter out modules from the standard library, you could use

sfood -fuq package.py | sfood-filter-stdlib | sfood-target-files 

As you've already noted, if there are other directories you'd like ignored, you can also use the sfood -I flag.

独守阴晴ぅ圆缺 2024-09-09 10:22:21

标准库中的 modulefinder

2.3 版本中的新增功能。

该模块提供了一个ModuleFinder
可以用来确定的类
由 a 导入的模块集
脚本。 modulefinder.py 也可以
作为脚本运行,给出文件名
Python 脚本作为其参数,
之后是进口报告
将打印模块。

我不确定它是否符合您关于不加载模块的要求。 从这里

modulefinder使用字节码检查
找到依赖关系,因此是
没有任何可能的副作用
由于导入模块而导致
研究过。

有关使用 pylint 或 Gui2exe 的其他提示此处

modulefinder from the standard lib

New in version 2.3.

This module provides a ModuleFinder
class that can be used to determine
the set of modules imported by a
script. modulefinder.py can also be
run as a script, giving the filename
of a Python script as its argument,
after which a report of the imported
modules will be printed.

I am not sure if it complies with your requierement about not loading the modules. From here:

modulefinder use bytecode inspection
to find dependencies, and therefore is
free from any side-effects that may be
caused by importing the modules being
studied.

Other hints about the use of pylint or Gui2exe here

满意归宿 2024-09-09 10:22:21

如果您所说的包是指 pip 安装的包(而不是带有 __init__.py 的目录),那么您可以使用名为 pip 的 Python 包。例如:

def get_all_package_dependencies():
    """Return dictionary of installed packages to list of package dependencies."""
    return {
        dist.key: [r.key for r in dist.requires()]
        for dist in pip.get_installed_distributions()
    }

If by package you mean a pip installed package (and not a directory with an __init__.py), then you can use the Python package called pip. For example:

def get_all_package_dependencies():
    """Return dictionary of installed packages to list of package dependencies."""
    return {
        dist.key: [r.key for r in dist.requires()]
        for dist in pip.get_installed_distributions()
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文