如何使用"setup.cfg"而不是Python 2.7的setup.py [英] How to use "setup.cfg" instead of setup.py with Python 2.7
问题描述
在我看来,不是整个
distutils.core.setup
函数,可以在同一目录中使用setup.cfg
文件
作为setup.py
文件,所有这些关键字都将从setup.cfg文件中读取.
It seemed to me, that instead of the whole plethora of named keyword-arguments for
the distutils.core.setup
function, one can use a setup.cfg
file in the same directory
as the setup.py
file and all these keywords will be read from the setup.cfg file.
我以为我可以使用Python 2.7轻松创建一个setup.cfg
,但需要进行最少的测试
版本就是行不通的.我确实用setup.py --name
测试了
只会返回:UNKNOWN
.
I thought i could simply create a setup.cfg
with Python 2.7, but a minimal testing
version just does not work. I did test it with setup.py --name
, which
just returns: UNKNOWN
.
和往常一样,使用python-packaging编写的文档令人困惑, 至今还不清楚,它们与哪个版本有关,或者至少与 文档是.
And as usual with python-packaging the documentation is confusing as hell, as it is never clear, which version they relate to or at least how old the documentation is.
我的两个安装文件:
setup.py:
from distutils.core import setup
setup()
setup.cfg:
setup.cfg:
[metadata]
name = foo
version = 0.1
我调查了distutils包,发现(除了像地狱一样丑陋)
似乎使用mail.message_from_file
工厂读取
I looked into the distutils package and found that (besides being fugly as hell)
it seems to use the mail.message_from_file
factory to read the setup.cfg
.
由于我只使用setup.py
的方法就可以了,所以我不会再花太多时间了
这么胡说八道,但是我仍然很好奇如何正确地做,如果有可能的话.
As i am quite ok with a setup.py
-only approach i would not bother much longer
with such nonsense, but i am still curious how to do it right, if it is possible at all.
官方包装文档和 Packaging-Authority 似乎对这里有很大帮助.
Neither the official packaging doc nor the Packaging-Authority seems to be a big help here.
几乎每当我感到被迫研究python 2.x stdlib时,我都想知道它们是否 尝试展示如何不编程.另一方面,C代码看起来很漂亮.
Almost every time i feel compelled to look into python's 2.x stdlib i am wondering if they try to showcase how not to program. On the other hand the C-Code seems quite beautiful.
推荐答案
请注意,自2016年12月开始,在setuptools版本30.3.0中,可以根据
Note that, as of December 2016 and setuptools version 30.3.0, it is possible to put package metadata in setup.cfg, per idle sign's answer.
问题在于 setup.cfg 文件没有执行您想要的操作.它不为setup
函数提供参数.它用于为 setup.py 可用的命令提供参数.您可以使用 setup.py --help-commands 列出受支持的命令.您应该会看到类似
The problem is that the setup.cfg file does not do what you want. It does not provide parameters to the setup
function. It is used to supply parameters to the commands that setup.py makes available. You can list the supported commands with setup.py --help-commands. You should see something like:
(env) gondolin/zender% ./setup.py --help-commands
Standard commands:
build build everything needed to install
build_py "build" pure Python modules (copy to build directory)
.....
install_data install data files
sdist create a source distribution (tarball, zip file, etc.)
这是您可以放入 setup.cfg 文件中的部分的列表.您可以使用 setup.py --help command 列出命令支持的选项.例如, sdist 命令支持以下选项:
This is the list of sections that you can put in a setup.cfg file. You can list the options that a command supports using setup.py --help command. For example, the sdist command supports the following options:
(env) gondolin/zender% ./setup.py --help sdist
Common commands: (see '--help-commands' for more)
....
Options for 'sdist' command:
--formats formats for source distribution (comma-separated list)
--keep-temp (-k) keep the distribution tree around after creating archive
file(s)
--dist-dir (-d) directory to put the source distribution archive(s) in
[default: dist]
--help-formats list available distribution formats
通过添加如下所示的 setup.cfg 文件,您可以控制用户在项目中运行 ./setup.py sdist 时发生的情况.
You can control what happens when a user runs ./setup.py sdist in your project by adding a setup.cfg file like the following.
[sdist]
keep-temp = 1
dist-dir = dist/source
所以... setup.cfg 仅为您的项目配置各种设置命令的行为. setup
函数确实需要将元数据作为关键字参数提供给它.您可以编写自己的distutils.dist.Distribution
类版本,该类从 setup.cfg 中提取元数据,并将其作为distclass=
关键字参数提供给setup
.
So... setup.cfg simply configures the behavior of the various setup commands for your project. The setup
function really needs to have the metadata supplied to it as keyword parameters. You could write your own version of the distutils.dist.Distribution
class that pulls metadata from setup.cfg and provide it as the distclass=
keyword parameter to setup
.
难题的缺失部分是标准的Distribution
类没有提供将path
参数传递给distutils.dist.DistributionMetadata
初始化程序的方法,该方法几乎完成了您想要的操作-它使用电子邮件解析您提到的内容.您找到的是用于处理 PEP-314 /的代码 PEP-345 元数据文件. setup
功能不使用此功能.相反,它用于解析嵌入在分布式程序包中的元数据.
The missing piece to the puzzle is that the standard Distribution
class does not provide a way to pass the path
parameter to the distutils.dist.DistributionMetadata
initializer which does pretty much what you want - it reads the package information using the email parsing stuff that you mentioned. What you found is the code that is used to process a PEP-314/PEP-345 metadata file. This is not used by the setup
function. Instead, it is used to parse the metadata embedded in a distributed package.
这篇关于如何使用"setup.cfg"而不是Python 2.7的setup.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!