5

是否可以让 distutils 只运行 python 模块依赖性分析(并可能安装缺少的模块)而不实际安装有问题的 python 模块?我想象一个命令如下:

./setup.py check-dependencies

这将报告目标系统上是否缺少任何依赖模块。

4

3 回答 3

5

Python 打包中的依赖关系是一个令人困惑的主题。长期以来,唯一的标准是 PEP 314,它定义了函数的requires,providesobsoletes参数distutils.core.setup。用于这些参数的元素是 Python 模块名称,例如provides=['xml', 'xml.utils']. PEP 对标准库依赖项不是很清楚(我必须依赖 Python >= 2.5 还是必须要求'xml'?),事实证明,没有使用这些字段的工具(甚至 distutils 本身)。

然后是setuptools。它引入了使用项目名称而不是模块的其他类型的依赖项,例如,您可以拥有setup(..., install_requires=['PyXML', 'Pylons'], tests_require=['nose']),这非常有用:人们使用唯一的项目名称在 PyPI 上发布软件,您可以在安装脚本中使用这些相同的名称来依赖在它们上,使用 easy_install 或 pip 可以安装这些依赖项、模块、脚本和所有内容。

几年前,当 distutils 再次掌权时,社区标准化了 setuptools 的一些依赖关系概念以产生 PEP 345,现在在 distutils2 中实现,旨在取代 distutils 和 setuptools。

To sum it all up: - you may have distutils-style module-level dependencies in your setup script, which are useless - you may have setuptools-style project-level deps, which are used by setuptools-based tools - you can have PEP 345-compliant projec-level deps in a setup.cfg file, which are used by distutils2

So, to let us answer your question, you need to tell us which kind you have. For all practical matters, distutils-style module deps should not be used, so it leaves setuptools project deps or the new PEP 345-style ones, which are still new and not widespread yet. distutils2 has a compat layer for setuptools, so it can be possible to use it to get the info you want out of a setuptools-based setup.py script.

Unrelated to packaging tools, there is also a tool that can scan your code to find the modules you’re using: it’s the modulefinder module, in the standard library, which is not very known or used, judging by the sad state of its code. This tool won’t tell you if a module used comes from the stdlib or a third-party project, and it can’t tell you the project name to use in your setup.py or setup.cfg file.

HTH

于 2011-12-07T16:41:16.730 回答
4

我认为你能得到的最接近的是:

setup.py install -v -n

-n这意味着以详细 ( -v) 模式运行空运行 ( )。

您也可以使用distuitls.dep_util模块,但它不能作为一个选项工作setup.py

于 2011-12-06T19:25:19.040 回答
0

distutils.dep_util 关注的是文件依赖(即如果 somefile.c 比 somefile.o 更新,那么 somefile.o 需要重新编译),而不是项目依赖。[想将此添加为对其他回复的评论,但显然添加评论按钮添加了答案?]

于 2011-12-07T16:24:02.883 回答