“Python/docs/3.9/install/index”的版本间差异

来自菜鸟教程
Python/docs/3.9/install/index
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:安装 Python 模块(旧版) — Python 文档}}
 
<div id="installing-python-modules-legacy-version" class="section">
 
<div id="installing-python-modules-legacy-version" class="section">
  
 
<span id="install-index"></span>
 
<span id="install-index"></span>
= Installing Python Modules (Legacy version) =
+
= 安装 Python 模块(旧版) =
  
; Author
+
; 作者
: Greg Ward
+
: 格雷格·沃德
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
; [[../../installing/index#installing-index|<span class="std std-ref">Installing Python Modules</span>]]
+
; [[../../installing/index#installing-index|<span class="std std-ref">安装 Python 模块</span>]]
: The up to date module installation documentation. For regular Python usage, you almost certainly want that document rather than this one.
+
: 最新的模块安装文档。 对于常规 Python 使用,您几乎肯定需要该文档而不是这个文档。
  
  
第18行: 第19行:
 
<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.
 
  
  
第28行: 第27行:
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
This guide only covers the basic tools for building and distributing
+
本指南仅涵盖作为此版本 Python 的一部分提供的用于构建和分发扩展的基本工具。 第三方工具提供更易于使用和更安全的替代方案。 有关更多信息,请参阅 Python Packaging User Guide 中的 [https://packaging.python.org/guides/tool-recommendations/ 快速建议部分]
extensions that are provided as part of this version of Python. Third party
 
tools offer easier to use and more secure alternatives. Refer to the [https://packaging.python.org/guides/tool-recommendations/ quick
 
recommendations section]
 
in the Python Packaging User Guide for more information.
 
  
  
第41行: 第36行:
  
 
<span id="inst-intro"></span>
 
<span id="inst-intro"></span>
== Introduction ==
+
== 介绍 ==
  
In Python 2.0, the <code>distutils</code> API was first added to the standard library.
+
Python 2.0 中,<code>distutils</code> API 首先被添加到标准库中。 这为 Linux 发行版维护人员提供了将 Python 项目转换为 Linux 发行版包的标准方法,并为系统管理员提供了将它们直接安装到目标系统的标准方法。
This provided Linux distro maintainers with a standard way of converting
 
Python projects into Linux distro packages, and system administrators with a
 
standard way of installing them directly onto target systems.
 
  
In the many years since Python 2.0 was released, tightly coupling the build
+
Python 2.0 发布多年以来,构建系统和包安装程序与语言运行时发布周期的紧密耦合已经证明是有问题的,现在建议项目使用 <code>pip</code> 包安装程序和<code>setuptools</code>构建系统,而不是直接使用<code>distutils</code>
system and package installer to the language runtime release cycle has turned
 
out to be problematic, and it is now recommended that projects use the
 
<code>pip</code> package installer and the <code>setuptools</code> build system, rather than
 
using <code>distutils</code> directly.
 
  
See [[../../installing/index#installing-index|<span class="std std-ref">Installing Python Modules</span>]] and [[../../distributing/index#distributing-index|<span class="std std-ref">Distributing Python Modules</span>]] for more details.
+
有关更多详细信息,请参阅 [[../../installing/index#installing-index|安装 Python 模块]] [[../../distributing/index#distributing-index|分发 Python 模块]]
  
This legacy documentation is being retained only until we're confident that the
+
仅在我们确信 <code>setuptools</code> 文档涵盖所需的所有内容之前,才会保留此旧文档。
<code>setuptools</code> documentation covers everything needed.
 
  
 
<div id="distutils-based-source-distributions" class="section">
 
<div id="distutils-based-source-distributions" class="section">
  
 
<span id="inst-new-standard"></span>
 
<span id="inst-new-standard"></span>
=== Distutils based source distributions ===
+
=== 基于 Distutils 的源代码分发 ===
  
If you download a module source distribution, you can tell pretty quickly if it
+
如果你下载一个模块源代码分发,你可以很快知道它是否以标准方式打包和分发,即 使用 Distutils。 首先,发行版的名称和版本号将在下载的存档名称中突出显示,例如 <code>foo-1.0.tar.gz</code> <code>widget-0.9.7.zip</code>。 接下来,存档将解压到一个类似名称的目录中:<code>foo-1.0</code> <code>widget-0.9.7</code>。 此外,该发行版将包含一个安装脚本 <code>setup.py</code> 和一个名为 <code>README.txt</code> 或可能只是 <code>README</code> 的文件,这应该说明构建和安装模块发行版是一件简单的事情从终端运行一个命令:
was packaged and distributed in the standard way, i.e. using the Distutils.
 
First, the distribution's name and version number will be featured prominently
 
in the name of the downloaded archive, e.g. <code>foo-1.0.tar.gz</code> or
 
<code>widget-0.9.7.zip</code>. Next, the archive will unpack into a similarly-named
 
directory: <code>foo-1.0</code> or <code>widget-0.9.7</code>. Additionally, the
 
distribution will contain a setup script <code>setup.py</code>, and a file named
 
<code>README.txt</code> or possibly just <code>README</code>, which should explain that
 
building and installing the module distribution is a simple matter of running
 
one command from a terminal:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第79行: 第57行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install</pre>
+
<pre class="none">python setup.py install</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
For Windows, this command should be run from a command prompt window
+
对于 Windows,此命令应从命令提示符窗口 (<span class="menuselection">Start ‣ Accessories</span>) 运行:
(<span class="menuselection">Start ‣ Accessories</span>):
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第91行: 第68行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>setup.py install</pre>
+
<pre class="none">setup.py install</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
If all these things are true, then you already know how to build and install the
+
如果所有这些都是真的,那么您已经知道如何构建和安装刚刚下载的模块: 运行上面的命令。 除非您需要以非标准方式安装东西或自定义构建过程,否则您实际上并不需要这本手册。 或者更确切地说,上面的命令是您离开本手册所需的一切。
modules you've just downloaded: Run the command above. Unless you need to
 
install things in a non-standard way or customize the build process, you don't
 
really need this manual. Or rather, the above command is everything you need to
 
get out of this manual.
 
  
  
第109行: 第82行:
  
 
<span id="inst-standard-install"></span>
 
<span id="inst-standard-install"></span>
== Standard Build and Install ==
+
== 标准构建和安装 ==
  
As described in section [[#inst-new-standard|<span class="std std-ref">Distutils based source distributions</span>]], building and installing a module
+
[[#inst-new-standard|基于Distutils的源代码分发]]部分所述,使用Distutils构建和安装模块分发通常是从终端运行的一个简单命令:
distribution using the Distutils is usually one simple command to run from a
 
terminal:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第119行: 第90行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install</pre>
+
<pre class="none">python setup.py install</pre>
  
 
</div>
 
</div>
第127行: 第98行:
  
 
<span id="inst-platform-variations"></span>
 
<span id="inst-platform-variations"></span>
=== Platform variations ===
+
=== 平台变化 ===
  
You should always run the setup command from the distribution root directory,
+
您应该始终从发行版根目录运行 setup 命令,即 模块源分发解包到的顶级子目录。 例如,如果您刚刚将模块源代码分发 <code>foo-1.0.tar.gz</code> 下载到 Unix 系统上,通常要做的事情是:
i.e. the top-level subdirectory that the module source distribution unpacks
 
into. For example, if you've just downloaded a module source distribution
 
<code>foo-1.0.tar.gz</code> onto a Unix system, the normal thing to do is:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第138行: 第106行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>gunzip -c foo-1.0.tar.gz | tar xf -    # unpacks into directory foo-1.0
+
<pre class="none">gunzip -c foo-1.0.tar.gz | tar xf -    # unpacks into directory foo-1.0
 
cd foo-1.0
 
cd foo-1.0
 
python setup.py install</pre>
 
python setup.py install</pre>
第145行: 第113行:
  
 
</div>
 
</div>
On Windows, you'd probably download <code>foo-1.0.zip</code>. If you downloaded the
+
Windows 上,您可能会下载 <code>foo-1.0.zip</code>。 如果你下载了存档文件到<code>C:\Temp</code>,那么它会解压到<code>C:\Temp\foo-1.0</code>; 您可以使用带有图形用户界面的存档操作器(例如 WinZip)或命令行工具(例如 '''unzip''' '''pkunzip''')来解压存档。 然后,打开命令提示符窗口并运行:
archive file to <code>C:\Temp</code>, then it would unpack into
 
<code>C:\Temp\foo-1.0</code>; you can use either an archive manipulator with a
 
graphical user interface (such as WinZip) or a command-line tool (such as
 
'''unzip''' or '''pkunzip''') to unpack the archive. Then, open a
 
command prompt window and run:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第156行: 第119行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>cd c:\Temp\foo-1.0
+
<pre class="none">cd c:\Temp\foo-1.0
 
python setup.py install</pre>
 
python setup.py install</pre>
  
第167行: 第130行:
  
 
<span id="inst-splitting-up"></span>
 
<span id="inst-splitting-up"></span>
=== Splitting the job up ===
+
=== 拆分工作 ===
  
Running <code>setup.py install</code> builds and installs all modules in one run. If you
+
运行 <code>setup.py install</code> 会在一次运行中构建和安装所有模块。 如果您更喜欢渐进式工作(如果您想自定义构建过程或出现问题时尤其有用),您可以使用设置脚本一次只做一件事。 当构建和安装将由不同的用户完成时,这特别有用——例如,您可能想要构建一个模块分发并将其交给系统管理员进行安装(或自己完成,具有超级用户权限)。
prefer to work incrementally---especially useful if you want to customize the
 
build process, or if things are going wrong---you can use the setup script to do
 
one thing at a time. This is particularly helpful when the build and install
 
will be done by different users---for example, you might want to build a module
 
distribution and hand it off to a system administrator for installation (or do
 
it yourself, with super-user privileges).
 
  
For example, you can build everything in one step, and then install everything
+
例如,您可以一步构建所有内容,然后通过调用安装脚本两次,在第二步安装所有内容:
in a second step, by invoking the setup script twice:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第184行: 第140行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py build
+
<pre class="none">python setup.py build
 
python setup.py install</pre>
 
python setup.py install</pre>
  
第190行: 第146行:
  
 
</div>
 
</div>
If you do this, you will notice that running the '''install''' command
+
如果这样做,您会注意到运行 '''install''' 命令首先运行 '''build''' 命令,在这种情况下,它很快就会注意到它无关紧要,因为<code>build</code> 目录是最新的。
first runs the '''build''' command, which---in this case---quickly notices
 
that it has nothing to do, since everything in the <code>build</code> directory is
 
up-to-date.
 
  
You may not need this ability to break things down often if all you do is
+
如果您所做的只是安装从 'net 下载的模块,您可能不需要这种经常分解事物的能力,但它对于更高级的任务非常方便。 如果您开始分发自己的 Python 模块和扩展,您将自行运行许多单独的 Distutils 命令。
install modules downloaded off the 'net, but it's very handy for more advanced
 
tasks. If you get into distributing your own Python modules and extensions,
 
you'll run lots of individual Distutils commands on their own.
 
  
  
第205行: 第155行:
  
 
<span id="inst-how-build-works"></span>
 
<span id="inst-how-build-works"></span>
=== How building works ===
+
=== 建筑如何运作 ===
  
As implied above, the '''build''' command is responsible for putting the
+
如上所述,'''build''' 命令负责将要安装的文件放入 ''构建目录'' 。 默认情况下,这是分布根目录下的 <code>build</code>; 如果您过分关注速度,或者想要保持源代码树的原始状态,您可以使用 <code>--build-base</code> 选项更改构建目录。 例如:
files to install into a ''build directory''. By default, this is <code>build</code>
 
under the distribution root; if you're excessively concerned with speed, or want
 
to keep the source tree pristine, you can change the build directory with the
 
<code>--build-base</code> option. For example:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第217行: 第163行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py build --build-base=/path/to/pybuild/foo-1.0</pre>
+
<pre class="none">python setup.py build --build-base=/path/to/pybuild/foo-1.0</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
(Or you could do this permanently with a directive in your system or personal
+
(或者,您可以使用系统或个人 Distutils 配置文件中的指令永久执行此操作;请参阅部分 [[#inst-config-files|Distutils 配置文件]] 。)通常,这不是必需的。
Distutils configuration file; see section [[#inst-config-files|<span class="std std-ref">Distutils Configuration Files</span>]].) Normally, this
 
isn't necessary.
 
  
The default layout for the build tree is as follows:
+
构建树的默认布局如下:
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第232行: 第176行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>--- build/ --- lib/
+
<pre class="none">--- build/ --- lib/
 
or
 
or
 
--- build/ --- lib.&lt;plat&gt;/
 
--- build/ --- lib.&lt;plat&gt;/
第240行: 第184行:
  
 
</div>
 
</div>
where <code>&lt;plat&gt;</code> expands to a brief description of the current OS/hardware
+
其中 <code>&lt;plat&gt;</code> 扩展为对当前操作系统/硬件平台和 Python 版本的简要描述。 第一种形式,只有一个 <code>lib</code> 目录,用于“纯模块分发”——即只包含纯 Python 模块的模块分发。 如果模块分发包含任何扩展(用 C/C++ 编写的模块),则使用带有两个 <code>&lt;plat&gt;</code> 目录的第二种形式。 在这种情况下,<code>temp.plat</code> 目录包含编译/链接过程生成的临时文件,但实际上并未安装。 在任一情况下,<code>lib</code>(或 <code>lib.plat</code>)目录包含将安装的所有 Python 模块(纯 Python 和扩展)。
platform and Python version. The first form, with just a <code>lib</code> directory,
 
is used for &quot;pure module distributions&quot;---that is, module distributions that
 
include only pure Python modules. If a module distribution contains any
 
extensions (modules written in C/C++), then the second form, with two <code>&lt;plat&gt;</code>
 
directories, is used. In that case, the <code>temp.plat</code> directory holds
 
temporary files generated by the compile/link process that don't actually get
 
installed. In either case, the <code>lib</code> (or <code>lib.plat</code>) directory
 
contains all Python modules (pure Python and extensions) that will be installed.
 
  
In the future, more directories will be added to handle Python scripts,
+
将来,将添加更多目录来处理 Python 脚本、文档、二进制可执行文件以及处理安装 Python 模块和应用程序工作所需的任何其他内容。
documentation, binary executables, and whatever else is needed to handle the job
 
of installing Python modules and applications.
 
  
  
第259行: 第193行:
  
 
<span id="inst-how-install-works"></span>
 
<span id="inst-how-install-works"></span>
=== How installation works ===
+
=== 安装工作原理 ===
  
After the '''build''' command runs (whether you run it explicitly, or the
+
'''build'''命令运行后(不管你是显式运行,还是'''install'''命令帮你搞定),'''install'''命令的工作就比较简单了:它所要做的就是将 <code>build/lib</code>(或 <code>build/lib.plat</code>)下的所有内容复制到您选择的安装目录。
'''install''' command does it for you), the work of the '''install'''
 
command is relatively simple: all it has to do is copy everything under
 
<code>build/lib</code> (or <code>build/lib.plat</code>) to your chosen installation
 
directory.
 
  
If you don't choose an installation directory---i.e., if you just run <code>setup.py install</code>---then the '''install''' command installs to the standard
+
如果你没有选择安装目录——即,如果你只是运行 <code>setup.py install</code>——那么 '''install''' 命令会安装到第三方 Python 模块的标准位置。 此位置因平台以及您构建/安装 Python 本身的方式而异。 在 Unix(以及同样基于 Unix 的 macOS)上,它还取决于所安装的模块发行版是纯 Python 还是包含扩展(“非纯”):
location for third-party Python modules. This location varies by platform and
 
by how you built/installed Python itself. On Unix (and Mac OS X, which is also
 
Unix-based), it also depends on whether the module distribution being installed
 
is pure Python or contains extensions (&quot;non-pure&quot;):
 
  
 
{|
 
{|
!width="13%"| Platform
+
!width="13%"| 平台
!width="42%"| Standard installation location
+
!width="42%"| 标准安装位置
!width="39%"| Default value
+
!width="39%"| 默认值
!width="6%"| Notes
+
!width="6%"| 笔记
 
|-
 
|-
| Unix (pure)
+
| Unix(纯)
 
| <code>prefix/lib/pythonX.Y/site-packages</code>
 
| <code>prefix/lib/pythonX.Y/site-packages</code>
 
| <code>/usr/local/lib/pythonX.Y/site-packages</code>
 
| <code>/usr/local/lib/pythonX.Y/site-packages</code>
 
| (1)
 
| (1)
 
|-
 
|-
| Unix (non-pure)
+
| Unix(非纯)
 
| <code>exec-prefix/lib/pythonX.Y/site-packages</code>
 
| <code>exec-prefix/lib/pythonX.Y/site-packages</code>
 
| <code>/usr/local/lib/pythonX.Y/site-packages</code>
 
| <code>/usr/local/lib/pythonX.Y/site-packages</code>
 
| (1)
 
| (1)
 
|-
 
|-
| Windows
+
| 视窗
 
| <code>prefix\Lib\site-packages</code>
 
| <code>prefix\Lib\site-packages</code>
 
| <code>C:\PythonXY\Lib\site-packages</code>
 
| <code>C:\PythonXY\Lib\site-packages</code>
第295行: 第221行:
 
|}
 
|}
  
Notes:
+
笔记:
  
# Most Linux distributions include Python as a standard part of the system, so <code>prefix</code> and <code>exec-prefix</code> are usually both <code>/usr</code> on Linux. If you build Python yourself on Linux (or any Unix-like system), the default <code>prefix</code> and <code>exec-prefix</code> are <code>/usr/local</code>.
+
# 大多数 Linux 发行版都包含 Python 作为系统的标准部分,因此 <code>prefix</code> <code>exec-prefix</code> 在 Linux 上通常都是 <code>/usr</code>。 如果你自己在 Linux(或任何类 Unix 系统)上构建 Python,默认的 <code>prefix</code> <code>exec-prefix</code> <code>/usr/local</code>
# The default installation directory on Windows was <code>C:\Program Files\Python</code> under Python 1.6a1, 1.5.2, and earlier.
+
# Windows 上的默认安装目录是 <code>C:\Program Files\Python</code> Python 1.6a1、1.5.2 和更早版本下。
  
<code>prefix</code> and <code>exec-prefix</code> stand for the directories that Python
+
<code>prefix</code> <code>exec-prefix</code> 代表 Python 安装到的目录,以及它在运行时找到它的库的位置。 它们在 Windows 下总是相同的,在 Unix 和 macOS 下也经常相同。 通过在交互模式下运行 Python 并键入一些简单的命令,您可以找出 Python 安装用于 <code>prefix</code> <code>exec-prefix</code> 的内容。 在 Unix 下,只需在 shell 提示符下键入 <code>python</code>。 在 Windows 下,选择 <span class="menuselection"> 开始 程序 ‣ Python XY Python(命令行)</span>。 解释器启动后,您可以在提示符下键入 Python 代码。 例如,在我的 Linux 系统上,我键入如下所示的三个 Python 语句,并得到如图所示的输出,以找出我的 <code>prefix</code> <code>exec-prefix</code>
is installed to, and where it finds its libraries at run-time. They are always
 
the same under Windows, and very often the same under Unix and Mac OS X. You
 
can find out what your Python installation uses for <code>prefix</code> and
 
<code>exec-prefix</code> by running Python in interactive mode and typing a few
 
simple commands. Under Unix, just type <code>python</code> at the shell prompt. Under
 
Windows, choose <span class="menuselection">Start Programs ‣ Python X.Y
 
Python (command line)</span>. Once the interpreter is started, you type Python code
 
at the prompt. For example, on my Linux system, I type the three Python
 
statements shown below, and get the output as shown, to find out my
 
<code>prefix</code> and <code>exec-prefix</code>:
 
  
 
<div class="highlight-pycon notranslate">
 
<div class="highlight-pycon notranslate">
第316行: 第232行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>Python 2.4 (#26, Aug  7 2004, 17:19:02)
+
<syntaxhighlight lang="pycon">Python 2.4 (#26, Aug  7 2004, 17:19:02)
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
+
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import sys
+
>>> import sys
&gt;&gt;&gt; sys.prefix
+
>>> sys.prefix
 
'/usr'
 
'/usr'
&gt;&gt;&gt; sys.exec_prefix
+
>>> sys.exec_prefix
'/usr'</pre>
+
'/usr'</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
A few other placeholders are used in this document: <code>X.Y</code> stands for the
+
本文档中使用了其他一些占位符: <code>X.Y</code> 代表 Python 的版本,例如 <code>3.2</code><code>abiflags</code> 将替换为 [[../../library/sys#sys|sys.abiflags]] 的值或未定义 ABI 标志的平台的空字符串; <code>distname</code> 将替换为正在安装的模块发行版的名称。 点和大写在路径中很重要; 例如,在 UNIX 上使用 <code>python3.2</code> 的值通常在 Windows 上使用 <code>Python32</code>
version of Python, for example <code>3.2</code>; <code>abiflags</code> will be replaced by
 
the value of [[../../library/sys#sys|<code>sys.abiflags</code>]] or the empty string for platforms which don't
 
define ABI flags; <code>distname</code> will be replaced by the name of the module
 
distribution being installed. Dots and capitalization are important in the
 
paths; for example, a value that uses <code>python3.2</code> on UNIX will typically use
 
<code>Python32</code> on Windows.
 
  
If you don't want to install modules to the standard location, or if you don't
+
如果您不想将模块安装到标准位置,或者您没有在那里写入的权限,那么您需要阅读 [[#inst-alt-install|备用安装]] 部分中的备用安装。 如果您想更多地自定义您的安装目录,请参阅关于自定义安装的 [[#inst-custom-install|自定义安装]] 部分。
have permission to write there, then you need to read about alternate
 
installations in section [[#inst-alt-install|<span class="std std-ref">Alternate Installation</span>]]. If you want to customize your
 
installation directories more heavily, see section [[#inst-custom-install|<span class="std std-ref">Custom Installation</span>]] on
 
custom installations.
 
  
  
第348行: 第254行:
  
 
<span id="inst-alt-install"></span>
 
<span id="inst-alt-install"></span>
== Alternate Installation ==
+
== 替代安装 ==
  
Often, it is necessary or desirable to install modules to a location other than
+
通常,需要或希望将模块安装到第三方 Python 模块的标准位置以外的位置。 例如,在 Unix 系统上,您可能没有写入标准第三方模块目录的权限。 或者,您可能希望先试用某个模块,然后再将其作为本地 Python 安装的标准部分。 在升级已经存在的发行版时尤其如此:在实际升级之前,您希望确保现有的脚本库仍然适用于新版本。
the standard location for third-party Python modules. For example, on a Unix
 
system you might not have permission to write to the standard third-party module
 
directory. Or you might wish to try out a module before making it a standard
 
part of your local Python installation. This is especially true when upgrading
 
a distribution already present: you want to make sure your existing base of
 
scripts still works with the new version before actually upgrading.
 
  
The Distutils '''install''' command is designed to make installing module
+
Distutils '''install''' 命令旨在使将模块发行版安装到备用位置变得简单而轻松。 基本思想是为安装提供一个基本目录,'''install''' 命令在此基本目录下选择一组目录(称为 ''安装方案'' ),在其中安装文件. 详细信息因平台而异,因此请阅读以下适用于您的部分。
distributions to an alternate location simple and painless. The basic idea is
 
that you supply a base directory for the installation, and the
 
'''install''' command picks a set of directories (called an ''installation
 
scheme'') under this base directory in which to install files. The details
 
differ across platforms, so read whichever of the following sections applies to
 
you.
 
  
Note that the various alternate installation schemes are mutually exclusive: you
+
请注意,各种替代安装方案是相互排斥的:您可以通过 <code>--user</code>,或 <code>--home</code>,或 <code>--prefix</code> <code>--exec-prefix</code>,或 <code>--install-base</code> ] 和 <code>--install-platbase</code>,但您不能从这些组中混音。
can pass <code>--user</code>, or <code>--home</code>, or <code>--prefix</code> and <code>--exec-prefix</code>, or
 
<code>--install-base</code> and <code>--install-platbase</code>, but you can't mix from these
 
groups.
 
  
 
<div id="alternate-installation-the-user-scheme" class="section">
 
<div id="alternate-installation-the-user-scheme" class="section">
  
 
<span id="inst-alt-install-user"></span>
 
<span id="inst-alt-install-user"></span>
=== Alternate installation: the user scheme ===
+
=== 替代安装:用户方案 ===
  
This scheme is designed to be the most convenient solution for users that don't
+
该方案旨在为没有全局站点包目录写入权限或不想安装到其中的用户提供最方便的解决方案。 它通过一个简单的选项启用:
have write permission to the global site-packages directory or don't want to
 
install into it. It is enabled with a simple option:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第384行: 第273行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --user</pre>
+
<pre class="none">python setup.py install --user</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Files will be installed into subdirectories of [[../../library/site#site|<code>site.USER_BASE</code>]] (written
+
文件将被安装到 [[../../library/site#site|site.USER_BASE]] 的子目录中(以下写为 <code>userbase</code>)。 此方案将纯 Python 模块和扩展模块安装在同一位置(也称为 [[../../library/site#site|site.USER_SITE]])。 以下是 UNIX(包括 macOS)的值:
as <code>userbase</code> hereafter). This scheme installs pure Python modules and
 
extension modules in the same location (also known as [[../../library/site#site|<code>site.USER_SITE</code>]]).
 
Here are the values for UNIX, including Mac OS X:
 
  
 
{|
 
{|
!width="20%"| Type of file
+
!width="20%"| 文件类型
!width="80%"| Installation directory
+
!width="80%"| 安装目录
 
|-
 
|-
| modules
+
| 模块
 
| <code>userbase/lib/pythonX.Y/site-packages</code>
 
| <code>userbase/lib/pythonX.Y/site-packages</code>
 
|-
 
|-
| scripts
+
| 脚本
 
| <code>userbase/bin</code>
 
| <code>userbase/bin</code>
 
|-
 
|-
| data
+
| 数据
 
| <code>userbase</code>
 
| <code>userbase</code>
 
|-
 
|-
| C headers
+
| C 头文件
 
| <code>userbase/include/pythonX.Yabiflags/distname</code>
 
| <code>userbase/include/pythonX.Yabiflags/distname</code>
 
|}
 
|}
  
And here are the values used on Windows:
+
以下是在 Windows 上使用的值:
  
 
{|
 
{|
!width="20%"| Type of file
+
!width="20%"| 文件类型
!width="80%"| Installation directory
+
!width="80%"| 安装目录
 
|-
 
|-
| modules
+
| 模块
 
| <code>userbase\PythonXY\site-packages</code>
 
| <code>userbase\PythonXY\site-packages</code>
 
|-
 
|-
| scripts
+
| 脚本
 
| <code>userbase\PythonXY\Scripts</code>
 
| <code>userbase\PythonXY\Scripts</code>
 
|-
 
|-
| data
+
| 数据
 
| <code>userbase</code>
 
| <code>userbase</code>
 
|-
 
|-
| C headers
+
| C 头文件
 
| <code>userbase\PythonXY\Include{distname}</code>
 
| <code>userbase\PythonXY\Include{distname}</code>
 
|}
 
|}
  
The advantage of using this scheme compared to the other ones described below is
+
与下面描述的其他方案相比,使用此方案的优点是用户站点包目录在正常情况下始终包含在 [[../../library/sys#sys|sys.path]] 中(有关更多信息,请参阅 [[../../library/site#module-site|site]]) ,这意味着在运行 <code>setup.py</code> 脚本完成安装后无需执行额外的步骤。
that the user site-packages directory is under normal conditions always included
 
in [[../../library/sys#sys|<code>sys.path</code>]] (see [[../../library/site#module-site|<code>site</code>]] for more information), which means that
 
there is no additional step to perform after running the <code>setup.py</code> script
 
to finalize the installation.
 
  
The '''build_ext''' command also has a <code>--user</code> option to add
+
'''build_ext''' 命令还有一个 <code>--user</code> 选项,可以将 <code>userbase/include</code> 添加到头文件的编译器搜索路径,将 <code>userbase/lib</code> 添加到库的编译器搜索路径,如以及共享 C 库的运行时搜索路径 (rpath)
<code>userbase/include</code> to the compiler search path for header files and
 
<code>userbase/lib</code> to the compiler search path for libraries as well as to
 
the runtime search path for shared C libraries (rpath).
 
  
  
第446行: 第325行:
  
 
<span id="inst-alt-install-home"></span>
 
<span id="inst-alt-install-home"></span>
=== Alternate installation: the home scheme ===
+
=== 替代安装:家庭方案 ===
  
The idea behind the &quot;home scheme&quot; is that you build and maintain a personal
+
“家庭计划”背后的想法是您构建和维护 Python 模块的个人存储。 该方案的名称源自 Unix 上“主”目录的概念,因为 Unix 用户使他们的主目录具有类似于 <code>/usr/</code> <code>/usr/local/</code> 的布局并不少见。 任何人都可以使用此方案,无论他们安装的操作系统是什么。
stash of Python modules. This scheme's name is derived from the idea of a
 
&quot;home&quot; directory on Unix, since it's not unusual for a Unix user to make their
 
home directory have a layout similar to <code>/usr/</code> or <code>/usr/local/</code>.
 
This scheme can be used by anyone, regardless of the operating system they
 
are installing for.
 
  
Installing a new module distribution is as simple as
+
安装一个新的模块发行版就像
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第461行: 第335行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --home=&lt;dir&gt;</pre>
+
<pre class="none">python setup.py install --home=&lt;dir&gt;</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
where you can supply any directory you like for the <code>--home</code> option. On
+
您可以在其中为 <code>--home</code> 选项提供您喜欢的任何目录。 在 Unix 上,懒惰的打字员只能输入波浪号 (<code>~</code>)'''install''' 命令会将其扩展到您的主目录:
Unix, lazy typists can just type a tilde (<code>~</code>); the '''install''' command
 
will expand this to your home directory:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第474行: 第346行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --home=~</pre>
+
<pre class="none">python setup.py install --home=~</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
To make Python find the distributions installed with this scheme, you may have
+
要让 Python 找到使用此方案安装的发行版,您可能需要 [[#inst-search-path|修改 Python 的搜索路径]] 或编辑 <code>sitecustomize</code>(参见 [[../../library/site#module-site|site]])以调用 [[../../library/site#site|site。 addsitedir()]] 或编辑 [[../../library/sys#sys|sys.path]]
to [[#inst-search-path|<span class="std std-ref">modify Python's search path</span>]] or edit
 
<code>sitecustomize</code> (see [[../../library/site#module-site|<code>site</code>]]) to call [[../../library/site#site|<code>site.addsitedir()</code>]] or edit
 
[[../../library/sys#sys|<code>sys.path</code>]].
 
  
The <code>--home</code> option defines the installation base directory. Files are
+
<code>--home</code> 选项定义安装基础目录。 文件安装到安装库下的以下目录如下:
installed to the following directories under the installation base as follows:
 
  
 
{|
 
{|
!width="20%"| Type of file
+
!width="20%"| 文件类型
!width="80%"| Installation directory
+
!width="80%"| 安装目录
 
|-
 
|-
| modules
+
| 模块
 
| <code>home/lib/python</code>
 
| <code>home/lib/python</code>
 
|-
 
|-
| scripts
+
| 脚本
 
| <code>home/bin</code>
 
| <code>home/bin</code>
 
|-
 
|-
| data
+
| 数据
 
| <code>home</code>
 
| <code>home</code>
 
|-
 
|-
| C headers
+
| C 头文件
 
| <code>home/include/python/distname</code>
 
| <code>home/include/python/distname</code>
 
|}
 
|}
  
(Mentally replace slashes with backslashes if you're on Windows.)
+
(如果您使用的是 Windows,请在心里用反斜杠替换斜杠。)
  
  
第511行: 第379行:
  
 
<span id="inst-alt-install-prefix-unix"></span>
 
<span id="inst-alt-install-prefix-unix"></span>
=== Alternate installation: Unix (the prefix scheme) ===
+
=== 替代安装:Unix(前缀方案) ===
  
The &quot;prefix scheme&quot; is useful when you wish to use one Python installation to
+
当您希望使用一个 Python 安装来执行构建/安装(即运行安装脚本),但将模块安装到另一个 Python 安装的第三方模块目录(或其他看起来像不同的 Python 安装)。 如果这听起来有点不寻常,那就是 - 这就是用户和家庭计划出现的原因。 然而,至少有两种已知的情况下前缀方案将是有用的。
perform the build/install (i.e., to run the setup script), but install modules
 
into the third-party module directory of a different Python installation (or
 
something that looks like a different Python installation). If this sounds a
 
trifle unusual, it is---that's why the user and home schemes come before. However,
 
there are at least two known cases where the prefix scheme will be useful.
 
  
First, consider that many Linux distributions put Python in <code>/usr</code>, rather
+
首先,考虑到许多 Linux 发行版将 Python 放在 <code>/usr</code> 中,而不是更传统的 <code>/usr/local</code>。 这是完全合适的,因为在这些情况下,Python 是“系统”的一部分,而不是本地附加组件。 但是,如果您从源代码安装 Python 模块,您可能希望它们进入 <code>/usr/local/lib/python2.X</code> 而不是 <code>/usr/lib/python2.X</code>。 这可以用
than the more traditional <code>/usr/local</code>. This is entirely appropriate,
 
since in those cases Python is part of &quot;the system&quot; rather than a local add-on.
 
However, if you are installing Python modules from source, you probably want
 
them to go in <code>/usr/local/lib/python2.X</code> rather than
 
<code>/usr/lib/python2.X</code>. This can be done with
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第531行: 第389行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>/usr/bin/python setup.py install --prefix=/usr/local</pre>
+
<pre class="none">/usr/bin/python setup.py install --prefix=/usr/local</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Another possibility is a network filesystem where the name used to write to a
+
另一种可能性是网络文件系统,其中用于写入远程目录的名称与用于读取它的名称不同:例如,作为 <code>/usr/local/bin/python</code> 访问的 Python 解释器可能会在 <code>/usr/local/lib/python2.X</code> 中搜索模块],但这些模块必须安装到 <code>/mnt/@server/export/lib/python2.X</code>。 这可以用
remote directory is different from the name used to read it: for example, the
 
Python interpreter accessed as <code>/usr/local/bin/python</code> might search for
 
modules in <code>/usr/local/lib/python2.X</code>, but those modules would have to
 
be installed to, say, <code>/mnt/@server/export/lib/python2.X</code>. This could
 
be done with
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第547行: 第400行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>/usr/local/bin/python setup.py install --prefix=/mnt/@server/export</pre>
+
<pre class="none">/usr/local/bin/python setup.py install --prefix=/mnt/@server/export</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
In either case, the <code>--prefix</code> option defines the installation base, and
+
在任一情况下,<code>--prefix</code> 选项定义安装基础,<code>--exec-prefix</code> 选项定义平台特定的安装基础,用于特定平台的文件。 (目前,这仅表示非纯模块发行版,但可以扩展为 C 库、二进制可执行文件等)如果未提供 <code>--exec-prefix</code>,则默认为 <code>--prefix</code>。 文件安装如下:
the <code>--exec-prefix</code> option defines the platform-specific installation
 
base, which is used for platform-specific files. (Currently, this just means
 
non-pure module distributions, but could be expanded to C libraries, binary
 
executables, etc.) If <code>--exec-prefix</code> is not supplied, it defaults to
 
<code>--prefix</code>. Files are installed as follows:
 
  
 
{|
 
{|
!width="23%"| Type of file
+
!width="23%"| 文件类型
!width="77%"| Installation directory
+
!width="77%"| 安装目录
 
|-
 
|-
| Python modules
+
| Python 模块
 
| <code>prefix/lib/pythonX.Y/site-packages</code>
 
| <code>prefix/lib/pythonX.Y/site-packages</code>
 
|-
 
|-
| extension modules
+
| 扩展模块
 
| <code>exec-prefix/lib/pythonX.Y/site-packages</code>
 
| <code>exec-prefix/lib/pythonX.Y/site-packages</code>
 
|-
 
|-
| scripts
+
| 脚本
 
| <code>prefix/bin</code>
 
| <code>prefix/bin</code>
 
|-
 
|-
| data
+
| 数据
 
| <code>prefix</code>
 
| <code>prefix</code>
 
|-
 
|-
| C headers
+
| C 头文件
 
| <code>prefix/include/pythonX.Yabiflags/distname</code>
 
| <code>prefix/include/pythonX.Yabiflags/distname</code>
 
|}
 
|}
  
There is no requirement that <code>--prefix</code> or <code>--exec-prefix</code>
+
不要求 <code>--prefix</code> <code>--exec-prefix</code> 实际上指向备用 Python 安装; 如果上面列出的目录尚不存在,则会在安装时创建它们。
actually point to an alternate Python installation; if the directories listed
 
above do not already exist, they are created at installation time.
 
  
Incidentally, the real reason the prefix scheme is important is simply that a
+
顺便说一句,前缀方案很重要的真正原因很简单,标准的 Unix 安装使用前缀方案,但是 <code>--prefix</code> <code>--exec-prefix</code> Python 本身作为 <code>sys.prefix</code> 和 [ X205X]。 因此,您可能认为您永远不会使用前缀方案,但是每次在没有任何其他选项的情况下运行 <code>python setup.py install</code> 时,您都在使用它。
standard Unix installation uses the prefix scheme, but with <code>--prefix</code>
 
and <code>--exec-prefix</code> supplied by Python itself as <code>sys.prefix</code> and
 
<code>sys.exec_prefix</code>. Thus, you might think you'll never use the prefix scheme,
 
but every time you run <code>python setup.py install</code> without any other options,
 
you're using it.
 
  
Note that installing extensions to an alternate Python installation has no
+
请注意,将扩展安装到备用 Python 安装对这些扩展的构建方式没有影响:特别是,将使用与用于运行安装脚本的 Python 解释器一起安装的 Python 头文件(<code>Python.h</code> 和朋友)在编译扩展。 您有责任确保用于运行以这种方式安装的扩展的解释器与用于构建它们的解释器兼容。 最好的方法是确保两个解释器是相同版本的 Python(可能是不同的构建,或者可能是相同构建的副本)。 (当然,如果您的 <code>--prefix</code> <code>--exec-prefix</code> 甚至没有指向备用 Python 安装,这无关紧要。)
effect on how those extensions are built: in particular, the Python header files
 
(<code>Python.h</code> and friends) installed with the Python interpreter used to run
 
the setup script will be used in compiling extensions. It is your
 
responsibility to ensure that the interpreter used to run extensions installed
 
in this way is compatible with the interpreter used to build them. The best way
 
to do this is to ensure that the two interpreters are the same version of Python
 
(possibly different builds, or possibly copies of the same build). (Of course,
 
if your <code>--prefix</code> and <code>--exec-prefix</code> don't even point to an
 
alternate Python installation, this is immaterial.)
 
  
  
第606行: 第438行:
  
 
<span id="inst-alt-install-prefix-windows"></span>
 
<span id="inst-alt-install-prefix-windows"></span>
=== Alternate installation: Windows (the prefix scheme) ===
+
=== 替代安装:Windows(前缀方案) ===
  
Windows has no concept of a user's home directory, and since the standard Python
+
Windows 没有用户主目录的概念,而且由于 Windows 下的标准 Python 安装比 Unix 下简单,<code>--prefix</code> 选项传统上用于在 Windows 上的不同位置安装附加包。
installation under Windows is simpler than under Unix, the <code>--prefix</code>
 
option has traditionally been used to install additional packages in separate
 
locations on Windows.
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第617行: 第446行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --prefix=&quot;\Temp\Python&quot;</pre>
+
<pre class="none">python setup.py install --prefix=&quot;\Temp\Python&quot;</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
to install modules to the <code>\Temp\Python</code> directory on the current drive.
+
将模块安装到当前驱动器上的 <code>\Temp\Python</code> 目录。
  
The installation base is defined by the <code>--prefix</code> option; the
+
安装基础由<code>--prefix</code>选项定义; Windows 下不支持 <code>--exec-prefix</code> 选项,这意味着纯 Python 模块和扩展模块安装在同一位置。 文件安装如下:
<code>--exec-prefix</code> option is not supported under Windows, which means that
 
pure Python modules and extension modules are installed into the same location.
 
Files are installed as follows:
 
  
 
{|
 
{|
!width="21%"| Type of file
+
!width="21%"| 文件类型
!width="79%"| Installation directory
+
!width="79%"| 安装目录
 
|-
 
|-
| modules
+
| 模块
 
| <code>prefix\Lib\site-packages</code>
 
| <code>prefix\Lib\site-packages</code>
 
|-
 
|-
| scripts
+
| 脚本
 
| <code>prefix\Scripts</code>
 
| <code>prefix\Scripts</code>
 
|-
 
|-
| data
+
| 数据
 
| <code>prefix</code>
 
| <code>prefix</code>
 
|-
 
|-
| C headers
+
| C 头文件
 
| <code>prefix\Include{distname}</code>
 
| <code>prefix\Include{distname}</code>
 
|}
 
|}
第653行: 第479行:
  
 
<span id="inst-custom-install"></span>
 
<span id="inst-custom-install"></span>
== Custom Installation ==
+
== 自定义安装 ==
  
Sometimes, the alternate installation schemes described in section
+
有时,在 [[#inst-alt-install|替代安装]] 部分中描述的替代安装方案并不能满足您的要求。 您可能只想调整一两个目录,同时将所有内容保留在同一个基本目录下,或者您可能想要完全重新定义安装方案。 无论哪种情况,您都在创建 ''自定义安装方案''
[[#inst-alt-install|<span class="std std-ref">Alternate Installation</span>]] just don't do what you want. You might want to tweak just
 
one or two directories while keeping everything under the same base directory,
 
or you might want to completely redefine the installation scheme. In either
 
case, you're creating a ''custom installation scheme''.
 
  
To create a custom installation scheme, you start with one of the alternate
+
要创建自定义安装方案,您可以从备用方案之一开始,并使用以下选项覆盖用于各种类型文件的一些安装目录:
schemes and override some of the installation directories used for the various
 
types of files, using these options:
 
  
 
{|
 
{|
!width="49%"| Type of file
+
!width="49%"| 文件类型
!width="51%"| Override option
+
!width="51%"| 覆盖选项
 
|-
 
|-
| Python modules
+
| Python 模块
 
| <code>--install-purelib</code>
 
| <code>--install-purelib</code>
 
|-
 
|-
| extension modules
+
| 扩展模块
 
| <code>--install-platlib</code>
 
| <code>--install-platlib</code>
 
|-
 
|-
| all modules
+
| 所有模块
 
| <code>--install-lib</code>
 
| <code>--install-lib</code>
 
|-
 
|-
| scripts
+
| 脚本
 
| <code>--install-scripts</code>
 
| <code>--install-scripts</code>
 
|-
 
|-
| data
+
| 数据
 
| <code>--install-data</code>
 
| <code>--install-data</code>
 
|-
 
|-
| C headers
+
| C 头文件
 
| <code>--install-headers</code>
 
| <code>--install-headers</code>
 
|}
 
|}
  
These override options can be relative, absolute,
+
这些覆盖选项可以是相对的、绝对的或根据安装基础目录之一明确定义的。 (有两个安装基础目录,它们通常是相同的——它们仅在您使用 Unix“前缀方案”并提供不同的 <code>--prefix</code> <code>--exec-prefix</code> 选项时有所不同;使用 [ X196X] 将覆盖为 <code>--install-purelib</code> <code>--install-platlib</code> 计算或给出的值,建议用于在 Python 和扩展模块之间没有区别的方案。)
or explicitly defined in terms of one of the installation base directories.
 
(There are two installation base directories, and they are normally the
 
same---they only differ when you use the Unix &quot;prefix scheme&quot; and supply
 
different <code>--prefix</code> and <code>--exec-prefix</code> options; using <code>--install-lib</code>
 
will override values computed or given for <code>--install-purelib</code> and
 
<code>--install-platlib</code>, and is recommended for schemes that don't make a
 
difference between Python and extension modules.)
 
  
For example, say you're installing a module distribution to your home directory
+
例如,假设您要在 Unix 下将模块分发版安装到您的主目录,但您希望脚本进入 <code>~/scripts</code> 而不是 <code>~/bin</code>。 如您所料,您可以使用 <code>--install-scripts</code> 选项覆盖此目录; 在这种情况下,提供相对路径最有意义,该路径将相对于安装基目录(在这种情况下是您的主目录)进行解释:
under Unix---but you want scripts to go in <code>~/scripts</code> rather than
 
<code>~/bin</code>. As you might expect, you can override this directory with the
 
<code>--install-scripts</code> option; in this case, it makes most sense to supply
 
a relative path, which will be interpreted relative to the installation base
 
directory (your home directory, in this case):
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第708行: 第516行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --home=~ --install-scripts=scripts</pre>
+
<pre class="none">python setup.py install --home=~ --install-scripts=scripts</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Another Unix example: suppose your Python installation was built and installed
+
另一个 Unix 示例:假设您的 Python 安装是使用 <code>/usr/local/python</code> 前缀构建和安装的,因此在标准安装脚本下,脚本将以 <code>/usr/local/python/bin</code> 结尾。 如果你希望它们在 <code>/usr/local/bin</code> 中,你可以为 <code>--install-scripts</code> 选项提供这个绝对目录:
with a prefix of <code>/usr/local/python</code>, so under a standard installation
 
scripts will wind up in <code>/usr/local/python/bin</code>. If you want them in
 
<code>/usr/local/bin</code> instead, you would supply this absolute directory for the
 
<code>--install-scripts</code> option:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第723行: 第527行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --install-scripts=/usr/local/bin</pre>
+
<pre class="none">python setup.py install --install-scripts=/usr/local/bin</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
(This performs an installation using the &quot;prefix scheme&quot;, where the prefix is
+
(这使用“前缀方案”执行安装,在这种情况下,前缀是安装 Python 解释器的任何内容 - <code>/usr/local/python</code>。)
whatever your Python interpreter was installed with--- <code>/usr/local/python</code>
 
in this case.)
 
  
If you maintain Python on Windows, you might want third-party modules to live in
+
如果您在 Windows 上维护 Python,您可能希望第三方模块位于 <code>prefix</code> 的子目录中,而不是直接位于 <code>prefix</code> 本身中。 这几乎和自定义脚本安装目录一样简单——你只需要记住有两种类型的模块需要担心,Python 和扩展模块,它们都可以通过一个选项方便地控制:
a subdirectory of <code>prefix</code>, rather than right in <code>prefix</code>
 
itself. This is almost as easy as customizing the script installation
 
directory---you just have to remember that there are two types of modules
 
to worry about, Python and extension modules, which can conveniently be both
 
controlled by one option:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第743行: 第540行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --install-lib=Site</pre>
+
<pre class="none">python setup.py install --install-lib=Site</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The specified installation directory is relative to <code>prefix</code>. Of
+
指定的安装目录相对于<code>prefix</code>。 当然,您还必须确保该目录在 Python 的模块搜索路径中,例如将 <code>.pth</code> 文件放在站点目录中(请参阅 [[../../library/site#module-site|site]])。 请参阅部分 [[#inst-search-path|修改 Python 的搜索路径]] 以了解如何修改 Python 的搜索路径。
course, you also have to ensure that this directory is in Python's module
 
search path, such as by putting a <code>.pth</code> file in a site directory (see
 
[[../../library/site#module-site|<code>site</code>]]). See section [[#inst-search-path|<span class="std std-ref">Modifying Python's Search Path</span>]] to find out how to modify
 
Python's search path.
 
  
If you want to define an entire installation scheme, you just have to supply all
+
如果要定义整个安装方案,只需提供所有安装目录选项。 推荐的方法是提供相对路径; 例如,如果您想在您的主目录中维护 <code>python</code> 下所有与 Python 模块相关的文件,并且您希望为您使用主目录的每个平台都有一个单独的目录,您可以定义以下安装方案:
of the installation directory options. The recommended way to do this is to
 
supply relative paths; for example, if you want to maintain all Python
 
module-related files under <code>python</code> in your home directory, and you want a
 
separate directory for each platform that you use your home directory from, you
 
might define the following installation scheme:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第765行: 第553行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --home=~ \
+
<pre class="none">python setup.py install --home=~ \
 
                         --install-purelib=python/lib \
 
                         --install-purelib=python/lib \
 
                         --install-platlib=python/lib.$PLAT \
 
                         --install-platlib=python/lib.$PLAT \
第774行: 第562行:
  
 
</div>
 
</div>
or, equivalently,
+
或者,等效地,
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第780行: 第568行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --home=~/python \
+
<pre class="none">python setup.py install --home=~/python \
 
                         --install-purelib=lib \
 
                         --install-purelib=lib \
 
                         --install-platlib='lib.$PLAT' \
 
                         --install-platlib='lib.$PLAT' \
第789行: 第577行:
  
 
</div>
 
</div>
<code>$PLAT</code> is not (necessarily) an environment variable---it will be expanded by
+
<code>$PLAT</code> 不是(必然)环境变量——它会在 Distutils 解析您的命令行选项时进行扩展,就像解析您的配置文件时一样。
the Distutils as it parses your command line options, just as it does when
 
parsing your configuration file(s).
 
  
Obviously, specifying the entire installation scheme every time you install a
+
显然,每次安装新的模块发行版时都指定整个安装方案会非常乏味。 因此,您可以将这些选项放入您的 Distutils 配置文件中(请参阅 [[#inst-config-files|Distutils 配置文件]] 部分):
new module distribution would be very tedious. Thus, you can put these options
 
into your Distutils config file (see section [[#inst-config-files|<span class="std std-ref">Distutils Configuration Files</span>]]):
 
  
 
<div class="highlight-ini notranslate">
 
<div class="highlight-ini notranslate">
第801行: 第585行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>[install]
+
<syntaxhighlight lang="ini">[install]
 
install-base=$HOME
 
install-base=$HOME
 
install-purelib=python/lib
 
install-purelib=python/lib
 
install-platlib=python/lib.$PLAT
 
install-platlib=python/lib.$PLAT
 
install-scripts=python/scripts
 
install-scripts=python/scripts
install-data=python/data</pre>
+
install-data=python/data</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
or, equivalently,
+
或者,等效地,
  
 
<div class="highlight-ini notranslate">
 
<div class="highlight-ini notranslate">
第817行: 第601行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>[install]
+
<syntaxhighlight lang="ini">[install]
 
install-base=$HOME/python
 
install-base=$HOME/python
 
install-purelib=lib
 
install-purelib=lib
 
install-platlib=lib.$PLAT
 
install-platlib=lib.$PLAT
 
install-scripts=scripts
 
install-scripts=scripts
install-data=data</pre>
+
install-data=data</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Note that these two are ''not'' equivalent if you supply a different installation
+
请注意,如果您在运行安装脚本时提供不同的安装基目录,则这两个 ''不是'' 等效。 例如,
base directory when you run the setup script. For example,
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第834行: 第617行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py install --install-base=/tmp</pre>
+
<pre class="none">python setup.py install --install-base=/tmp</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
would install pure modules to <code>/tmp/python/lib</code> in the first case, and
+
在第一种情况下将纯模块安装到 <code>/tmp/python/lib</code>,在第二种情况下安装到 <code>/tmp/lib</code>。 (对于第二种情况,您可能需要提供 <code>/tmp/python</code> 的安装基础。)
to <code>/tmp/lib</code> in the second case. (For the second case, you probably
 
want to supply an installation base of <code>/tmp/python</code>.)
 
  
You probably noticed the use of <code>$HOME</code> and <code>$PLAT</code> in the sample
+
您可能注意到在示例配置文件输入中使用了 <code>$HOME</code> <code>$PLAT</code>。 这些是 Distutils 配置变量,它们与环境变量非常相似。 实际上,您可以在具有这种概念的平台上的配置文件中使用环境变量,但 Distutils 还定义了一些可能不在您的环境中的额外变量,例如 <code>$PLAT</code>。 (当然,在没有环境变量的系统上,例如 Mac OS 9,Distutils 提供的配置变量是您唯一可以使用的。)有关详细信息,请参阅 [[#inst-config-files|Distutils 配置文件]] 部分.
configuration file input. These are Distutils configuration variables, which
 
bear a strong resemblance to environment variables. In fact, you can use
 
environment variables in config files on platforms that have such a notion but
 
the Distutils additionally define a few extra variables that may not be in your
 
environment, such as <code>$PLAT</code>. (And of course, on systems that don't have
 
environment variables, such as Mac OS 9, the configuration variables supplied by
 
the Distutils are the only ones you can use.) See section [[#inst-config-files|<span class="std std-ref">Distutils Configuration Files</span>]]
 
for details.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
When a [[../../library/venv#venv-def|<span class="std std-ref">virtual environment</span>]] is activated, any options
+
当一个[[../../library/venv#venv-def|虚拟环境]]被激活时,所有distutils配置文件中任何改变安装路径的选项都会被忽略,以防止无意中将项目安装到虚拟环境之外。
that change the installation path will be ignored from all distutils configuration
 
files to prevent inadvertently installing projects outside of the virtual
 
environment.
 
  
  
第867行: 第637行:
  
 
<span id="inst-search-path"></span>
 
<span id="inst-search-path"></span>
=== Modifying Python's Search Path ===
+
=== 修改 Python 的搜索路径 ===
  
When the Python interpreter executes an [[../../reference/simple_stmts#import|<code>import</code>]] statement, it searches
+
Python 解释器执行 [[../../reference/simple_stmts#import|import]] 语句时,它会沿着搜索路径搜索 Python 代码和扩展模块。 在构建解释器时,路径的默认值被配置到 Python 二进制文件中。 您可以通过导入[[../../library/sys#module-sys|sys]]模块并打印<code>sys.path</code>的值来确定路径。
for both Python code and extension modules along a search path. A default value
 
for the path is configured into the Python binary when the interpreter is built.
 
You can determine the path by importing the [[../../library/sys#module-sys|<code>sys</code>]] module and printing the
 
value of <code>sys.path</code>.
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第879行: 第645行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>$ python
+
<pre class="none">$ python
 
Python 2.2 (#11, Oct  3 2002, 13:31:27)
 
Python 2.2 (#11, Oct  3 2002, 13:31:27)
 
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
 
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
第893行: 第659行:
  
 
</div>
 
</div>
The null string in <code>sys.path</code> represents the current working directory.
+
<code>sys.path</code> 中的空字符串表示当前工作目录。
  
The expected convention for locally installed packages is to put them in the
+
本地安装包的预期约定是将它们放在 <code>/site-packages/</code> 目录中,但您可能希望将 Python 模块安装到某个任意目录中。 例如,您的站点可能有一个约定,将与 Web 服务器相关的所有软件保留在 <code>/www</code> 下。 附加 Python 模块可能属于 <code>/www/python</code>,为了导入它们,必须将此目录添加到 <code>sys.path</code>。 有几种不同的方法可以添加目录。
<code>.../site-packages/</code> directory, but you may want to install Python
 
modules into some arbitrary directory. For example, your site may have a
 
convention of keeping all software related to the web server under <code>/www</code>.
 
Add-on Python modules might then belong in <code>/www/python</code>, and in order to
 
import them, this directory must be added to <code>sys.path</code>. There are several
 
different ways to add the directory.
 
  
The most convenient way is to add a path configuration file to a directory
+
最方便的方法是将路径配置文件添加到已经在 Python 路径上的目录中,通常是添加到 <code>.../site-packages/</code> 目录中。 路径配置文件的扩展名为 <code>.pth</code>,每行必须包含一个将附加到 <code>sys.path</code> 的路径。 (因为新路径附加到 <code>sys.path</code>,添加目录中的模块不会覆盖标准模块。 这意味着您不能使用此机制来安装标准模块的固定版本。)
that's already on Python's path, usually to the <code>.../site-packages/</code>
 
directory. Path configuration files have an extension of <code>.pth</code>, and each
 
line must contain a single path that will be appended to <code>sys.path</code>. (Because
 
the new paths are appended to <code>sys.path</code>, modules in the added directories
 
will not override standard modules. This means you can't use this mechanism for
 
installing fixed versions of standard modules.)
 
  
Paths can be absolute or relative, in which case they're relative to the
+
路径可以是绝对的或相对的,在这种情况下,它们相对于包含 <code>.pth</code> 文件的目录。 有关更多信息,请参阅 [[../../library/site#module-site|site]] 模块的文档。
directory containing the <code>.pth</code> file. See the documentation of
 
the [[../../library/site#module-site|<code>site</code>]] module for more information.
 
  
A slightly less convenient way is to edit the <code>site.py</code> file in Python's
+
稍微不太方便的方法是编辑Python标准库中的<code>site.py</code>文件,修改<code>sys.path</code><code>site.py</code> Python 解释器执行时自动导入,除非提供 [[../../using/cmdline#cmdoption-S|-S]] 开关来抑制这种行为。 因此,您可以简单地编辑 <code>site.py</code> 并向其添加两行:
standard library, and modify <code>sys.path</code>. <code>site.py</code> is automatically
 
imported when the Python interpreter is executed, unless the [[../../using/cmdline#cmdoption-S|<code>-S</code>]] switch
 
is supplied to suppress this behaviour. So you could simply edit
 
<code>site.py</code> and add two lines to it:
 
  
 
<div class="highlight-python notranslate">
 
<div class="highlight-python notranslate">
第925行: 第673行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import sys
+
<syntaxhighlight lang="python">import sys
sys.path.append('/www/python/')</pre>
+
sys.path.append('/www/python/')</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
However, if you reinstall the same major version of Python (perhaps when
+
但是,如果您重新安装 Python 的相同主要版本(例如,从 2.2 升级到 2.2.2 时)<code>site.py</code> 将被库存版本覆盖。 您必须记住它已被修改并在安装之前保存副本。
upgrading from 2.2 to 2.2.2, for example) <code>site.py</code> will be overwritten by
 
the stock version. You'd have to remember that it was modified and save a copy
 
before doing the installation.
 
  
There are two environment variables that can modify <code>sys.path</code>.
+
有两个环境变量可以修改<code>sys.path</code><span id="index-0" class="target"></span>[[../../using/cmdline#envvar-PYTHONHOME|PYTHONHOME]] 设置 Python 安装前缀的替代值。 例如,如果 <span id="index-1" class="target"></span>[[../../using/cmdline#envvar-PYTHONHOME|PYTHONHOME]] 设置为 <code>/www/python</code>,则搜索路径将设置为 <code>['', '/www/python/lib/pythonX.Y/', '/www/python/lib/pythonX.Y/plat-linux2', ...]</code>
<span id="index-0" class="target"></span>[[../../using/cmdline#envvar-PYTHONHOME|<code>PYTHONHOME</code>]] sets an alternate value for the prefix of the Python
 
installation. For example, if <span id="index-1" class="target"></span>[[../../using/cmdline#envvar-PYTHONHOME|<code>PYTHONHOME</code>]] is set to <code>/www/python</code>,
 
the search path will be set to <code>['', '/www/python/lib/pythonX.Y/', '/www/python/lib/pythonX.Y/plat-linux2', ...]</code>.
 
  
The <span id="index-2" class="target"></span>[[../../using/cmdline#envvar-PYTHONPATH|<code>PYTHONPATH</code>]] variable can be set to a list of paths that will be
+
<span id="index-2" class="target"></span>[[../../using/cmdline#envvar-PYTHONPATH|PYTHONPATH]] 变量可以设置为将添加到 <code>sys.path</code> 开头的路径列表。 例如,如果 <span id="index-3" class="target"></span>[[../../using/cmdline#envvar-PYTHONPATH|PYTHONPATH]] 设置为 <code>/www/python:/opt/py</code>,则搜索路径将以 <code>['/www/python', '/opt/py']</code> 开头。 (请注意,目录必须存在才能添加到 <code>sys.path</code>[[../../library/site#module-site|site]] 模块删除不存在的路径。)
added to the beginning of <code>sys.path</code>. For example, if <span id="index-3" class="target"></span>[[../../using/cmdline#envvar-PYTHONPATH|<code>PYTHONPATH</code>]] is
 
set to <code>/www/python:/opt/py</code>, the search path will begin with
 
<code>['/www/python', '/opt/py']</code>. (Note that directories must exist in order to
 
be added to <code>sys.path</code>; the [[../../library/site#module-site|<code>site</code>]] module removes paths that don't
 
exist.)
 
  
Finally, <code>sys.path</code> is just a regular Python list, so any Python application
+
最后,<code>sys.path</code> 只是一个常规的 Python 列表,因此任何 Python 应用程序都可以通过添加或删除条目来修改它。
can modify it by adding or removing entries.
 
  
  
第958行: 第694行:
  
 
<span id="inst-config-files"></span>
 
<span id="inst-config-files"></span>
== Distutils Configuration Files ==
+
== Distutils 配置文件 ==
  
As mentioned above, you can use Distutils configuration files to record personal
+
如上所述,您可以使用 Distutils 配置文件来记录任何 Distutils 选项的个人或站点首选项。 也就是说,任何命令的任何选项都可以存储在两个或三个(取决于您的平台)配置文件之一中,在解析命令行之前将参考这些文件。 这意味着配置文件将覆盖默认值,而命令行将依次覆盖配置文件。 此外,如果应用多个配置文件,“较早”文件中的值将被“较晚”文件覆盖。
or site preferences for any Distutils options. That is, any option to any
 
command can be stored in one of two or three (depending on your platform)
 
configuration files, which will be consulted before the command-line is parsed.
 
This means that configuration files will override default values, and the
 
command-line will in turn override configuration files. Furthermore, if
 
multiple configuration files apply, values from &quot;earlier&quot; files are overridden
 
by &quot;later&quot; files.
 
  
 
<div id="location-and-names-of-config-files" class="section">
 
<div id="location-and-names-of-config-files" class="section">
  
 
<span id="inst-config-filenames"></span>
 
<span id="inst-config-filenames"></span>
=== Location and names of config files ===
+
=== 配置文件的位置和名称 ===
  
The names and locations of the configuration files vary slightly across
+
配置文件的名称和位置因平台而异。 在 Unix 和 macOS 上,三个配置文件(按处理顺序)是:
platforms. On Unix and Mac OS X, the three configuration files (in the order
 
they are processed) are:
 
  
 
{|
 
{|
!width="18%"| Type of file
+
!width="18%"| 文件类型
!width="73%"| Location and filename
+
!width="73%"| 位置和文件名
!width="9%"| Notes
+
!width="9%"| 笔记
 
|-
 
|-
| system
+
| 系统
 
| <code>prefix/lib/pythonver/distutils/distutils.cfg</code>
 
| <code>prefix/lib/pythonver/distutils/distutils.cfg</code>
 
| (1)
 
| (1)
 
|-
 
|-
| personal
+
| 个人的
 
| <code>$HOME/.pydistutils.cfg</code>
 
| <code>$HOME/.pydistutils.cfg</code>
 
| (2)
 
| (2)
 
|-
 
|-
| local
+
| 当地的
 
| <code>setup.cfg</code>
 
| <code>setup.cfg</code>
 
| (3)
 
| (3)
 
|}
 
|}
  
And on Windows, the configuration files are:
+
Windows 上,配置文件是:
  
 
{|
 
{|
!width="20%"| Type of file
+
!width="20%"| 文件类型
!width="70%"| Location and filename
+
!width="70%"| 位置和文件名
!width="10%"| Notes
+
!width="10%"| 笔记
 
|-
 
|-
| system
+
| 系统
 
| <code>prefix\Lib\distutils\distutils.cfg</code>
 
| <code>prefix\Lib\distutils\distutils.cfg</code>
 
| (4)
 
| (4)
 
|-
 
|-
| personal
+
| 个人的
 
| <code>%HOME%\pydistutils.cfg</code>
 
| <code>%HOME%\pydistutils.cfg</code>
 
| (5)
 
| (5)
 
|-
 
|-
| local
+
| 当地的
 
| <code>setup.cfg</code>
 
| <code>setup.cfg</code>
 
| (3)
 
| (3)
 
|}
 
|}
  
On all platforms, the &quot;personal&quot; file can be temporarily disabled by
+
在所有平台上,可以通过传递 –no-user-cfg 选项来暂时禁用“个人”文件。
passing the --no-user-cfg option.
 
  
Notes:
+
笔记:
  
# Strictly speaking, the system-wide configuration file lives in the directory where the Distutils are installed; under Python 1.6 and later on Unix, this is as shown. For Python 1.5.2, the Distutils will normally be installed to <code>prefix/lib/python1.5/site-packages/distutils</code>, so the system configuration file should be put there under Python 1.5.2.
+
# 严格来说,系统范围的配置文件位于安装 Distutils 的目录中; 在 Python 1.6 及更高版本的 Unix 下,如图所示。 对于 Python 1.5.2,Distutils 通常会安装到 <code>prefix/lib/python1.5/site-packages/distutils</code>,所以系统配置文件应该放在 Python 1.5.2 下。
# On Unix, if the <span id="index-4" class="target"></span><code>HOME</code> environment variable is not defined, the user's home directory will be determined with the <code>getpwuid()</code> function from the standard [[../../library/pwd#module-pwd|<code>pwd</code>]] module. This is done by the [[../../library/os.path#os.path|<code>os.path.expanduser()</code>]] function used by Distutils.
+
# Unix 上,如果 <span id="index-4" class="target"></span><code>HOME</code> 环境变量未定义,则用户的主目录将由标准 [[../../library/pwd#module-pwd|pwd]] 模块中的 <code>getpwuid()</code> 函数确定. 这是由 Distutils 使用的 [[../../library/os.path#os.path|os.path.expanduser()]] 函数完成的。
# I.e., in the current directory (usually the location of the setup script).
+
# 即,在当前目录中(通常是安装脚本的位置)。
# (See also note (1).) Under Python 1.6 and later, Python's default &quot;installation prefix&quot; is <code>C:\Python</code>, so the system configuration file is normally <code>C:\Python\Lib\distutils\distutils.cfg</code>. Under Python 1.5.2, the default prefix was <code>C:\Program Files\Python</code>, and the Distutils were not part of the standard library---so the system configuration file would be <code>C:\Program Files\Python\distutils\distutils.cfg</code> in a standard Python 1.5.2 installation under Windows.
+
# (另请参见注释(1)。)在 Python 1.6 及更高版本下,Python 的默认“安装前缀”为 <code>C:\Python</code>,因此系统配置文件通常为 <code>C:\Python\Lib\distutils\distutils.cfg</code>。 在 Python 1.5.2 下,默认前缀是 <code>C:\Program Files\Python</code>,并且 Distutils 不是标准库的一部分——因此在标准 Python 1.5.2 安装中,系统配置文件将是 <code>C:\Program Files\Python\distutils\distutils.cfg</code>视窗。
# On Windows, if the <span id="index-5" class="target"></span><code>HOME</code> environment variable is not defined, <span id="index-6" class="target"></span><code>USERPROFILE</code> then <span id="index-7" class="target"></span><code>HOMEDRIVE</code> and <span id="index-8" class="target"></span><code>HOMEPATH</code> will be tried. This is done by the [[../../library/os.path#os.path|<code>os.path.expanduser()</code>]] function used by Distutils.
+
# Windows 上,如果 <span id="index-5" class="target"></span><code>HOME</code> 环境变量未定义,则 <span id="index-6" class="target"></span><code>USERPROFILE</code> 然后 <span id="index-7" class="target"></span><code>HOMEDRIVE</code> 和 [将尝试 X122X]<code>HOMEPATH</code>。 这是由 Distutils 使用的 [[../../library/os.path#os.path|os.path.expanduser()]] 函数完成的。
  
  
第1,032行: 第758行:
  
 
<span id="inst-config-syntax"></span>
 
<span id="inst-config-syntax"></span>
=== Syntax of config files ===
+
=== 配置文件的语法 ===
  
The Distutils configuration files all have the same syntax. The config files
+
Distutils 配置文件都具有相同的语法。 配置文件分为几个部分。 每个 Distutils 命令都有一个部分,另外还有一个 <code>global</code> 部分用于影响每个命令的全局选项。 每个部分每行包含一个选项,指定为 <code>option=value</code>
are grouped into sections. There is one section for each Distutils command,
 
plus a <code>global</code> section for global options that affect every command. Each
 
section consists of one option per line, specified as <code>option=value</code>.
 
  
For example, the following is a complete config file that just forces all
+
例如,下面是一个完整的配置文件,它只是强制所有命令默认安静运行:
commands to run quietly by default:
 
  
 
<div class="highlight-ini notranslate">
 
<div class="highlight-ini notranslate">
第1,046行: 第768行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>[global]
+
<syntaxhighlight lang="ini">[global]
verbose=0</pre>
+
verbose=0</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
If this is installed as the system config file, it will affect all processing of
+
如果它作为系统配置文件安装,它将影响当前系统上任何用户对任何 Python 模块分发的所有处理。 如果它作为您的个人配置文件安装(在支持它们的系统上),它只会影响您处理的模块分发。 如果它被用作特定模块分发的 <code>setup.cfg</code>,它只会影响该分发。
any Python module distribution by any user on the current system. If it is
 
installed as your personal config file (on systems that support them), it will
 
affect only module distributions processed by you. And if it is used as the
 
<code>setup.cfg</code> for a particular module distribution, it affects only that
 
distribution.
 
  
You could override the default &quot;build base&quot; directory and make the
+
您可以覆盖默认的“build base”目录,并使 '''build*''' 命令始终使用以下命令强制重建所有文件:
'''build*''' commands always forcibly rebuild all files with the
 
following:
 
  
 
<div class="highlight-ini notranslate">
 
<div class="highlight-ini notranslate">
第1,067行: 第782行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>[build]
+
<syntaxhighlight lang="ini">[build]
 
build-base=blib
 
build-base=blib
force=1</pre>
+
force=1</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
which corresponds to the command-line arguments
+
对应于命令行参数
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,080行: 第795行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py build --build-base=blib --force</pre>
+
<pre class="none">python setup.py build --build-base=blib --force</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
except that including the '''build''' command on the command-line means
+
除了在命令行中包含 '''build''' 命令意味着该命令将被运行。 在配置文件中包含特定命令没有这样的含义; 这仅意味着如果运行该命令,则配置文件中的选项将适用。 (或者,如果运行其他从中派生值的命令,它们将使用配置文件中的值。)
that command will be run. Including a particular command in config files has no
 
such implication; it only means that if the command is run, the options in the
 
config file will apply. (Or if other commands that derive values from it are
 
run, they will use the values in the config file.)
 
  
You can find out the complete list of options for any command using the
+
您可以使用 <code>--help</code> 选项找到任何命令的完整选项列表,例如:
<code>--help</code> option, e.g.:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,098行: 第808行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py build --help</pre>
+
<pre class="none">python setup.py build --help</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
and you can find out the complete list of global options by using
+
您可以通过使用 <code>--help</code> 而无需命令来查找全局选项的完整列表:
<code>--help</code> without a command:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,110行: 第819行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py --help</pre>
+
<pre class="none">python setup.py --help</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
See also the &quot;Reference&quot; section of the &quot;Distributing Python Modules&quot; manual.
+
另请参阅“分发 Python 模块”手册的“参考”部分。
  
  
第1,124行: 第833行:
  
 
<span id="inst-building-ext"></span>
 
<span id="inst-building-ext"></span>
== Building Extensions: Tips and Tricks ==
+
== 构建扩展:提示和技巧 ==
  
Whenever possible, the Distutils try to use the configuration information made
+
只要有可能,Distutils 就会尝试使用由用于运行 <code>setup.py</code> 脚本的 Python 解释器提供的配置信息。 例如,用于编译 Python 的相同编译器和链接器标志也将用于编译扩展。 通常这会很有效,但在复杂的情况下这可能不合适。 本节讨论如何覆盖通常的 Distutils 行为。
available by the Python interpreter used to run the <code>setup.py</code> script.
 
For example, the same compiler and linker flags used to compile Python will also
 
be used for compiling extensions. Usually this will work well, but in
 
complicated situations this might be inappropriate. This section discusses how
 
to override the usual Distutils behaviour.
 
  
 
<div id="tweaking-compiler-linker-flags" class="section">
 
<div id="tweaking-compiler-linker-flags" class="section">
  
 
<span id="inst-tweak-flags"></span>
 
<span id="inst-tweak-flags"></span>
=== Tweaking compiler/linker flags ===
+
=== 调整编译器/链接器标志 ===
  
Compiling a Python extension written in C or C++ will sometimes require
+
编译用 C C++ 编写的 Python 扩展有时需要为编译器和链接器指定自定义标志,以便使用特定库或生成特殊类型的目标代码。 如果扩展尚未在您的平台上进行测试,或者您正在尝试交叉编译 Python,则尤其如此。
specifying custom flags for the compiler and linker in order to use a particular
 
library or produce a special kind of object code. This is especially true if the
 
extension hasn't been tested on your platform, or if you're trying to
 
cross-compile Python.
 
  
In the most general case, the extension author might have foreseen that
+
在最一般的情况下,扩展作者可能已经预见到编译扩展会很复杂,并提供了一个 <code>Setup</code> 文件供您编辑。 这可能只有在模块分发包含许多单独的扩展模块,或者如果它们经常需要精心设计的编译器标志集才能工作时才会这样做。
compiling the extensions would be complicated, and provided a <code>Setup</code> file
 
for you to edit. This will likely only be done if the module distribution
 
contains many separate extension modules, or if they often require elaborate
 
sets of compiler flags in order to work.
 
  
A <code>Setup</code> file, if present, is parsed in order to get a list of extensions
+
解析 <code>Setup</code> 文件(如果存在)以获得要构建的扩展列表。 <code>Setup</code> 中的每一行描述了一个模块。 行具有以下结构:
to build. Each line in a <code>Setup</code> describes a single module. Lines have
 
the following structure:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,158行: 第852行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>module ... [sourcefile ...] [cpparg ...] [library ...]</pre>
+
<pre class="none">module ... [sourcefile ...] [cpparg ...] [library ...]</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Let's examine each of the fields in turn.
+
让我们依次检查每个字段。
  
* ''module'' is the name of the extension module to be built, and should be a valid Python identifier. You can't just change this in order to rename a module (edits to the source code would also be needed), so this should be left alone.
+
* ''module'' 是要构建的扩展模块的名称,应该是一个有效的 Python 标识符。 您不能仅仅为了重命名模块而更改它(还需要对源代码进行编辑),因此应该将其保留。
* ''sourcefile'' is anything that's likely to be a source code file, at least judging by the filename. Filenames ending in <code>.c</code> are assumed to be written in C, filenames ending in <code>.C</code>, <code>.cc</code>, and <code>.c++</code> are assumed to be C++, and filenames ending in <code>.m</code> or <code>.mm</code> are assumed to be in Objective C.
+
* ''sourcefile'' 是任何可能是源代码文件的东西,至少从文件名来看是这样。 假设以 <code>.c</code> 结尾的文件名是用 C 编写的,以 <code>.C</code><code>.cc</code> <code>.c++</code> 结尾的文件名假设是 C++,并且文件名结尾在 <code>.m</code> <code>.mm</code> 中被假定在目标 C 中。
* ''cpparg'' is an argument for the C preprocessor, and is anything starting with <code>-I</code>, <code>-D</code>, <code>-U</code> or <code>-C</code>.
+
* ''cpparg'' C 预处理器的参数,是任何以 <code>-I</code><code>-D</code><code>-U</code> <code>-C</code> 开头的东西。
* ''library'' is anything ending in <code>.a</code> or beginning with <code>-l</code> or <code>-L</code>.
+
* ''library'' 是以 <code>.a</code> 结尾或以 <code>-l</code> <code>-L</code> 开头的任何内容。
  
If a particular platform requires a special library on your platform, you can
+
如果特定平台需要在您的平台上使用特殊库,您可以通过编辑 <code>Setup</code> 文件并运行 <code>python setup.py build</code> 来添加它。 例如,如果由行定义的模块
add it by editing the <code>Setup</code> file and running <code>python setup.py build</code>.
 
For example, if the module defined by the line
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,178行: 第870行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>foo foomodule.c</pre>
+
<pre class="none">foo foomodule.c</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
must be linked with the math library <code>libm.a</code> on your platform, simply add
+
必须与平台上的数学库 <code>libm.a</code> 链接,只需将 <code>-lm</code> 添加到该行:
<code>-lm</code> to the line:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,190行: 第881行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>foo foomodule.c -lm</pre>
+
<pre class="none">foo foomodule.c -lm</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Arbitrary switches intended for the compiler or the linker can be supplied with
+
用于编译器或链接器的任意开关可以与 <code>-Xcompiler</code> ''arg'' <code>-Xlinker</code> ''arg'' 选项一起提供:
the <code>-Xcompiler</code> ''arg'' and <code>-Xlinker</code> ''arg'' options:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,202行: 第892行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>foo foomodule.c -Xcompiler -o32 -Xlinker -shared -lm</pre>
+
<pre class="none">foo foomodule.c -Xcompiler -o32 -Xlinker -shared -lm</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The next option after <code>-Xcompiler</code> and <code>-Xlinker</code> will be
+
<code>-Xcompiler</code> <code>-Xlinker</code> 之后的下一个选项将被附加到正确的命令行中,因此在上面的示例中,编译器将传递 <code>-o32</code> 选项,并且链接器将通过 <code>-shared</code>。 如果编译器选项需要参数,则必须提供多个 <code>-Xcompiler</code> 选项; 例如,要传递 <code>-x c++</code><code>Setup</code> 文件必须包含 <code>-Xcompiler -x -Xcompiler c++</code>
appended to the proper command line, so in the above example the compiler will
 
be passed the <code>-o32</code> option, and the linker will be passed
 
<code>-shared</code>. If a compiler option requires an argument, you'll have to
 
supply multiple <code>-Xcompiler</code> options; for example, to pass <code>-x c++</code>
 
the <code>Setup</code> file would have to contain <code>-Xcompiler -x -Xcompiler c++</code>.
 
  
Compiler flags can also be supplied through setting the <span id="index-9" class="target"></span><code>CFLAGS</code>
+
也可以通过设置 <span id="index-9" class="target"></span><code>CFLAGS</code> 环境变量来提供编译器标志。 如果设置,<span id="index-10" class="target"></span><code>CFLAGS</code> 的内容将被添加到 <code>Setup</code> 文件中指定的编译器标志中。
environment variable. If set, the contents of <span id="index-10" class="target"></span><code>CFLAGS</code> will be added to
 
the compiler flags specified in the <code>Setup</code> file.
 
  
  
第1,223行: 第906行:
  
 
<span id="inst-non-ms-compilers"></span>
 
<span id="inst-non-ms-compilers"></span>
=== Using non-Microsoft compilers on Windows ===
+
=== 在 Windows 上使用非 Microsoft 编译器 ===
  
 
<div id="borland-codegear-c" class="section">
 
<div id="borland-codegear-c" class="section">
第1,229行: 第912行:
 
==== Borland/CodeGear C++ ====
 
==== Borland/CodeGear C++ ====
  
This subsection describes the necessary steps to use Distutils with the Borland
+
本小节描述了在 Borland C++ 编译器 5.5 版中使用 Distutils 的必要步骤。 首先,您必须知道 Borland 的目标文件格式 (OMF) 与您可以从 Python ActiveState 网站下载的 Python 版本使用的格式不同。 (Python 是用 Microsoft Visual C++ 构建的,它使用 COFF 作为目标文件格式。)因此,您必须将 Python 的库 <code>python25.lib</code> 转换为 Borland 格式。 您可以按如下方式执行此操作:
C++ compiler version 5.5. First you have to know that Borland's object file
 
format (OMF) is different from the format used by the Python version you can
 
download from the Python or ActiveState Web site. (Python is built with
 
Microsoft Visual C++, which uses COFF as the object file format.) For this
 
reason you have to convert Python's library <code>python25.lib</code> into the
 
Borland format. You can do this as follows:
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,241行: 第918行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>coff2omf python25.lib python25_bcpp.lib</pre>
+
<pre class="none">coff2omf python25.lib python25_bcpp.lib</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The <code>coff2omf</code> program comes with the Borland compiler. The file
+
<code>coff2omf</code> 程序与 Borland 编译器一起提供。 文件 <code>python25.lib</code> 位于 Python 安装的 <code>Libs</code> 目录中。 如果您的扩展使用其他库(zlib,...),您也必须转换它们。
<code>python25.lib</code> is in the <code>Libs</code> directory of your Python
 
installation. If your extension uses other libraries (zlib, ...) you have to
 
convert them too.
 
  
The converted files have to reside in the same directories as the normal
+
转换后的文件必须与普通库位于相同的目录中。
libraries.
 
  
How does Distutils manage to use these libraries with their changed names? If
+
Distutils 如何设法使用这些已更改名称的库? 如果扩展需要一个库(例如。 <code>foo</code>) Distutils 首先检查是否找到后缀为 <code>_bcpp</code> 的库(例如。 <code>foo_bcpp.lib</code>) 然后使用这个库。 如果它没有找到这样一个特殊的库,它会使用默认名称 (<code>foo.lib</code>) [[#id4|1]]
the extension needs a library (eg. <code>foo</code>) Distutils checks first if it
 
finds a library with suffix <code>_bcpp</code> (eg. <code>foo_bcpp.lib</code>) and then
 
uses this library. In the case it doesn't find such a special library it uses
 
the default name (<code>foo.lib</code>.) [[#id4|1]]
 
  
To let Distutils compile your extension with Borland C++ you now have to type:
+
要让 Distutils 使用 Borland C++ 编译您的扩展,您现在必须键入:
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,266行: 第935行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py build --compiler=bcpp</pre>
+
<pre class="none">python setup.py build --compiler=bcpp</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
If you want to use the Borland C++ compiler as the default, you could specify
+
如果您想使用 Borland C++ 编译器作为默认编译器,您可以在您的个人或系统范围的 Distutils 配置文件中指定它(参见 [[#inst-config-files|Distutils 配置文件]] 部分。)
this in your personal or system-wide configuration file for Distutils (see
 
section [[#inst-config-files|<span class="std std-ref">Distutils Configuration Files</span>]].)
 
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
; [https://www.embarcadero.com/products C++Builder Compiler]
+
; [https://www.embarcadero.com/products C++Builder 编译器]
: Information about the free C++ compiler from Borland, including links to the download pages.
+
: Borland 免费 C++ 编译器的信息,包括下载页面的链接。
; [http://www.cyberus.ca/~g_will/pyExtenDL.shtml Creating Python Extensions Using Borland's Free Compiler]
+
; [http://www.cyberus.ca/~g_will/pyExtenDL.shtml 使用 Borland 的免费编译器创建 Python 扩展]
: Document describing how to use Borland's free command-line C++ compiler to build Python.
+
: 描述如何使用 Borland 的免费命令行 C++ 编译器构建 Python 的文档。
  
  
第1,292行: 第959行:
 
==== GNU C / Cygwin / MinGW ====
 
==== GNU C / Cygwin / MinGW ====
  
This section describes the necessary steps to use Distutils with the GNU C/C++
+
本节描述了在 Cygwin 和 MinGW 发行版中将 Distutils GNU C/C++ 编译器一起使用的必要步骤。 [[#id5|2]] 对于用 Cygwin 构建的 Python 解释器,一切都应该可以工作,无需以下任何步骤。
compilers in their Cygwin and MinGW distributions. [[#id5|2]] For a Python interpreter
 
that was built with Cygwin, everything should work without any of these
 
following steps.
 
  
Not all extensions can be built with MinGW or Cygwin, but many can. Extensions
+
并非所有扩展都可以使用 MinGW Cygwin 构建,但很多都可以。 最有可能不起作用的扩展是那些使用 C++ 或依赖于 Microsoft Visual C 扩展的扩展。
most likely to not work are those that use C++ or depend on Microsoft Visual C
 
extensions.
 
  
To let Distutils compile your extension with Cygwin you have to type:
+
要让 Distutils 使用 Cygwin 编译您的扩展,您必须键入:
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,307行: 第969行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py build --compiler=cygwin</pre>
+
<pre class="none">python setup.py build --compiler=cygwin</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
and for Cygwin in no-cygwin mode [[#id6|3]] or for MinGW type:
+
对于无 cygwin 模式 [[#id6|3]] MinGW 类型的 Cygwin:
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,318行: 第980行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>python setup.py build --compiler=mingw32</pre>
+
<pre class="none">python setup.py build --compiler=mingw32</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
If you want to use any of these options/compilers as default, you should
+
如果你想默认使用这些选项/编译器中的任何一个,你应该考虑将它写在你的个人或系统范围的 Distutils 配置文件中(参见 [[#inst-config-files|Distutils 配置文件]] 部分。)
consider writing it in your personal or system-wide configuration file for
 
Distutils (see section [[#inst-config-files|<span class="std std-ref">Distutils Configuration Files</span>]].)
 
  
 
<div id="older-versions-of-python-and-mingw" class="section">
 
<div id="older-versions-of-python-and-mingw" class="section">
  
===== Older Versions of Python and MinGW =====
+
===== 旧版本的 Python MinGW =====
  
The following instructions only apply if you're using a version of Python
+
以下说明仅适用于使用低于 2.4.1 的 Python 版本和低于 3.0.0 的 MinGW(使用 binutils-2.13.90-20030111-1)。
inferior to 2.4.1 with a MinGW inferior to 3.0.0 (with
 
binutils-2.13.90-20030111-1).
 
  
These compilers require some special libraries. This task is more complex than
+
这些编译器需要一些特殊的库。 这个任务比 Borland C++ 更复杂,因为没有程序来转换库。 首先,您必须创建 Python DLL 导出的符号列表。 (您可以在 https://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/ 找到适合此任务的好程序)。
for Borland's C++, because there is no program to convert the library. First
 
you have to create a list of symbols which the Python DLL exports. (You can find
 
a good program for this task at
 
https://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/).
 
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,345行: 第999行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>pexports python25.dll &gt;python25.def</pre>
+
<pre class="none">pexports python25.dll &gt;python25.def</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The location of an installed <code>python25.dll</code> will depend on the
+
已安装的 <code>python25.dll</code> 的位置将取决于安装选项以及 Windows 的版本和语言。 在“只为我”安装中,它将出现在安装目录的根目录中。 在共享安装中,它将位于系统目录中。
installation options and the version and language of Windows. In a &quot;just for
 
me&quot; installation, it will appear in the root of the installation directory. In
 
a shared installation, it will be located in the system directory.
 
  
Then you can create from these information an import library for gcc.
+
然后您可以根据这些信息为 gcc 创建一个导入库。
  
 
<div class="highlight-none notranslate">
 
<div class="highlight-none notranslate">
第1,361行: 第1,012行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>/cygwin/bin/dlltool --dllname python25.dll --def python25.def --output-lib libpython25.a</pre>
+
<pre class="none">/cygwin/bin/dlltool --dllname python25.dll --def python25.def --output-lib libpython25.a</pre>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The resulting library has to be placed in the same directory as
+
生成的库必须放置在与 <code>python25.lib</code> 相同的目录中。 (应该是你的Python安装目录下的<code>libs</code>目录。)
<code>python25.lib</code>. (Should be the <code>libs</code> directory under your Python
 
installation directory.)
 
  
If your extension uses other libraries (zlib,...) you might have to convert
+
如果您的扩展程序使用其他库(zlib,...),您可能也必须转换它们。 转换后的文件必须与普通库位于相同的目录中。
them too. The converted files have to reside in the same directories as the
 
normal libraries do.
 
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
; [http://old.zope.org/Members/als/tips/win32_mingw_modules Building Python modules on MS Windows platform with MinGW]
+
; [http://old.zope.org/Members/als/tips/win32_mingw_modules 使用 MinGW 在 MS Windows 平台上构建 Python 模块]
: Information about building the required libraries for the MinGW environment.
+
: 有关为 MinGW 环境构建所需库的信息。
  
  
 
</div>
 
</div>
Footnotes
+
脚注
  
 
; <span class="brackets">[[#id1|1]]</span>
 
; <span class="brackets">[[#id1|1]]</span>
: This also means you could replace all existing COFF-libraries with OMF-libraries of the same name.
+
: 这也意味着您可以用同名的 OMF 库替换所有现有的 COFF 库。
 
; <span class="brackets">[[#id2|2]]</span>
 
; <span class="brackets">[[#id2|2]]</span>
: Check https://www.sourceware.org/cygwin/ and http://www.mingw.org/ for more information
+
: 查看 https://www.sourceware.org/cygwin/ 了解更多信息
 
; <span class="brackets">[[#id3|3]]</span>
 
; <span class="brackets">[[#id3|3]]</span>
: Then you have no POSIX emulation available, but you also don't need <code>cygwin1.dll</code>.
+
: 那么您就没有可用的 POSIX 仿真,但您也不需要 <code>cygwin1.dll</code>
 +
 
  
 +
</div>
  
 
</div>
 
</div>
第1,400行: 第1,049行:
  
 
</div>
 
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

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

安装 Python 模块(旧版)

作者
格雷格·沃德

也可以看看

安装 Python 模块
最新的模块安装文档。 对于常规 Python 使用,您几乎肯定需要该文档而不是这个文档。


笔记

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


笔记

本指南仅涵盖作为此版本 Python 的一部分提供的用于构建和分发扩展的基本工具。 第三方工具提供更易于使用和更安全的替代方案。 有关更多信息,请参阅 Python Packaging User Guide 中的 快速建议部分


介绍

在 Python 2.0 中,distutils API 首先被添加到标准库中。 这为 Linux 发行版维护人员提供了将 Python 项目转换为 Linux 发行版包的标准方法,并为系统管理员提供了将它们直接安装到目标系统的标准方法。

在 Python 2.0 发布多年以来,构建系统和包安装程序与语言运行时发布周期的紧密耦合已经证明是有问题的,现在建议项目使用 pip 包安装程序和setuptools构建系统,而不是直接使用distutils

有关更多详细信息,请参阅 安装 Python 模块分发 Python 模块

仅在我们确信 setuptools 文档涵盖所需的所有内容之前,才会保留此旧文档。

基于 Distutils 的源代码分发

如果你下载一个模块源代码分发,你可以很快知道它是否以标准方式打包和分发,即 使用 Distutils。 首先,发行版的名称和版本号将在下载的存档名称中突出显示,例如 foo-1.0.tar.gzwidget-0.9.7.zip。 接下来,存档将解压到一个类似名称的目录中:foo-1.0widget-0.9.7。 此外,该发行版将包含一个安装脚本 setup.py 和一个名为 README.txt 或可能只是 README 的文件,这应该说明构建和安装模块发行版是一件简单的事情从终端运行一个命令:

python setup.py install

对于 Windows,此命令应从命令提示符窗口 (Start ‣ Accessories) 运行:

setup.py install

如果所有这些都是真的,那么您已经知道如何构建和安装刚刚下载的模块: 运行上面的命令。 除非您需要以非标准方式安装东西或自定义构建过程,否则您实际上并不需要这本手册。 或者更确切地说,上面的命令是您离开本手册所需的一切。


标准构建和安装

基于Distutils的源代码分发部分所述,使用Distutils构建和安装模块分发通常是从终端运行的一个简单命令:

python setup.py install

平台变化

您应该始终从发行版根目录运行 setup 命令,即 模块源分发解包到的顶级子目录。 例如,如果您刚刚将模块源代码分发 foo-1.0.tar.gz 下载到 Unix 系统上,通常要做的事情是:

gunzip -c foo-1.0.tar.gz | tar xf -    # unpacks into directory foo-1.0
cd foo-1.0
python setup.py install

在 Windows 上,您可能会下载 foo-1.0.zip。 如果你下载了存档文件到C:\Temp,那么它会解压到C:\Temp\foo-1.0; 您可以使用带有图形用户界面的存档操作器(例如 WinZip)或命令行工具(例如 unzippkunzip)来解压存档。 然后,打开命令提示符窗口并运行:

cd c:\Temp\foo-1.0
python setup.py install

拆分工作

运行 setup.py install 会在一次运行中构建和安装所有模块。 如果您更喜欢渐进式工作(如果您想自定义构建过程或出现问题时尤其有用),您可以使用设置脚本一次只做一件事。 当构建和安装将由不同的用户完成时,这特别有用——例如,您可能想要构建一个模块分发并将其交给系统管理员进行安装(或自己完成,具有超级用户权限)。

例如,您可以一步构建所有内容,然后通过调用安装脚本两次,在第二步安装所有内容:

python setup.py build
python setup.py install

如果这样做,您会注意到运行 install 命令首先运行 build 命令,在这种情况下,它很快就会注意到它无关紧要,因为build 目录是最新的。

如果您所做的只是安装从 'net 下载的模块,您可能不需要这种经常分解事物的能力,但它对于更高级的任务非常方便。 如果您开始分发自己的 Python 模块和扩展,您将自行运行许多单独的 Distutils 命令。


建筑如何运作

如上所述,build 命令负责将要安装的文件放入 构建目录 。 默认情况下,这是分布根目录下的 build; 如果您过分关注速度,或者想要保持源代码树的原始状态,您可以使用 --build-base 选项更改构建目录。 例如:

python setup.py build --build-base=/path/to/pybuild/foo-1.0

(或者,您可以使用系统或个人 Distutils 配置文件中的指令永久执行此操作;请参阅部分 Distutils 配置文件 。)通常,这不是必需的。

构建树的默认布局如下:

--- build/ --- lib/
or
--- build/ --- lib.<plat>/
               temp.<plat>/

其中 <plat> 扩展为对当前操作系统/硬件平台和 Python 版本的简要描述。 第一种形式,只有一个 lib 目录,用于“纯模块分发”——即只包含纯 Python 模块的模块分发。 如果模块分发包含任何扩展(用 C/C++ 编写的模块),则使用带有两个 <plat> 目录的第二种形式。 在这种情况下,temp.plat 目录包含编译/链接过程生成的临时文件,但实际上并未安装。 在任一情况下,lib(或 lib.plat)目录包含将安装的所有 Python 模块(纯 Python 和扩展)。

将来,将添加更多目录来处理 Python 脚本、文档、二进制可执行文件以及处理安装 Python 模块和应用程序工作所需的任何其他内容。


安装工作原理

build命令运行后(不管你是显式运行,还是install命令帮你搞定),install命令的工作就比较简单了:它所要做的就是将 build/lib(或 build/lib.plat)下的所有内容复制到您选择的安装目录。

如果你没有选择安装目录——即,如果你只是运行 setup.py install——那么 install 命令会安装到第三方 Python 模块的标准位置。 此位置因平台以及您构建/安装 Python 本身的方式而异。 在 Unix(以及同样基于 Unix 的 macOS)上,它还取决于所安装的模块发行版是纯 Python 还是包含扩展(“非纯”):

平台 标准安装位置 默认值 笔记
Unix(纯) prefix/lib/pythonX.Y/site-packages /usr/local/lib/pythonX.Y/site-packages (1)
Unix(非纯) exec-prefix/lib/pythonX.Y/site-packages /usr/local/lib/pythonX.Y/site-packages (1)
视窗 prefix\Lib\site-packages C:\PythonXY\Lib\site-packages (2)

笔记:

  1. 大多数 Linux 发行版都包含 Python 作为系统的标准部分,因此 prefixexec-prefix 在 Linux 上通常都是 /usr。 如果你自己在 Linux(或任何类 Unix 系统)上构建 Python,默认的 prefixexec-prefix/usr/local
  2. Windows 上的默认安装目录是 C:\Program Files\Python 在 Python 1.6a1、1.5.2 和更早版本下。

prefixexec-prefix 代表 Python 安装到的目录,以及它在运行时找到它的库的位置。 它们在 Windows 下总是相同的,在 Unix 和 macOS 下也经常相同。 通过在交互模式下运行 Python 并键入一些简单的命令,您可以找出 Python 安装用于 prefixexec-prefix 的内容。 在 Unix 下,只需在 shell 提示符下键入 python。 在 Windows 下,选择 开始 ‣ 程序 ‣ Python XY ‣ Python(命令行)。 解释器启动后,您可以在提示符下键入 Python 代码。 例如,在我的 Linux 系统上,我键入如下所示的三个 Python 语句,并得到如图所示的输出,以找出我的 prefixexec-prefix

Python 2.4 (#26, Aug  7 2004, 17:19:02)
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/usr'
>>> sys.exec_prefix
'/usr'

本文档中使用了其他一些占位符: X.Y 代表 Python 的版本,例如 3.2abiflags 将替换为 sys.abiflags 的值或未定义 ABI 标志的平台的空字符串; distname 将替换为正在安装的模块发行版的名称。 点和大写在路径中很重要; 例如,在 UNIX 上使用 python3.2 的值通常在 Windows 上使用 Python32

如果您不想将模块安装到标准位置,或者您没有在那里写入的权限,那么您需要阅读 备用安装 部分中的备用安装。 如果您想更多地自定义您的安装目录,请参阅关于自定义安装的 自定义安装 部分。


替代安装

通常,需要或希望将模块安装到第三方 Python 模块的标准位置以外的位置。 例如,在 Unix 系统上,您可能没有写入标准第三方模块目录的权限。 或者,您可能希望先试用某个模块,然后再将其作为本地 Python 安装的标准部分。 在升级已经存在的发行版时尤其如此:在实际升级之前,您希望确保现有的脚本库仍然适用于新版本。

Distutils install 命令旨在使将模块发行版安装到备用位置变得简单而轻松。 基本思想是为安装提供一个基本目录,install 命令在此基本目录下选择一组目录(称为 安装方案 ),在其中安装文件. 详细信息因平台而异,因此请阅读以下适用于您的部分。

请注意,各种替代安装方案是相互排斥的:您可以通过 --user,或 --home,或 --prefix--exec-prefix,或 --install-base ] 和 --install-platbase,但您不能从这些组中混音。

替代安装:用户方案

该方案旨在为没有全局站点包目录写入权限或不想安装到其中的用户提供最方便的解决方案。 它通过一个简单的选项启用:

python setup.py install --user

文件将被安装到 site.USER_BASE 的子目录中(以下写为 userbase)。 此方案将纯 Python 模块和扩展模块安装在同一位置(也称为 site.USER_SITE)。 以下是 UNIX(包括 macOS)的值:

文件类型 安装目录
模块 userbase/lib/pythonX.Y/site-packages
脚本 userbase/bin
数据 userbase
C 头文件 userbase/include/pythonX.Yabiflags/distname

以下是在 Windows 上使用的值:

文件类型 安装目录
模块 userbase\PythonXY\site-packages
脚本 userbase\PythonXY\Scripts
数据 userbase
C 头文件 userbase\PythonXY\Include{distname}

与下面描述的其他方案相比,使用此方案的优点是用户站点包目录在正常情况下始终包含在 sys.path 中(有关更多信息,请参阅 site) ,这意味着在运行 setup.py 脚本完成安装后无需执行额外的步骤。

build_ext 命令还有一个 --user 选项,可以将 userbase/include 添加到头文件的编译器搜索路径,将 userbase/lib 添加到库的编译器搜索路径,如以及共享 C 库的运行时搜索路径 (rpath)。


替代安装:家庭方案

“家庭计划”背后的想法是您构建和维护 Python 模块的个人存储。 该方案的名称源自 Unix 上“主”目录的概念,因为 Unix 用户使他们的主目录具有类似于 /usr//usr/local/ 的布局并不少见。 任何人都可以使用此方案,无论他们安装的操作系统是什么。

安装一个新的模块发行版就像

python setup.py install --home=<dir>

您可以在其中为 --home 选项提供您喜欢的任何目录。 在 Unix 上,懒惰的打字员只能输入波浪号 (~); install 命令会将其扩展到您的主目录:

python setup.py install --home=~

要让 Python 找到使用此方案安装的发行版,您可能需要 修改 Python 的搜索路径 或编辑 sitecustomize(参见 site)以调用 site。 addsitedir() 或编辑 sys.path

--home 选项定义安装基础目录。 文件安装到安装库下的以下目录如下:

文件类型 安装目录
模块 home/lib/python
脚本 home/bin
数据 home
C 头文件 home/include/python/distname

(如果您使用的是 Windows,请在心里用反斜杠替换斜杠。)


替代安装:Unix(前缀方案)

当您希望使用一个 Python 安装来执行构建/安装(即运行安装脚本),但将模块安装到另一个 Python 安装的第三方模块目录(或其他看起来像不同的 Python 安装)。 如果这听起来有点不寻常,那就是 - 这就是用户和家庭计划出现的原因。 然而,至少有两种已知的情况下前缀方案将是有用的。

首先,考虑到许多 Linux 发行版将 Python 放在 /usr 中,而不是更传统的 /usr/local。 这是完全合适的,因为在这些情况下,Python 是“系统”的一部分,而不是本地附加组件。 但是,如果您从源代码安装 Python 模块,您可能希望它们进入 /usr/local/lib/python2.X 而不是 /usr/lib/python2.X。 这可以用

/usr/bin/python setup.py install --prefix=/usr/local

另一种可能性是网络文件系统,其中用于写入远程目录的名称与用于读取它的名称不同:例如,作为 /usr/local/bin/python 访问的 Python 解释器可能会在 /usr/local/lib/python2.X 中搜索模块],但这些模块必须安装到 /mnt/@server/export/lib/python2.X。 这可以用

/usr/local/bin/python setup.py install --prefix=/mnt/@server/export

在任一情况下,--prefix 选项定义安装基础,--exec-prefix 选项定义平台特定的安装基础,用于特定平台的文件。 (目前,这仅表示非纯模块发行版,但可以扩展为 C 库、二进制可执行文件等)如果未提供 --exec-prefix,则默认为 --prefix。 文件安装如下:

文件类型 安装目录
Python 模块 prefix/lib/pythonX.Y/site-packages
扩展模块 exec-prefix/lib/pythonX.Y/site-packages
脚本 prefix/bin
数据 prefix
C 头文件 prefix/include/pythonX.Yabiflags/distname

不要求 --prefix--exec-prefix 实际上指向备用 Python 安装; 如果上面列出的目录尚不存在,则会在安装时创建它们。

顺便说一句,前缀方案很重要的真正原因很简单,标准的 Unix 安装使用前缀方案,但是 --prefix--exec-prefix 由 Python 本身作为 sys.prefix 和 [ X205X]。 因此,您可能认为您永远不会使用前缀方案,但是每次在没有任何其他选项的情况下运行 python setup.py install 时,您都在使用它。

请注意,将扩展安装到备用 Python 安装对这些扩展的构建方式没有影响:特别是,将使用与用于运行安装脚本的 Python 解释器一起安装的 Python 头文件(Python.h 和朋友)在编译扩展。 您有责任确保用于运行以这种方式安装的扩展的解释器与用于构建它们的解释器兼容。 最好的方法是确保两个解释器是相同版本的 Python(可能是不同的构建,或者可能是相同构建的副本)。 (当然,如果您的 --prefix--exec-prefix 甚至没有指向备用 Python 安装,这无关紧要。)


替代安装:Windows(前缀方案)

Windows 没有用户主目录的概念,而且由于 Windows 下的标准 Python 安装比 Unix 下简单,--prefix 选项传统上用于在 Windows 上的不同位置安装附加包。

python setup.py install --prefix="\Temp\Python"

将模块安装到当前驱动器上的 \Temp\Python 目录。

安装基础由--prefix选项定义; Windows 下不支持 --exec-prefix 选项,这意味着纯 Python 模块和扩展模块安装在同一位置。 文件安装如下:

文件类型 安装目录
模块 prefix\Lib\site-packages
脚本 prefix\Scripts
数据 prefix
C 头文件 prefix\Include{distname}


自定义安装

有时,在 替代安装 部分中描述的替代安装方案并不能满足您的要求。 您可能只想调整一两个目录,同时将所有内容保留在同一个基本目录下,或者您可能想要完全重新定义安装方案。 无论哪种情况,您都在创建 自定义安装方案

要创建自定义安装方案,您可以从备用方案之一开始,并使用以下选项覆盖用于各种类型文件的一些安装目录:

文件类型 覆盖选项
Python 模块 --install-purelib
扩展模块 --install-platlib
所有模块 --install-lib
脚本 --install-scripts
数据 --install-data
C 头文件 --install-headers

这些覆盖选项可以是相对的、绝对的或根据安装基础目录之一明确定义的。 (有两个安装基础目录,它们通常是相同的——它们仅在您使用 Unix“前缀方案”并提供不同的 --prefix--exec-prefix 选项时有所不同;使用 [ X196X] 将覆盖为 --install-purelib--install-platlib 计算或给出的值,建议用于在 Python 和扩展模块之间没有区别的方案。)

例如,假设您要在 Unix 下将模块分发版安装到您的主目录,但您希望脚本进入 ~/scripts 而不是 ~/bin。 如您所料,您可以使用 --install-scripts 选项覆盖此目录; 在这种情况下,提供相对路径最有意义,该路径将相对于安装基目录(在这种情况下是您的主目录)进行解释:

python setup.py install --home=~ --install-scripts=scripts

另一个 Unix 示例:假设您的 Python 安装是使用 /usr/local/python 前缀构建和安装的,因此在标准安装脚本下,脚本将以 /usr/local/python/bin 结尾。 如果你希望它们在 /usr/local/bin 中,你可以为 --install-scripts 选项提供这个绝对目录:

python setup.py install --install-scripts=/usr/local/bin

(这使用“前缀方案”执行安装,在这种情况下,前缀是安装 Python 解释器的任何内容 - /usr/local/python。)

如果您在 Windows 上维护 Python,您可能希望第三方模块位于 prefix 的子目录中,而不是直接位于 prefix 本身中。 这几乎和自定义脚本安装目录一样简单——你只需要记住有两种类型的模块需要担心,Python 和扩展模块,它们都可以通过一个选项方便地控制:

python setup.py install --install-lib=Site

指定的安装目录相对于prefix。 当然,您还必须确保该目录在 Python 的模块搜索路径中,例如将 .pth 文件放在站点目录中(请参阅 site)。 请参阅部分 修改 Python 的搜索路径 以了解如何修改 Python 的搜索路径。

如果要定义整个安装方案,只需提供所有安装目录选项。 推荐的方法是提供相对路径; 例如,如果您想在您的主目录中维护 python 下所有与 Python 模块相关的文件,并且您希望为您使用主目录的每个平台都有一个单独的目录,您可以定义以下安装方案:

python setup.py install --home=~ \
                        --install-purelib=python/lib \
                        --install-platlib=python/lib.$PLAT \
                        --install-scripts=python/scripts
                        --install-data=python/data

或者,等效地,

python setup.py install --home=~/python \
                        --install-purelib=lib \
                        --install-platlib='lib.$PLAT' \
                        --install-scripts=scripts
                        --install-data=data

$PLAT 不是(必然)环境变量——它会在 Distutils 解析您的命令行选项时进行扩展,就像解析您的配置文件时一样。

显然,每次安装新的模块发行版时都指定整个安装方案会非常乏味。 因此,您可以将这些选项放入您的 Distutils 配置文件中(请参阅 Distutils 配置文件 部分):

[install]
install-base=$HOME
install-purelib=python/lib
install-platlib=python/lib.$PLAT
install-scripts=python/scripts
install-data=python/data

或者,等效地,

[install]
install-base=$HOME/python
install-purelib=lib
install-platlib=lib.$PLAT
install-scripts=scripts
install-data=data

请注意,如果您在运行安装脚本时提供不同的安装基目录,则这两个 不是 等效。 例如,

python setup.py install --install-base=/tmp

在第一种情况下将纯模块安装到 /tmp/python/lib,在第二种情况下安装到 /tmp/lib。 (对于第二种情况,您可能需要提供 /tmp/python 的安装基础。)

您可能注意到在示例配置文件输入中使用了 $HOME$PLAT。 这些是 Distutils 配置变量,它们与环境变量非常相似。 实际上,您可以在具有这种概念的平台上的配置文件中使用环境变量,但 Distutils 还定义了一些可能不在您的环境中的额外变量,例如 $PLAT。 (当然,在没有环境变量的系统上,例如 Mac OS 9,Distutils 提供的配置变量是您唯一可以使用的。)有关详细信息,请参阅 Distutils 配置文件 部分.

笔记

当一个虚拟环境被激活时,所有distutils配置文件中任何改变安装路径的选项都会被忽略,以防止无意中将项目安装到虚拟环境之外。


修改 Python 的搜索路径

当 Python 解释器执行 import 语句时,它会沿着搜索路径搜索 Python 代码和扩展模块。 在构建解释器时,路径的默认值被配置到 Python 二进制文件中。 您可以通过导入sys模块并打印sys.path的值来确定路径。

$ python
Python 2.2 (#11, Oct  3 2002, 13:31:27)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
 '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
 '/usr/local/lib/python2.3/site-packages']
>>>

sys.path 中的空字符串表示当前工作目录。

本地安装包的预期约定是将它们放在 …/site-packages/ 目录中,但您可能希望将 Python 模块安装到某个任意目录中。 例如,您的站点可能有一个约定,将与 Web 服务器相关的所有软件保留在 /www 下。 附加 Python 模块可能属于 /www/python,为了导入它们,必须将此目录添加到 sys.path。 有几种不同的方法可以添加目录。

最方便的方法是将路径配置文件添加到已经在 Python 路径上的目录中,通常是添加到 .../site-packages/ 目录中。 路径配置文件的扩展名为 .pth,每行必须包含一个将附加到 sys.path 的路径。 (因为新路径附加到 sys.path,添加目录中的模块不会覆盖标准模块。 这意味着您不能使用此机制来安装标准模块的固定版本。)

路径可以是绝对的或相对的,在这种情况下,它们相对于包含 .pth 文件的目录。 有关更多信息,请参阅 site 模块的文档。

稍微不太方便的方法是编辑Python标准库中的site.py文件,修改sys.pathsite.py 在 Python 解释器执行时自动导入,除非提供 -S 开关来抑制这种行为。 因此,您可以简单地编辑 site.py 并向其添加两行:

import sys
sys.path.append('/www/python/')

但是,如果您重新安装 Python 的相同主要版本(例如,从 2.2 升级到 2.2.2 时)site.py 将被库存版本覆盖。 您必须记住它已被修改并在安装之前保存副本。

有两个环境变量可以修改sys.pathPYTHONHOME 设置 Python 安装前缀的替代值。 例如,如果 PYTHONHOME 设置为 /www/python,则搜索路径将设置为 [, '/www/python/lib/pythonX.Y/', '/www/python/lib/pythonX.Y/plat-linux2', ...]

PYTHONPATH 变量可以设置为将添加到 sys.path 开头的路径列表。 例如,如果 PYTHONPATH 设置为 /www/python:/opt/py,则搜索路径将以 ['/www/python', '/opt/py'] 开头。 (请注意,目录必须存在才能添加到 sys.pathsite 模块删除不存在的路径。)

最后,sys.path 只是一个常规的 Python 列表,因此任何 Python 应用程序都可以通过添加或删除条目来修改它。


Distutils 配置文件

如上所述,您可以使用 Distutils 配置文件来记录任何 Distutils 选项的个人或站点首选项。 也就是说,任何命令的任何选项都可以存储在两个或三个(取决于您的平台)配置文件之一中,在解析命令行之前将参考这些文件。 这意味着配置文件将覆盖默认值,而命令行将依次覆盖配置文件。 此外,如果应用多个配置文件,“较早”文件中的值将被“较晚”文件覆盖。

配置文件的位置和名称

配置文件的名称和位置因平台而异。 在 Unix 和 macOS 上,三个配置文件(按处理顺序)是:

文件类型 位置和文件名 笔记
系统 prefix/lib/pythonver/distutils/distutils.cfg (1)
个人的 $HOME/.pydistutils.cfg (2)
当地的 setup.cfg (3)

在 Windows 上,配置文件是:

文件类型 位置和文件名 笔记
系统 prefix\Lib\distutils\distutils.cfg (4)
个人的 %HOME%\pydistutils.cfg (5)
当地的 setup.cfg (3)

在所有平台上,可以通过传递 –no-user-cfg 选项来暂时禁用“个人”文件。

笔记:

  1. 严格来说,系统范围的配置文件位于安装 Distutils 的目录中; 在 Python 1.6 及更高版本的 Unix 下,如图所示。 对于 Python 1.5.2,Distutils 通常会安装到 prefix/lib/python1.5/site-packages/distutils,所以系统配置文件应该放在 Python 1.5.2 下。
  2. 在 Unix 上,如果 HOME 环境变量未定义,则用户的主目录将由标准 pwd 模块中的 getpwuid() 函数确定. 这是由 Distutils 使用的 os.path.expanduser() 函数完成的。
  3. 即,在当前目录中(通常是安装脚本的位置)。
  4. (另请参见注释(1)。)在 Python 1.6 及更高版本下,Python 的默认“安装前缀”为 C:\Python,因此系统配置文件通常为 C:\Python\Lib\distutils\distutils.cfg。 在 Python 1.5.2 下,默认前缀是 C:\Program Files\Python,并且 Distutils 不是标准库的一部分——因此在标准 Python 1.5.2 安装中,系统配置文件将是 C:\Program Files\Python\distutils\distutils.cfg视窗。
  5. 在 Windows 上,如果 HOME 环境变量未定义,则 USERPROFILE 然后 HOMEDRIVE 和 [将尝试 X122X]HOMEPATH。 这是由 Distutils 使用的 os.path.expanduser() 函数完成的。


配置文件的语法

Distutils 配置文件都具有相同的语法。 配置文件分为几个部分。 每个 Distutils 命令都有一个部分,另外还有一个 global 部分用于影响每个命令的全局选项。 每个部分每行包含一个选项,指定为 option=value

例如,下面是一个完整的配置文件,它只是强制所有命令默认安静运行:

[global]
verbose=0

如果它作为系统配置文件安装,它将影响当前系统上任何用户对任何 Python 模块分发的所有处理。 如果它作为您的个人配置文件安装(在支持它们的系统上),它只会影响您处理的模块分发。 如果它被用作特定模块分发的 setup.cfg,它只会影响该分发。

您可以覆盖默认的“build base”目录,并使 build* 命令始终使用以下命令强制重建所有文件:

[build]
build-base=blib
force=1

对应于命令行参数

python setup.py build --build-base=blib --force

除了在命令行中包含 build 命令意味着该命令将被运行。 在配置文件中包含特定命令没有这样的含义; 这仅意味着如果运行该命令,则配置文件中的选项将适用。 (或者,如果运行其他从中派生值的命令,它们将使用配置文件中的值。)

您可以使用 --help 选项找到任何命令的完整选项列表,例如:

python setup.py build --help

您可以通过使用 --help 而无需命令来查找全局选项的完整列表:

python setup.py --help

另请参阅“分发 Python 模块”手册的“参考”部分。


构建扩展:提示和技巧

只要有可能,Distutils 就会尝试使用由用于运行 setup.py 脚本的 Python 解释器提供的配置信息。 例如,用于编译 Python 的相同编译器和链接器标志也将用于编译扩展。 通常这会很有效,但在复杂的情况下这可能不合适。 本节讨论如何覆盖通常的 Distutils 行为。

调整编译器/链接器标志

编译用 C 或 C++ 编写的 Python 扩展有时需要为编译器和链接器指定自定义标志,以便使用特定库或生成特殊类型的目标代码。 如果扩展尚未在您的平台上进行测试,或者您正在尝试交叉编译 Python,则尤其如此。

在最一般的情况下,扩展作者可能已经预见到编译扩展会很复杂,并提供了一个 Setup 文件供您编辑。 这可能只有在模块分发包含许多单独的扩展模块,或者如果它们经常需要精心设计的编译器标志集才能工作时才会这样做。

解析 Setup 文件(如果存在)以获得要构建的扩展列表。 Setup 中的每一行描述了一个模块。 行具有以下结构:

module ... [sourcefile ...] [cpparg ...] [library ...]

让我们依次检查每个字段。

  • module 是要构建的扩展模块的名称,应该是一个有效的 Python 标识符。 您不能仅仅为了重命名模块而更改它(还需要对源代码进行编辑),因此应该将其保留。
  • sourcefile 是任何可能是源代码文件的东西,至少从文件名来看是这样。 假设以 .c 结尾的文件名是用 C 编写的,以 .C.cc.c++ 结尾的文件名假设是 C++,并且文件名结尾在 .m.mm 中被假定在目标 C 中。
  • cpparg 是 C 预处理器的参数,是任何以 -I-D-U-C 开头的东西。
  • library 是以 .a 结尾或以 -l-L 开头的任何内容。

如果特定平台需要在您的平台上使用特殊库,您可以通过编辑 Setup 文件并运行 python setup.py build 来添加它。 例如,如果由行定义的模块

foo foomodule.c

必须与平台上的数学库 libm.a 链接,只需将 -lm 添加到该行:

foo foomodule.c -lm

用于编译器或链接器的任意开关可以与 -Xcompiler arg-Xlinker arg 选项一起提供:

foo foomodule.c -Xcompiler -o32 -Xlinker -shared -lm

-Xcompiler-Xlinker 之后的下一个选项将被附加到正确的命令行中,因此在上面的示例中,编译器将传递 -o32 选项,并且链接器将通过 -shared。 如果编译器选项需要参数,则必须提供多个 -Xcompiler 选项; 例如,要传递 -x c++Setup 文件必须包含 -Xcompiler -x -Xcompiler c++

也可以通过设置 CFLAGS 环境变量来提供编译器标志。 如果设置,CFLAGS 的内容将被添加到 Setup 文件中指定的编译器标志中。


在 Windows 上使用非 Microsoft 编译器

Borland/CodeGear C++

本小节描述了在 Borland C++ 编译器 5.5 版中使用 Distutils 的必要步骤。 首先,您必须知道 Borland 的目标文件格式 (OMF) 与您可以从 Python 或 ActiveState 网站下载的 Python 版本使用的格式不同。 (Python 是用 Microsoft Visual C++ 构建的,它使用 COFF 作为目标文件格式。)因此,您必须将 Python 的库 python25.lib 转换为 Borland 格式。 您可以按如下方式执行此操作:

coff2omf python25.lib python25_bcpp.lib

coff2omf 程序与 Borland 编译器一起提供。 文件 python25.lib 位于 Python 安装的 Libs 目录中。 如果您的扩展使用其他库(zlib,...),您也必须转换它们。

转换后的文件必须与普通库位于相同的目录中。

Distutils 如何设法使用这些已更改名称的库? 如果扩展需要一个库(例如。 foo) Distutils 首先检查是否找到后缀为 _bcpp 的库(例如。 foo_bcpp.lib) 然后使用这个库。 如果它没有找到这样一个特殊的库,它会使用默认名称 (foo.lib。) 1

要让 Distutils 使用 Borland C++ 编译您的扩展,您现在必须键入:

python setup.py build --compiler=bcpp

如果您想使用 Borland C++ 编译器作为默认编译器,您可以在您的个人或系统范围的 Distutils 配置文件中指定它(参见 Distutils 配置文件 部分。)

也可以看看

C++Builder 编译器
Borland 免费 C++ 编译器的信息,包括下载页面的链接。
使用 Borland 的免费编译器创建 Python 扩展
描述如何使用 Borland 的免费命令行 C++ 编译器构建 Python 的文档。


GNU C / Cygwin / MinGW

本节描述了在 Cygwin 和 MinGW 发行版中将 Distutils 与 GNU C/C++ 编译器一起使用的必要步骤。 2 对于用 Cygwin 构建的 Python 解释器,一切都应该可以工作,无需以下任何步骤。

并非所有扩展都可以使用 MinGW 或 Cygwin 构建,但很多都可以。 最有可能不起作用的扩展是那些使用 C++ 或依赖于 Microsoft Visual C 扩展的扩展。

要让 Distutils 使用 Cygwin 编译您的扩展,您必须键入:

python setup.py build --compiler=cygwin

对于无 cygwin 模式 3 或 MinGW 类型的 Cygwin:

python setup.py build --compiler=mingw32

如果你想默认使用这些选项/编译器中的任何一个,你应该考虑将它写在你的个人或系统范围的 Distutils 配置文件中(参见 Distutils 配置文件 部分。)

旧版本的 Python 和 MinGW

以下说明仅适用于使用低于 2.4.1 的 Python 版本和低于 3.0.0 的 MinGW(使用 binutils-2.13.90-20030111-1)。

这些编译器需要一些特殊的库。 这个任务比 Borland 的 C++ 更复杂,因为没有程序来转换库。 首先,您必须创建 Python DLL 导出的符号列表。 (您可以在 https://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/ 找到适合此任务的好程序)。

pexports python25.dll >python25.def

已安装的 python25.dll 的位置将取决于安装选项以及 Windows 的版本和语言。 在“只为我”安装中,它将出现在安装目录的根目录中。 在共享安装中,它将位于系统目录中。

然后您可以根据这些信息为 gcc 创建一个导入库。

/cygwin/bin/dlltool --dllname python25.dll --def python25.def --output-lib libpython25.a

生成的库必须放置在与 python25.lib 相同的目录中。 (应该是你的Python安装目录下的libs目录。)

如果您的扩展程序使用其他库(zlib,...),您可能也必须转换它们。 转换后的文件必须与普通库位于相同的目录中。

也可以看看

使用 MinGW 在 MS Windows 平台上构建 Python 模块
有关为 MinGW 环境构建所需库的信息。


脚注

1
这也意味着您可以用同名的 OMF 库替换所有现有的 COFF 库。
2
查看 https://www.sourceware.org/cygwin/ 了解更多信息
3
那么您就没有可用的 POSIX 仿真,但您也不需要 cygwin1.dll