如何在 Python 2.7 中使用“setup.cfg”而不是 setup.py

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27077355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:24:29  来源:igfitidea点击:

How to use "setup.cfg" instead of setup.py with Python 2.7

pythonpython-2.7installation

提问by Don Question

It seemed to me, that instead of the whole plethora of named keyword-arguments for the distutils.core.setupfunction, one can use a setup.cfgfile in the same directory as the setup.pyfile and all these keywords will be read from the setup.cfg file.

在我看来,不是distutils.core.setup函数的大量命名关键字参数,可以使用与setup.cfg文件相同目录中的setup.py文件,所有这些关键字都将从 setup.cfg 文件中读取。

I thought i could simply create a setup.cfgwith Python 2.7, but a minimal testing version just does not work. I did test it with setup.py --name, which just returns: UNKNOWN.

我以为我可以简单地setup.cfg使用 Python 2.7创建一个,但是最小的测试版本不起作用。我确实用 对其进行了测试,它只setup.py --name返回:UNKNOWN

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.

和往常一样,python 打包文档令人困惑,因为它永远不清楚,它们与哪个版本相关,或者至少文档有多旧。

My two setup files:

我的两个安装文件:

setup.py:

设置.py:

from distutils.core import setup
setup()

setup.cfg:

设置.cfg:

[metadata]
name = foo
version = 0.1

I looked into the distutils package and found that (besides being fugly as hell) it seems to use the mail.message_from_filefactory to read the setup.cfg.

我查看了 distutils 包,发现(除了令人毛骨悚然)它似乎使用mail.message_from_file工厂读取setup.cfg.

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.

因为我对setup.py-only 方法很满意,所以我不会再为这种废话费心了,但我仍然很好奇如何正确地做到这一点,如果可能的话。

Neither the official packaging docnor the Packaging-Authorityseems to be a big help here.

无论是官方的包装文档还是包装,当局似乎是一个很大的帮助在这里。



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.

几乎每次我都觉得有必要查看 python 的 2.x stdlib 时,我想知道他们是否试图展示如何不编程。另一方面,C 代码看起来很漂亮。

采纳答案by D.Shawley

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.

请注意,从 2016 年 12 月和 setuptools 版本 30.3.0 开始,可以根据idle sign's answer将包元数据放在 setup.cfg 中。



The problem is that the setup.cfgfile does not do what you want. It does not provide parameters to the setupfunction. It is used to supply parameters to the commands that setup.pymakes available. You can list the supported commands with setup.py --help-commands. You should see something like:

问题是setup.cfg文件没有做你想要的。它不向setup函数提供参数。它用于为setup.py提供的命令提供参数。您可以使用setup.py --help-commands列出支持的命令。你应该看到类似的东西:

(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.)

This is the list of sections that you can put in a setup.cfgfile. You can list the options that a command supports using setup.py --help command. For example, the sdistcommand supports the following options:

这是您可以放入setup.cfg文件的部分列表。您可以使用setup.py --help command列出命令支持的选项。例如,sdist命令支持以下选项:

(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

You can control what happens when a user runs ./setup.py sdistin your project by adding a setup.cfgfile like the following.

您可以通过添加如下所示的setup.cfg文件来控制当用户在您的项目中运行./setup.py sdist时会发生什么。

[sdist]
keep-temp = 1
dist-dir = dist/source

So... setup.cfgsimply configures the behavior of the various setup commands for your project. The setupfunction really needs to have the metadata supplied to it as keyword parameters. You could write your own version of the distutils.dist.Distributionclass that pulls metadata from setup.cfgand provide it as the distclass=keyword parameter to setup.

所以... setup.cfg只是为您的项目配置各种设置命令的行为。该setup函数确实需要将元数据作为关键字参数提供给它。您可以编写自己的distutils.dist.Distribution类版本,从setup.cfg中提取元数据并将其作为distclass=关键字参数提供给setup

The missing piece to the puzzle is that the standard Distributionclass does not provide a way to pass the pathparameter to the distutils.dist.DistributionMetadatainitializer 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-345metadata file. This is not used by the setupfunction. Instead, it is used to parse the metadata embedded in a distributed package.

拼图的缺失部分是标准Distribution类没有提供将path参数传递给distutils.dist.DistributionMetadata初始化程序的方法,这几乎可以满足您的要求 - 它使用您提到的电子邮件解析内容读取包信息。您找到的是用于处理PEP-314/ PEP-345元数据文件的代码。setup函数不使用它。相反,它用于解析嵌入在分布式包中的元数据。

回答by Don Question

Disclaimer: The following remarks are true for python 2.7with the stdlib distutilspackage. They may be misleading or false for other versions.

免责声明:以下评论适用于带有stdlib包的python 2.7对于其他版本,它们可能具有误导性或虚假性distutils

Independently to D. Shawleys very elaborate and sophisticated answeri came to pretty the same conclusions and decided to do a trivial solutionfor declarative metadatas.

独立于 D. Shawleys 非常详尽和复杂的答案,我得出了几乎相同的结论,并决定为声明性元数据做一个简单的解决方案

So for the sake of everyone, who got stuck with the same question and does not want to loose to much time on such trivialities here is a short summary for setup.cfgissues:

因此,为了大家,谁被同样的问题困住了,不想在这些琐事上浪费太多时间,这里是setup.cfg问题的简短摘要:

  • setup()use of setup.cfgdoes not allow to declare name, version, license and similar meta-data for all provided commands, in a general way
  • you must use the keyword-arguments of setup()to define those meta-datas
  • all sections of setup.cfgare named after commands and change the behaviour of the setup()function see D.Shawleys answer
  • you could define the metadatas in a very redundant way for every command individually
  • you are free to use a custom section-name for custom commands. You can use this possibility to implement your own solution, like i did.
  • setup()使用setup.cfg不允许以一般方式声明所有提供的命令的名称、版本、许可证和类似的元数据
  • 您必须使用的关键字参数setup()来定义这些元数据
  • 的所有部分setup.cfg都以命令命名并更改setup()函数的行为,请参阅 D.Shawleys 的回答
  • 您可以以非常冗余的方式为每个命令单独定义元数据
  • 您可以自由地为自定义命令使用自定义部分名称。您可以像我一样使用这种可能性来实现您自己的解决方案。

So with these findings in mind i implemented a Q&D? solution which allows a setup.cfg-section like:

因此,考虑到这些发现,我实施了Q&D?允许setup.cfg-section 的解决方案,如:

[metadata]
name = foo
version = 0.1

and a setup.py with:

和一个 setup.py :

distutils.core.setup(**metadata_from_setupcfg())

Basically i took the distutils2-solution as orientation and avoided the need to do an additional import, with the additional path-hassle in the setup.pyscript, by using execfile.

基本上,我将 distutils2-solution 作为方向,并setup.py通过使用execfile.

There is an exemplary gistfor the interested available.

对于感兴趣的人,有一个示例性的要点

回答by idle sign

I'm glad to say that since setuptools 30.3.0(8 Dec 2016) declarative configuration in setup.cfgis available.

我很高兴地说,由于setuptools 30.3.0(2016 年 12 月 8 日)在setup.cfg 中的声明性配置可用

Documentation.

文档