“Python/docs/3.9/distutils/configfile”的版本间差异

来自菜鸟教程
Python/docs/3.9/distutils/configfile
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:3. 编写设置配置文件 — Python 文档}}
 
<div id="writing-the-setup-configuration-file" class="section">
 
<div id="writing-the-setup-configuration-file" class="section">
  
 
<span id="setup-config"></span>
 
<span id="setup-config"></span>
= <span class="section-number">3. </span>Writing the Setup Configuration File =
+
= 3. 编写设置配置文件 =
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
This document is being retained solely until the <code>setuptools</code> documentation
+
本文档仅保留到 https://setuptools.readthedocs.io/en/latest/setuptools.html 上的 <code>setuptools</code> 文档独立涵盖当前包含在此处的所有相关信息之前。
at https://setuptools.readthedocs.io/en/latest/setuptools.html
 
independently covers all of the relevant information currently included here.
 
  
  
 
</div>
 
</div>
Often, it's not possible to write down everything needed to build a distribution
+
通常,不可能写下构建发行版 ''先验'' 所需的所有内容:您可能需要从用户或用户系统获取一些信息,以便继续。 只要该信息相当简单——例如用于搜索 C 头文件或库的目录列表——然后提供一个配置文件 <code>setup.cfg</code> 供用户编辑是一种廉价且简单的请求方式它。 配置文件还允许您为任何命令选项提供默认值,然后安装程序可以在命令行上或通过编辑配置文件来覆盖这些默认值。
''a priori'': you may need to get some information from the user, or from the
 
user's system, in order to proceed. As long as that information is fairly
 
simple---a list of directories to search for C header files or libraries, for
 
example---then providing a configuration file, <code>setup.cfg</code>, for users to
 
edit is a cheap and easy way to solicit it. Configuration files also let you
 
provide default values for any command option, which the installer can then
 
