Files
hustoj/web/python-docs/distutils/builtdist.html
2025-03-09 17:10:56 +08:00

639 lines
46 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>5. 创建构建分发版 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="6. Distutils 示例" href="examples.html" />
<link rel="prev" title="4. 创建源代码分发包" href="sourcedist.html" />
<link rel="canonical" href="https://docs.python.org/3/distutils/builtdist.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">5. 创建构建分发版</a><ul>
<li><a class="reference internal" href="#creating-rpm-packages">5.1. 创建RPM软件包</a></li>
<li><a class="reference internal" href="#creating-windows-installers">5.2. Creating Windows Installers</a></li>
<li><a class="reference internal" href="#cross-compiling-on-windows">5.3. 在 Windows 上的交叉编译</a><ul>
<li><a class="reference internal" href="#the-postinstallation-script">5.3.1. 安装后脚本</a></li>
</ul>
</li>
<li><a class="reference internal" href="#vista-user-access-control-uac">5.4. Vista User Access Control (UAC)</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="sourcedist.html"
title="上一章"><span class="section-number">4. </span>创建源代码分发包</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="examples.html"
title="下一章"><span class="section-number">6. </span>Distutils 示例</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/distutils/builtdist.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/distutils/builtdist.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="examples.html" title="6. Distutils 示例"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="sourcedist.html" title="4. 创建源代码分发包"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">分发 Python 模块(遗留版本)</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="creating-built-distributions">
<span id="built-dist"></span><h1><span class="section-number">5. </span>创建构建分发版<a class="headerlink" href="#creating-built-distributions" title="永久链接至标题"></a></h1>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>这篇文档是历史遗留文档,在 <a class="reference external" href="https://setuptools.readthedocs.io/en/latest/setuptools.html">https://setuptools.readthedocs.io/en/latest/setuptools.html</a> 上的 <code class="docutils literal notranslate"><span class="pre">setuptools</span></code> 文档独立涵盖此处包含的所有相关信息之后,将不再单独作为正式文档保留。</p>
</div>
<p>“构建分发版”即你通常所认为的“二进制包”或“安装程序”(取决于你的技术背景)。 但它并不一定是二进制的,因为它可能只包含 Python 源代码和/或字节码;并且我们也不将其称为包,因为这个词在 Python 中已经被使用。 (而“安装程序”是主流桌面系统领域的一个专有术语。)</p>
<p>分发版可以让你的模块分发安装器尽可能地方便易用:对于基于 RPM 的 Linux 系统的用户,它将为二进制 RPM对于 Windows 的用户,它将为可执行文件安装器;对于基于 Debian 的 Linux 用户,它将为 Debian 安装包;诸如此类。 显然,没有人能为世界上所有的系统平台都构建分发版,因此 Distutils 被设计用来让模块开发者能够专注于他们的专长 --- 编写代码并创建源代码分发版 --- 而让另一群作为中介的 <em>打包者</em> 负责在尽可能多的受支持系统平台上将源代码分发版转换为构建分发版。</p>
<p>当然,模块开发者可以是自己模块的打包者;或者打包者也可以是住在别处的“某一位”能够接触到原始开发者接触不到的特定系统平台的志愿者;或者还可以是一个定期抓取新的源代码分发版并在尽可能多的受支持系统平台上将其转换为构建分发版的软件。 无论他们属于哪一种,打包者都会使用设置脚本和 <strong class="command">bdist</strong> 命令族来生成构建分发版。</p>
<p>作为一个简单示例,如果我在 Distutils 源代码树中运行以下命令:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">bdist</span>
</pre></div>
</div>
<p>则 Distutils 将构建我的模块分发版(在此情况下即 Distutils 本身),执行“模拟”安装(同样是在 <code class="file docutils literal notranslate"><span class="pre">build</span></code> 目录中),并为我的系统平台创建默认类型的构建分发版。 构建分发版的默认格式在 Unix 上是一个“非自动” tar 文件,而在 Windows 上则是一个简单的可执行文件安装器。 (此 tar 文件被称为“非自动”是因为它必须在指定的位置上解包方可使用。)</p>
<p>这样,上述命令在 Unix 系统上将会创建 <code class="file docutils literal notranslate"><span class="pre">Distutils-1.0.</span><em><span class="pre">plat</span></em><span class="pre">.tar.gz</span></code>;在正确的位置上解包这个 tar 文件将会装好 Distutils就像是你下载了源代码分发版并运行 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">install</span></code> 一样。 (这个“正确的位置”可能是文件系统根目录或 Python 的 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em></code> 目录,具体取决于提供给 <strong class="command">bdist_dumb</strong> 命令的选项;在默认情况下是相对于 <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em></code> 创建非自动颁发版。)</p>
<p>显然,对于纯粹的 Python 分发版来说,这并不比运行 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">install</span></code> 更简单 --- 但是对于包括需要被编译的扩展的非纯粹的分发版来说,就可能是有人能使用你的扩展而有人不能使用的差别。 而创建“自动”构建分发版,例如 RPM 包或 Windows 的可执行文件安装器,对于用户来说就会更加方便,即使你的分发版不包括任何扩展。</p>
<p><strong class="command">bdist</strong> 命令有一个 <code class="xref std std-option docutils literal notranslate"><span class="pre">--formats</span></code> 选项,与 <strong class="command">sdist</strong> 命令类似,你可以用该选项来选择要生成的构建分发版类型:例如,:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">bdist</span> <span class="o">--</span><span class="nb">format</span><span class="o">=</span><span class="nb">zip</span>
</pre></div>
</div>
<p>当在 Unix 系统上运行时,会再次创建 <code class="file docutils literal notranslate"><span class="pre">Distutils-1.0.</span><em><span class="pre">plat</span></em><span class="pre">.zip</span></code>,这个归档文件将从根目录被解包以安装 Distutils。</p>
<p>构建分发版的可用格式有:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 25%" />
<col style="width: 58%" />
<col style="width: 17%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>格式</p></th>
<th class="head"><p>描述</p></th>
<th class="head"><p>备注</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">gztar</span></code></p></td>
<td><p>gzipped tar 文件 (<code class="file docutils literal notranslate"><span class="pre">.tar.gz</span></code>)</p></td>
<td><p>(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">bztar</span></code></p></td>
<td><p>bzipped tar 文件 (<code class="file docutils literal notranslate"><span class="pre">.tar.bz2</span></code>)</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">xztar</span></code></p></td>
<td><p>xzipped tar 文件 (<code class="file docutils literal notranslate"><span class="pre">.tar.xz</span></code>)</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">ztar</span></code></p></td>
<td><p>带压缩的 tar 文件 (<code class="file docutils literal notranslate"><span class="pre">.tar.Z</span></code>)</p></td>
<td><p>(3)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">tar</span></code></p></td>
<td><p>tar 文件 (<code class="file docutils literal notranslate"><span class="pre">.tar</span></code>)</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">zip</span></code></p></td>
<td><p>zip 文件 (<code class="file docutils literal notranslate"><span class="pre">.zip</span></code>)</p></td>
<td><p>(2),(4)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">rpm</span></code></p></td>
<td><p>RPM</p></td>
<td><p>(5)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">pkgtool</span></code></p></td>
<td><p>Solaris <strong class="program">pkgtool</strong></p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">sdux</span></code></p></td>
<td><p>HP-UX <strong class="program">swinstall</strong></p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">wininst</span></code></p></td>
<td><p>Windows的自解压ZIP文件</p></td>
<td><p>(4)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">msi</span></code></p></td>
<td><p>Microsoft安装程序。</p></td>
<td></td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>添加了对 <code class="docutils literal notranslate"><span class="pre">xztar</span></code> 格式的支持</p>
</div>
<p>注释:</p>
<ol class="arabic simple">
<li><p>默认 Unix</p></li>
<li><p>默认Windows</p></li>
<li><p>需要外部 <strong class="program">compress</strong> 工具。</p></li>
<li><p>需要有外部 <strong class="program">zip</strong> 工具或 <a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> 模块(自 Python 1.6 起是标准 Python 库的一部分)</p></li>
<li><p>需要有外部 <strong class="program">rpm</strong> 工具,版本号为 3.0.4 或以上(可使用 <code class="docutils literal notranslate"><span class="pre">rpm</span> <span class="pre">--version</span></code> 查看你所使用的版本)</p></li>
</ol>
<p>你不必附带 <code class="xref std std-option docutils literal notranslate"><span class="pre">--formats</span></code> 来使用 <strong class="command">bdist</strong> 命令;你还可以使用直接实现了你想要的特定格式的命令。 某些这样的 <strong class="command">bdist</strong> &quot;子命令&quot; 实际上会生成几种相似的格式;例如,<strong class="command">bdist_dumb</strong> 命令将生成所有 &quot;非自动&quot; 归档格式 (<code class="docutils literal notranslate"><span class="pre">tar</span></code>, <code class="docutils literal notranslate"><span class="pre">gztar</span></code>, <code class="docutils literal notranslate"><span class="pre">bztar</span></code>, <code class="docutils literal notranslate"><span class="pre">xztar</span></code>, <code class="docutils literal notranslate"><span class="pre">ztar</span></code><code class="docutils literal notranslate"><span class="pre">zip</span></code>),而 <strong class="command">bdist_rpm</strong> 将同时生成二进制和源代码 RPM。 <strong class="command">bdist</strong> 子命令以及每个子命令所生成的格式如下:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 41%" />
<col style="width: 59%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>命令</p></th>
<th class="head"><p>格式</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><strong class="command">bdist_dumb</strong></p></td>
<td><p>tar, gztar, bztar, xztar, ztar, zip</p></td>
</tr>
<tr class="row-odd"><td><p><strong class="command">bdist_rpm</strong></p></td>
<td><p>rpm, srpm</p></td>
</tr>
<tr class="row-even"><td><p><strong class="command">bdist_wininst</strong></p></td>
<td><p>wininst</p></td>
</tr>
<tr class="row-odd"><td><p><strong class="command">bdist_msi</strong></p></td>
<td><p>msi</p></td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>从Python 3.8开始不推荐使用 bdist_wininst</p>
</div>
<p>以下小节提供了每个 <strong class="command">bdist_*</strong> 命令的详情。</p>
<section id="creating-rpm-packages">
<span id="creating-rpms"></span><h2><span class="section-number">5.1. </span>创建RPM软件包<a class="headerlink" href="#creating-rpm-packages" title="永久链接至标题"></a></h2>
<p>RPM 格式被许多流行的 Linux 发行版所使用,包括 Red Hat, SuSE 和 Mandrake。 如果其中(或任何其他基于 RPM 的 Linux 发行版)的某一个是你的常用环境,那么为相同发行版的其他用户创建 RPM 包是很容易的。 根据你的模块分发版的复杂度以及 Linux 发行版之间的差异性,你还可能创建适用于多个基于 RPM 的发行版的 RPM 包。</p>
<p>为你的模块分发版创建 RPM 的通常方式是运行 <strong class="command">bdist_rpm</strong> 命令:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">bdist_rpm</span>
</pre></div>
</div>
<p>或者 <strong class="command">bdist</strong> 命令附带 <code class="xref std std-option docutils literal notranslate"><span class="pre">--format</span></code> 选项:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">bdist</span> <span class="o">--</span><span class="n">formats</span><span class="o">=</span><span class="n">rpm</span>
</pre></div>
</div>
<p>前者允许你指定 RPM 专属的选项;后者允许你方便地一次性指定多种格式。 如果这两样你全都要,你可以显式地指定多个 <strong class="command">bdist_*</strong> 命令及其选项:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">bdist_rpm</span> <span class="o">--</span><span class="n">packager</span><span class="o">=</span><span class="s2">&quot;John Doe &lt;jdoe@example.org&gt;&quot;</span> \
<span class="n">bdist_wininst</span> <span class="o">--</span><span class="n">target</span><span class="o">-</span><span class="n">version</span><span class="o">=</span><span class="s2">&quot;2.0&quot;</span>
</pre></div>
</div>
<p>创建 RPM 包的操作是由 <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件驱动的,就像 Distutils 的使用是由 setup 脚本驱动的一样。 为了让你更方便,<strong class="command">bdist_rpm</strong> 命令通常会根据你在 setup 脚本、命令行和任意 Distutils 配置文件中提供的信息创建一个 <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件。 <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件中的各种选项和小节从 setup 脚本中派生的情况如下:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 48%" />
<col style="width: 52%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>RPM <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件配置或选项</p></th>
<th class="head"><p>Distutils安装脚本选项</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>名称</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">name</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>摘要(在序言中)</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">description</span></code></p></td>
</tr>
<tr class="row-even"><td><p>版本</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">version</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>供应商</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">author</span></code><code class="docutils literal notranslate"><span class="pre">author_email</span></code>, 或 --- &amp; <code class="docutils literal notranslate"><span class="pre">maintainer</span></code><code class="docutils literal notranslate"><span class="pre">maintainer_email</span></code></p></td>
</tr>
<tr class="row-even"><td><p>版权所有</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">license</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>Url</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">url</span></code></p></td>
</tr>
<tr class="row-even"><td><p>%d描述部分</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">long_description</span></code></p></td>
</tr>
</tbody>
</table>
<p>此外,在 <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件中还有许多选项在 setup 脚本中没有对应的选项。 这些选项大多是通过传给 <strong class="command">bdist_rpm</strong> 命令的选项来处理的,如下所示:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 36%" />
<col style="width: 34%" />
<col style="width: 29%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>RPM <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件配置或选项</p></th>
<th class="head"><p><strong class="command">bdist_rpm</strong> 选项</p></th>
<th class="head"><p>默认值</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>发布版本</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">release</span></code></p></td>
<td><p>&quot;1&quot;</p></td>
</tr>
<tr class="row-odd"><td><p>组织</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">group</span></code></p></td>
<td><p>&quot;Development/Libraries&quot;</p></td>
</tr>
<tr class="row-even"><td><p>供应商</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">vendor</span></code></p></td>
<td><p>(同上)</p></td>
</tr>
<tr class="row-odd"><td><p>打包</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">packager</span></code></p></td>
<td><p>(none)</p></td>
</tr>
<tr class="row-even"><td><p>提供</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">provides</span></code></p></td>
<td><p>(none)</p></td>
</tr>
<tr class="row-odd"><td><p>需求</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">requires</span></code></p></td>
<td><p>(none)</p></td>
</tr>
<tr class="row-even"><td><p>冲突</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">conflicts</span></code></p></td>
<td><p>(none)</p></td>
</tr>
<tr class="row-odd"><td><p>淘汰</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">obsoletes</span></code></p></td>
<td><p>(none)</p></td>
</tr>
<tr class="row-even"><td><p>Distribution</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">distribution_name</span></code></p></td>
<td><p>(none)</p></td>
</tr>
<tr class="row-odd"><td><p>构建要求</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">build_requires</span></code></p></td>
<td><p>(none)</p></td>
</tr>
<tr class="row-even"><td><p>Icon</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">icon</span></code></p></td>
<td><p>(none)</p></td>
</tr>
</tbody>
</table>
<p>显然,即使是在命令行中提供少量的此类选项也是很繁琐易出错的,因此通常最好是将它们放在 setup 配置文件 <code class="file docutils literal notranslate"><span class="pre">setup.cfg</span></code> 中,参见 <a class="reference internal" href="configfile.html#setup-config"><span class="std std-ref">编写设置脚本的配置文件</span></a> 一节。 如果你要分发或打包许多 Python 模块分发版,你可能会需要将适用于所有这些分发版的选项放在你私人的 Distutils 配置文件中 (<code class="file docutils literal notranslate"><span class="pre">~/.pydistutils.cfg</span></code>)。 如果你想要临时禁用此文件,你可以将 <code class="xref std std-option docutils literal notranslate"><span class="pre">--no-user-cfg</span></code> 选项传给 <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code></p>
<p>构建一个二进制 RPM 包有三个步骤,它们全都是由 Distutils 自动处理的:</p>
<ol class="arabic simple">
<li><p>创建一个 <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件,该文件对包进行了描述(类似于 Distutils setup 脚本;实际上 setup 脚本中的许多信息都会出现在 <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件中)</p></li>
<li><p>创建源 RPM</p></li>
<li><p>创建“二进制”RPM其中可能包含二进制代码也可能不包含具体取决于你的模块分发版是否包含 Python 扩展)</p></li>
</ol>
<p>通常RPM 会将后两个步骤捆绑在一起;当你使用 Distutils 时,三个步骤通常都会捆绑在一起。</p>
<p>如果你愿意,你也可以将这三个步骤分开。 你可以使用 <code class="xref std std-option docutils literal notranslate"><span class="pre">--spec-only</span></code> 选项来让 <strong class="command">bdist_rpm</strong> 只创建 <code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件并退出;在这种情况下,<code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件将被写到“分发目录” --- 通常为 <code class="file docutils literal notranslate"><span class="pre">dist/</span></code>,但可通过 <code class="xref std std-option docutils literal notranslate"><span class="pre">--dist-dir</span></code> 选项来自定义。 (通常,<code class="file docutils literal notranslate"><span class="pre">.spec</span></code> 文件会位于“构建树”的深处,在 <strong class="command">bdist_rpm</strong> 所创建的一个临时目录中。)</p>
</section>
<section id="creating-windows-installers">
<span id="creating-wininst"></span><h2><span class="section-number">5.2. </span>Creating Windows Installers<a class="headerlink" href="#creating-windows-installers" title="永久链接至标题"></a></h2>
<div class="admonition warning">
<p class="admonition-title">警告</p>
<p>从Python 3.8开始不推荐使用 bdist_wininst</p>
</div>
<p>Executable installers are the natural format for binary distributions on
Windows. They display a nice graphical user interface, display some information
about the module distribution to be installed taken from the metadata in the
setup script, let the user select a few options, and start or cancel the
installation.</p>
<p>Since the metadata is taken from the setup script, creating Windows installers
is usually as easy as running:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">bdist_wininst</span>
</pre></div>
</div>
<p>or the <strong class="command">bdist</strong> command with the <code class="xref std std-option docutils literal notranslate"><span class="pre">--formats</span></code> option:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">bdist</span> <span class="o">--</span><span class="n">formats</span><span class="o">=</span><span class="n">wininst</span>
</pre></div>
</div>
<p>If you have a pure module distribution (only containing pure Python modules and
packages), the resulting installer will be version independent and have a name
like <code class="file docutils literal notranslate"><span class="pre">foo-1.0.win32.exe</span></code>. Note that creating <code class="docutils literal notranslate"><span class="pre">wininst</span></code> binary
distributions in only supported on Windows systems.</p>
<p>If you have a non-pure distribution, the extensions can only be created on a
Windows platform, and will be Python version dependent. The installer filename
will reflect this and now has the form <code class="file docutils literal notranslate"><span class="pre">foo-1.0.win32-py2.0.exe</span></code>. You
have to create a separate installer for every Python version you want to
support.</p>
<p>The installer will try to compile pure modules into <a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a> after installation
on the target system in normal and optimizing mode. If you don't want this to
happen for some reason, you can run the <strong class="command">bdist_wininst</strong> command with
the <code class="xref std std-option docutils literal notranslate"><span class="pre">--no-target-compile</span></code> and/or the <code class="xref std std-option docutils literal notranslate"><span class="pre">--no-target-optimize</span></code>
option.</p>
<p>By default the installer will display the cool &quot;Python Powered&quot; logo when it is
run, but you can also supply your own 152x261 bitmap which must be a Windows
<code class="file docutils literal notranslate"><span class="pre">.bmp</span></code> file with the <code class="xref std std-option docutils literal notranslate"><span class="pre">--bitmap</span></code> option.</p>
<p>The installer will also display a large title on the desktop background window
when it is run, which is constructed from the name of your distribution and the
version number. This can be changed to another text by using the
<code class="xref std std-option docutils literal notranslate"><span class="pre">--title</span></code> option.</p>
<p>The installer file will be written to the &quot;distribution directory&quot; --- normally
<code class="file docutils literal notranslate"><span class="pre">dist/</span></code>, but customizable with the <code class="xref std std-option docutils literal notranslate"><span class="pre">--dist-dir</span></code> option.</p>
</section>
<section id="cross-compiling-on-windows">
<span id="cross-compile-windows"></span><h2><span class="section-number">5.3. </span>在 Windows 上的交叉编译<a class="headerlink" href="#cross-compiling-on-windows" title="永久链接至标题"></a></h2>
<p>从 Python 2.6 开始distutils 能够在不同 Windows 平台之间执行交叉编译。 实际上,这意味着只要安装了正确的工具,你可以使用 32 位版 Windows 来创建 64 位的扩展或是反向操作。</p>
<p>要针对替代平台进行编译,请为构建命令指定 <code class="xref std std-option docutils literal notranslate"><span class="pre">--plat-name</span></code> 选项。 目前的有效值为 'win32' 和d 'win-amd64'。 例如,在 32 位版 Windows 上,你可以执行:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">build</span> <span class="o">--</span><span class="n">plat</span><span class="o">-</span><span class="n">name</span><span class="o">=</span><span class="n">win</span><span class="o">-</span><span class="n">amd64</span>
</pre></div>
</div>
<p>to build a 64bit version of your extension. The Windows Installers also
support this option, so the command:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">build</span> <span class="o">--</span><span class="n">plat</span><span class="o">-</span><span class="n">name</span><span class="o">=</span><span class="n">win</span><span class="o">-</span><span class="n">amd64</span> <span class="n">bdist_wininst</span>
</pre></div>
</div>
<p>将在你的 32 位版 Windows 上创建一个 64 位版的安装程序可执行文件。</p>
<p>要进行交叉编译,你必须下载 Python 源代码并针对你的目标平台交叉编译 Python 本身 —— 使用 Python 的二进制安装版是无法做到的(因为其中不包括针对其他平台的 .lib 等文件。) 实际上,这意味着 32 位操作系统的用户将需要使用 Visual Studio 2008 来打开 Python 源代码目录树下的 <code class="file docutils literal notranslate"><span class="pre">PCbuild/PCbuild.sln</span></code> 解决方案并构建 'pythoncore' 项目的 &quot;x64&quot; 配置之后才能进行扩展的交叉编译。</p>
<p>请注意在默认情况下Visual Studio 2008 并不会安装 64 位编译器或工具。 你可能需要重新执行 Visual Studio 安装过程并选择这些工具(使用控制面板 -&gt; [添加/移除] 程序是检查或修改你的现有安装的一个便捷方式。)</p>
<section id="the-postinstallation-script">
<span id="postinstallation-script"></span><h3><span class="section-number">5.3.1. </span>安装后脚本<a class="headerlink" href="#the-postinstallation-script" title="永久链接至标题"></a></h3>
<p>从 Python 2.3 开始,可以通过 <code class="xref std std-option docutils literal notranslate"><span class="pre">--install-script</span></code> 选项指定一个安装后脚本。 必须要指定脚本的主文件名,并且该脚本文件名还必须在传给 setup 函数的参数中列出。</p>
<p>在安装时当所有文件拷贝完毕后该脚本将会在目标系统上运行,并将 <code class="docutils literal notranslate"><span class="pre">argv[1]</span></code> 设为 <code class="xref std std-option docutils literal notranslate"><span class="pre">-install</span></code>,而在卸载时当文件被移除前会再次运行并将 <code class="docutils literal notranslate"><span class="pre">argv[1]</span></code> 设为 <code class="xref std std-option docutils literal notranslate"><span class="pre">-remove</span></code></p>
<p>安装脚本嵌入在 Windows 安装程序中运行,每个输出 (<code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>) 都会被重定向到一个缓冲区并将在脚本完成后显示到 GUI 中。</p>
<p>一些在此上下文中特别有用的功能在安装脚本中作为附加内置函数被提供。</p>
<dl class="function">
<dt id="directory_created">
<code class="sig-name descname">directory_created</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#directory_created" title="永久链接至目标"></a></dt>
<dt id="file_created">
<code class="sig-name descname">file_created</code><span class="sig-paren">(</span><em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#file_created" title="永久链接至目标"></a></dt>
<dd><p>这些函数应当在安装时的安装后脚本创建某个目录或文件时被调用。 它会将 <em>path</em> 注册到卸载程序,这样当分发版被卸载时它将被移除。 安全起见,目录只有在为空时才会被移除。</p>
</dd></dl>
<dl class="function">
<dt id="get_special_folder_path">
<code class="sig-name descname">get_special_folder_path</code><span class="sig-paren">(</span><em class="sig-param">csidl_string</em><span class="sig-paren">)</span><a class="headerlink" href="#get_special_folder_path" title="永久链接至目标"></a></dt>
<dd><p>此函数可被用来获取 Windows 中的特殊文件夹位置如 Start Menu 或 Desktop。 它将返回相应文件夹的完整路径。 <em>csidl_string</em> 必须为下列字符串之一:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="s2">&quot;CSIDL_APPDATA&quot;</span>
<span class="s2">&quot;CSIDL_COMMON_STARTMENU&quot;</span>
<span class="s2">&quot;CSIDL_STARTMENU&quot;</span>
<span class="s2">&quot;CSIDL_COMMON_DESKTOPDIRECTORY&quot;</span>
<span class="s2">&quot;CSIDL_DESKTOPDIRECTORY&quot;</span>
<span class="s2">&quot;CSIDL_COMMON_STARTUP&quot;</span>
<span class="s2">&quot;CSIDL_STARTUP&quot;</span>
<span class="s2">&quot;CSIDL_COMMON_PROGRAMS&quot;</span>
<span class="s2">&quot;CSIDL_PROGRAMS&quot;</span>
<span class="s2">&quot;CSIDL_FONTS&quot;</span>
</pre></div>
</div>
<p>如果文件夹不能被检索到,会触发 <a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a></p>
<p>有哪些可用的文件夹取决于的 Windows 的具体版本,并可能受特定配置的影响。 更多细节请参考 Microsoft 有关 <code class="xref c c-func docutils literal notranslate"><span class="pre">SHGetSpecialFolderPath()</span></code> 函数的文档。</p>
</dd></dl>
<dl class="function">
<dt id="create_shortcut">
<code class="sig-name descname">create_shortcut</code><span class="sig-paren">(</span><em class="sig-param">target</em>, <em class="sig-param">description</em>, <em class="sig-param">filename</em><span class="optional">[</span>, <em class="sig-param">arguments</em><span class="optional">[</span>, <em class="sig-param">workdir</em><span class="optional">[</span>, <em class="sig-param">iconpath</em><span class="optional">[</span>, <em class="sig-param">iconindex</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#create_shortcut" title="永久链接至目标"></a></dt>
<dd><p>此函数会创建一个快捷方式。 <em>target</em> 是此快捷方式要启动的程序的路径。 <em>description</em> 是快捷方式的描述文本。 <em>filename</em> 是用户将看到的快捷方式的标题。 <em>arguments</em> 指定命令行参数,如果有的话。 <em>workdir</em> 是程序的工作目录。 <em>iconpath</em> 是包含快捷方式的图标的文件,而 <em>iconindex</em><em>iconpath</em> 文件中图标的索引号。 同样地,更多细节请参考 Microsoft 有关 <code class="xref py py-class docutils literal notranslate"><span class="pre">IShellLink</span></code> 接口的文档。</p>
</dd></dl>
</section>
</section>
<section id="vista-user-access-control-uac">
<h2><span class="section-number">5.4. </span>Vista User Access Control (UAC)<a class="headerlink" href="#vista-user-access-control-uac" title="永久链接至标题"></a></h2>
<p>Starting with Python 2.6, bdist_wininst supports a <code class="xref std std-option docutils literal notranslate"><span class="pre">--user-access-control</span></code>
option. The default is 'none' (meaning no UAC handling is done), and other
valid values are 'auto' (meaning prompt for UAC elevation if Python was
installed for all users) and 'force' (meaning always prompt for elevation).</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>从Python 3.8开始不推荐使用 bdist_wininst</p>
</div>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">5. 创建构建分发版</a><ul>
<li><a class="reference internal" href="#creating-rpm-packages">5.1. 创建RPM软件包</a></li>
<li><a class="reference internal" href="#creating-windows-installers">5.2. Creating Windows Installers</a></li>
<li><a class="reference internal" href="#cross-compiling-on-windows">5.3. 在 Windows 上的交叉编译</a><ul>
<li><a class="reference internal" href="#the-postinstallation-script">5.3.1. 安装后脚本</a></li>
</ul>
</li>
<li><a class="reference internal" href="#vista-user-access-control-uac">5.4. Vista User Access Control (UAC)</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="sourcedist.html"
title="上一章"><span class="section-number">4. </span>创建源代码分发包</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="examples.html"
title="下一章"><span class="section-number">6. </span>Distutils 示例</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/distutils/builtdist.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="examples.html" title="6. Distutils 示例"
>下一页</a> |</li>
<li class="right" >
<a href="sourcedist.html" title="4. 创建源代码分发包"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >分发 Python 模块(遗留版本)</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>