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

den*_*nis 22 python dependencies packaging

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

(我认为至少可以获得一级依赖关系;即使是98%的解决方案也会比雪崩下载更好.)

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

unu*_*tbu 29

Snakefood

sfood -fuq package.py | sfood-target-files 
Run Code Online (Sandbox Code Playgroud)

将列出依赖项.

`-f` tells sfood to follow dependencies recursively
`-u` tells sfood to ignore unused imports
`-q` tells sfood to be quiet about debugging information
Run Code Online (Sandbox Code Playgroud)

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

sfood -fuq package.py | sfood-filter-stdlib | sfood-target-files 
Run Code Online (Sandbox Code Playgroud)

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


joa*_*uin 13

来自标准库的modulefinder

版本2.3中的新功能.

此模块提供ModuleFinder类,可用于确定脚本导入的模块集.modulefinder.py也可以作为脚本运行,以Python脚本的文件名作为参数,之后将打印导入模块的报告.

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

modulefinder使用字节码检查来查找依赖关系,因此不会因导入正在研究的模块而导致任何副作用.

关于在这里使用pylint或Gui2exe的其他提示

  • modulefinder确实有效,但它会显示许多无用的东西和间接依赖(依赖关系可以使用的依赖关系,但实际上并非如此).你可以在这里找到我使用modulefinder做的示例脚本:https://gist.github.com/lrq3000/6175634这里有一个使用AST解析方法的替代脚本(对我来说效果更好):https:// gist .github.com/lrq3000/6175522 (3认同)

Tim*_*ark 6

如果你打包是指一个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()
    }
Run Code Online (Sandbox Code Playgroud)