override either on the command-line or by editing the config file.
 
  
The setup configuration file is a useful middle-ground between the setup
+
安装配置文件是安装脚本(理想情况下对安装程序 [[#id2|1]] 不透明)和安装脚本的命令行之间的有用中间地带,该命令行不受您的控制且完全由安装人员决定。 事实上,<code>setup.cfg</code>(以及目标系统上存在的任何其他 Distutils 配置文件)是在安装脚本的内容之后,但在命令行之前处理的。 这有几个有用的结果:
script---which, ideally, would be opaque to installers [[#id2|1]]---and the command-line to
 
the setup script, which is outside of your control and entirely up to the
 
installer. In fact, <code>setup.cfg</code> (and any other Distutils configuration
 
files present on the target system) are processed after the contents of the
 
setup script, but before the command-line. This has several useful
 
consequences:
 
  
* installers can override some of what you put in <code>setup.py</code> by editing <code>setup.cfg</code>
+
* 安装人员可以通过编辑 <code>setup.cfg</code> 来覆盖您在 <code>setup.py</code> 中放置的一些内容
* you can provide non-standard defaults for options that are not easily set in <code>setup.py</code>
+
* 您可以为 <code>setup.py</code> 中不容易设置的选项提供非标准默认值
* installers can override anything in <code>setup.cfg</code> using the command-line options to <code>setup.py</code>
+
* 安装程序可以使用 <code>setup.py</code> 的命令行选项覆盖 <code>setup.cfg</code> 中的任何内容
  
The basic syntax of the configuration file is simple:
+
配置文件的基本语法很简单:
  
 
<div class="highlight-ini notranslate">
 
<div class="highlight-ini notranslate">
第41行: 第27行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>[command]
+
<syntaxhighlight lang="ini">[command]
 
option=value
 
option=value
...</pre>
+
...</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
where ''command'' is one of the Distutils commands (e.g. '''build_py''',
+
其中 ''command'' Distutils 命令之一(例如 '''build_py''''''install''')''option''是command支持的选项之一。 可以为每个命令提供任意数量的选项,并且文件中可以包含任意数量的命令部分。 空行被忽略,注释也被忽略,它们从 <code>'#'</code> 字符开始直到行尾。 只需缩进连续行,就可以将长选项值拆分为多行。
'''install'''), and ''option'' is one of the options that command supports.
 
Any number of options can be supplied for each command, and any number of
 
command sections can be included in the file. Blank lines are ignored, as are
 
comments, which run from a <code>'#'</code> character until the end of the line. Long
 
option values can be split across multiple lines simply by indenting the
 
continuation lines.
 
  
You can find out the list of options supported by a particular command with the
+
您可以使用通用 <code>--help</code> 选项找出特定命令支持的选项列表,例如
universal <code>--help</code> option, e.g.
 
  
 
<div class="highlight-shell-session notranslate">
 
<div class="highlight-shell-session notranslate">
第63行: 第42行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>$ python setup.py --help build_ext
+
<pre class="session">$ python setup.py --help build_ext
 
[...]
 
[...]
 
Options for 'build_ext' command:
 
Options for 'build_ext' command:
第79行: 第58行:
  
 
</div>
 
</div>
Note that an option spelled <code>--foo-bar</code> on the command-line is spelled
+
请注意,命令行上拼写为 <code>--foo-bar</code> 的选项在配置文件中拼写为 <code>foo_bar</code>
<code>foo_bar</code> in configuration files.
 
  
For example, say you want your extensions to be built &quot;in-place&quot;---that is, you
+
例如,假设您希望“就地”构建扩展——也就是说,您有一个扩展 <code>pkg.ext</code>,并且您想要编译后的扩展文件(例如,Unix 上的 <code>ext.so</code>)与纯 Python 模块 <code>pkg.mod1</code> <code>pkg.mod2</code> 放在同一源目录中。 您始终可以在命令行上使用 <code>--inplace</code> 选项来确保这一点:
have an extension <code>pkg.ext</code>, and you want the compiled extension file
 
(<code>ext.so</code> on Unix, say) to be put in the same source directory as your
 
pure Python modules <code>pkg.mod1</code> and <code>pkg.mod2</code>. You can always use the
 
<code>--inplace</code> option on the command-line to ensure this:
 
  
 
<div class="highlight-sh notranslate">
 
<div class="highlight-sh notranslate">
第92行: 第66行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py build_ext --inplace</pre>
+
<syntaxhighlight lang="sh">python setup.py build_ext --inplace</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
But this requires that you always specify the '''build_ext''' command
+
但这需要你始终明确指定 '''build_ext''' 命令,并记得提供 <code>--inplace</code>。 一个更简单的方法是“设置并忘记”这个选项,通过在 <code>setup.cfg</code> 中编码,这个发行版的配置文件:
explicitly, and remember to provide <code>--inplace</code>. An easier way is to
 
&quot;set and forget&quot; this option, by encoding it in <code>setup.cfg</code>, the
 
configuration file for this distribution:
 
  
 
<div class="highlight-ini notranslate">
 
<div class="highlight-ini notranslate">
第106行: 第77行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>[build_ext]
+
<syntaxhighlight lang="ini">[build_ext]
inplace=1</pre>
+
inplace=1</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
This will affect all builds of this module distribution, whether or not you
+
这将影响此模块分发的所有构建,无论您是否明确指定 '''build_ext'''。 如果你在你的源代码分发中包含 <code>setup.cfg</code>,它也会影响最终用户的构建——这对于这个选项来说可能是个坏主意,因为总是就地构建扩展会破坏模块分发的安装。 但是,在某些特殊情况下,模块直接构建在其安装目录中,因此可以想象这是一个有用的功能。 (不过,分发期望在其安装目录中构建的扩展几乎总是一个坏主意。)
explicitly specify '''build_ext'''. If you include <code>setup.cfg</code> in
 
your source distribution, it will also affect end-user builds---which is
 
probably a bad idea for this option, since always building extensions in-place
 
would break installation of the module distribution. In certain peculiar cases,
 
though, modules are built right in their installation directory, so this is
 
conceivably a useful ability. (Distributing extensions that expect to be built
 
in their installation directory is almost always a bad idea, though.)
 
  
Another example: certain commands take a lot of options that don't change from
+
另一个例子:某些命令有很多选项,每次运行都不会改变; 例如,'''bdist_rpm''' 需要知道生成用于创建 RPM 发行版的“spec”文件所需的一切。 这些信息有些来自安装脚本,有些是由 Distutils 自动生成的(例如安装的文件列表)。 但是其中一些必须作为 '''bdist_rpm''' 的选项提供,这对于每次运行在命令行上执行都会非常乏味。 因此,这里是 Distutils 自己的 <code>setup.cfg</code> 的片段:
run to run; for example, '''bdist_rpm''' needs to know everything required
 
to generate a &quot;spec&quot; file for creating an RPM distribution. Some of this
 
information comes from the setup script, and some is automatically generated by
 
the Distutils (such as the list of files installed). But some of it has to be
 
supplied as options to '''bdist_rpm''', which would be very tedious to do
 
on the command-line for every run. Hence, here is a snippet from the Distutils'
 
own <code>setup.cfg</code>:
 
  
 
<div class="highlight-ini notranslate">
 
<div class="highlight-ini notranslate">
第134行: 第91行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>[bdist_rpm]
+
<syntaxhighlight lang="ini">[bdist_rpm]
 
release = 1
 
release = 1
packager = Greg Ward &lt;gward@python.net&gt;
+
packager = Greg Ward <gward@python.net>
 
doc_files = CHANGES.txt
 
doc_files = CHANGES.txt
 
             README.txt
 
             README.txt
 
             USAGE.txt
 
             USAGE.txt
 
             doc/
 
             doc/
             examples/</pre>
+
             examples/</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Note that the <code>doc_files</code> option is simply a whitespace-separated string
+
请注意, <code>doc_files</code> 选项只是一个以空格分隔的字符串,分为多行以提高可读性。
split across multiple lines for readability.
 
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
; [[../../install/index#inst-config-syntax|<span class="std std-ref">Syntax of config files</span>]] in &quot;Installing Python Modules&quot;
+
; [[../../install/index#inst-config-syntax|“安装Python模块”中配置文件的语法]]
: More information on the configuration files is available in the manual for system administrators.
+
: 系统管理员手册中提供了有关配置文件的更多信息。
  
  
 
</div>
 
</div>
Footnotes
+
脚注
  
 
; <span class="brackets">[[#id1|1]]</span>
 
; <span class="brackets">[[#id1|1]]</span>
: This ideal probably won't be achieved until auto-configuration is fully supported by the Distutils.
+
: Distutils 完全支持自动配置之前,这个理想可能不会实现。
  
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Python 3.9 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

3. 编写设置配置文件

笔记

本文档仅保留到 https://setuptools.readthedocs.io/en/latest/setuptools.html 上的 setuptools 文档独立涵盖当前包含在此处的所有相关信息之前。


通常,不可能写下构建发行版 先验 所需的所有内容:您可能需要从用户或用户系统获取一些信息,以便继续。 只要该信息相当简单——例如用于搜索 C 头文件或库的目录列表——然后提供一个配置文件 setup.cfg 供用户编辑是一种廉价且简单的请求方式它。 配置文件还允许您为任何命令选项提供默认值,然后安装程序可以在命令行上或通过编辑配置文件来覆盖这些默认值。

安装配置文件是安装脚本(理想情况下对安装程序 1 不透明)和安装脚本的命令行之间的有用中间地带,该命令行不受您的控制且完全由安装人员决定。 事实上,setup.cfg(以及目标系统上存在的任何其他 Distutils 配置文件)是在安装脚本的内容之后,但在命令行之前处理的。 这有几个有用的结果:

  • 安装人员可以通过编辑 setup.cfg 来覆盖您在 setup.py 中放置的一些内容
  • 您可以为 setup.py 中不容易设置的选项提供非标准默认值
  • 安装程序可以使用 setup.py 的命令行选项覆盖 setup.cfg 中的任何内容

配置文件的基本语法很简单:

[command]
option=value
...

其中 command 是 Distutils 命令之一(例如 build_pyinstall)和option是command支持的选项之一。 可以为每个命令提供任意数量的选项,并且文件中可以包含任意数量的命令部分。 空行被忽略,注释也被忽略,它们从 '#' 字符开始直到行尾。 只需缩进连续行,就可以将长选项值拆分为多行。

您可以使用通用 --help 选项找出特定命令支持的选项列表,例如

$ python setup.py --help build_ext
[...]
Options for 'build_ext' command:
  --build-lib (-b)     directory for compiled extension modules
  --build-temp (-t)    directory for temporary files (build by-products)
  --inplace (-i)       ignore build-lib and put compiled extensions into the
                       source directory alongside your pure Python modules
  --include-dirs (-I)  list of directories to search for header files
  --define (-D)        C preprocessor macros to define
  --undef (-U)         C preprocessor macros to undefine
  --swig-opts          list of SWIG command line options
[...]

请注意,命令行上拼写为 --foo-bar 的选项在配置文件中拼写为 foo_bar

例如,假设您希望“就地”构建扩展——也就是说,您有一个扩展 pkg.ext,并且您想要编译后的扩展文件(例如,Unix 上的 ext.so)与纯 Python 模块 pkg.mod1pkg.mod2 放在同一源目录中。 您始终可以在命令行上使用 --inplace 选项来确保这一点:

python setup.py build_ext --inplace

但这需要你始终明确指定 build_ext 命令,并记得提供 --inplace。 一个更简单的方法是“设置并忘记”这个选项,通过在 setup.cfg 中编码,这个发行版的配置文件:

[build_ext]
inplace=1

这将影响此模块分发的所有构建,无论您是否明确指定 build_ext。 如果你在你的源代码分发中包含 setup.cfg,它也会影响最终用户的构建——这对于这个选项来说可能是个坏主意,因为总是就地构建扩展会破坏模块分发的安装。 但是,在某些特殊情况下,模块直接构建在其安装目录中,因此可以想象这是一个有用的功能。 (不过,分发期望在其安装目录中构建的扩展几乎总是一个坏主意。)

另一个例子:某些命令有很多选项,每次运行都不会改变; 例如,bdist_rpm 需要知道生成用于创建 RPM 发行版的“spec”文件所需的一切。 这些信息有些来自安装脚本,有些是由 Distutils 自动生成的(例如安装的文件列表)。 但是其中一些必须作为 bdist_rpm 的选项提供,这对于每次运行在命令行上执行都会非常乏味。 因此,这里是 Distutils 自己的 setup.cfg 的片段:

[bdist_rpm]
release = 1
packager = Greg Ward <gward@python.net>
doc_files = CHANGES.txt
            README.txt
            USAGE.txt
            doc/
            examples/

请注意, doc_files 选项只是一个以空格分隔的字符串,分为多行以提高可读性。

也可以看看

“安装Python模块”中配置文件的语法
系统管理员手册中提供了有关配置文件的更多信息。


脚注

1
在 Distutils 完全支持自动配置之前,这个理想可能不会实现。