gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

650 line
44KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="zipapp — Manage executable Python zip archives" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/zipapp.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/zipapp.py This module provides tools to manage the creation of zip files containing Python code, which can be executed directly by the Python interpreter. The module provides both ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/zipapp.py This module provides tools to manage the creation of zip files containing Python code, which can be executed directly by the Python interpreter. The module provides both ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>zipapp — Manage executable Python zip archives &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Python Runtime Services" href="python.html" />
  33. <link rel="prev" title="venv — Creation of virtual environments" href="venv.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/zipapp.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" 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"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code> — Manage executable Python zip archives</a><ul>
  86. <li><a class="reference internal" href="#basic-example">Basic Example</a></li>
  87. <li><a class="reference internal" href="#command-line-interface">Command-Line Interface</a></li>
  88. <li><a class="reference internal" href="#python-api">Python API</a></li>
  89. <li><a class="reference internal" href="#examples">Examples</a></li>
  90. <li><a class="reference internal" href="#specifying-the-interpreter">Specifying the Interpreter</a></li>
  91. <li><a class="reference internal" href="#creating-standalone-applications-with-zipapp">Creating Standalone Applications with zipapp</a><ul>
  92. <li><a class="reference internal" href="#caveats">Caveats</a></li>
  93. </ul>
  94. </li>
  95. <li><a class="reference internal" href="#the-python-zip-application-archive-format">The Python Zip Application Archive Format</a></li>
  96. </ul>
  97. </li>
  98. </ul>
  99. </div>
  100. <div>
  101. <h4>Previous topic</h4>
  102. <p class="topless"><a href="venv.html"
  103. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code> — Creation of virtual environments</a></p>
  104. </div>
  105. <div>
  106. <h4>Next topic</h4>
  107. <p class="topless"><a href="python.html"
  108. title="next chapter">Python Runtime Services</a></p>
  109. </div>
  110. <div role="note" aria-label="source link">
  111. <h3>This Page</h3>
  112. <ul class="this-page-menu">
  113. <li><a href="../bugs.html">Report a Bug</a></li>
  114. <li>
  115. <a href="https://github.com/python/cpython/blob/main/Doc/library/zipapp.rst"
  116. rel="nofollow">Show Source
  117. </a>
  118. </li>
  119. </ul>
  120. </div>
  121. </nav>
  122. </div>
  123. </div>
  124. <div class="related" role="navigation" aria-label="related navigation">
  125. <h3>Navigation</h3>
  126. <ul>
  127. <li class="right" style="margin-right: 10px">
  128. <a href="../genindex.html" title="General Index"
  129. accesskey="I">index</a></li>
  130. <li class="right" >
  131. <a href="../py-modindex.html" title="Python Module Index"
  132. >modules</a> |</li>
  133. <li class="right" >
  134. <a href="python.html" title="Python Runtime Services"
  135. accesskey="N">next</a> |</li>
  136. <li class="right" >
  137. <a href="venv.html" title="venv — Creation of virtual environments"
  138. accesskey="P">previous</a> |</li>
  139. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  140. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  141. <li class="switchers">
  142. <div class="language_switcher_placeholder"></div>
  143. <div class="version_switcher_placeholder"></div>
  144. </li>
  145. <li>
  146. </li>
  147. <li id="cpython-language-and-version">
  148. <a href="../index.html">3.12.3 Documentation</a> &#187;
  149. </li>
  150. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  151. <li class="nav-item nav-item-2"><a href="distribution.html" accesskey="U">Software Packaging and Distribution</a> &#187;</li>
  152. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code> — Manage executable Python zip archives</a></li>
  153. <li class="right">
  154. <div class="inline-search" role="search">
  155. <form class="inline-search" action="../search.html" method="get">
  156. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  157. <input type="submit" value="Go" />
  158. </form>
  159. </div>
  160. |
  161. </li>
  162. <li class="right">
  163. <label class="theme-selector-label">
  164. Theme
  165. <select class="theme-selector" oninput="activateTheme(this.value)">
  166. <option value="auto" selected>Auto</option>
  167. <option value="light">Light</option>
  168. <option value="dark">Dark</option>
  169. </select>
  170. </label> |</li>
  171. </ul>
  172. </div>
  173. <div class="document">
  174. <div class="documentwrapper">
  175. <div class="bodywrapper">
  176. <div class="body" role="main">
  177. <section id="module-zipapp">
  178. <span id="zipapp-manage-executable-python-zip-archives"></span><h1><a class="reference internal" href="#module-zipapp" title="zipapp: Manage executable Python zip archives"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code></a> — Manage executable Python zip archives<a class="headerlink" href="#module-zipapp" title="Link to this heading">¶</a></h1>
  179. <div class="versionadded">
  180. <p><span class="versionmodified added">New in version 3.5.</span></p>
  181. </div>
  182. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/zipapp.py">Lib/zipapp.py</a></p>
  183. <hr class="docutils" id="index-0" />
  184. <p>This module provides tools to manage the creation of zip files containing
  185. Python code, which can be <a class="reference internal" href="../using/cmdline.html#using-on-interface-options"><span class="std std-ref">executed directly by the Python interpreter</span></a>. The module provides both a
  186. <a class="reference internal" href="#zipapp-command-line-interface"><span class="std std-ref">Command-Line Interface</span></a> and a <a class="reference internal" href="#zipapp-python-api"><span class="std std-ref">Python API</span></a>.</p>
  187. <section id="basic-example">
  188. <h2>Basic Example<a class="headerlink" href="#basic-example" title="Link to this heading">¶</a></h2>
  189. <p>The following example shows how the <a class="reference internal" href="#zipapp-command-line-interface"><span class="std std-ref">Command-Line Interface</span></a>
  190. can be used to create an executable archive from a directory containing
  191. Python code. When run, the archive will execute the <code class="docutils literal notranslate"><span class="pre">main</span></code> function from
  192. the module <code class="docutils literal notranslate"><span class="pre">myapp</span></code> in the archive.</p>
  193. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipapp<span class="w"> </span>myapp<span class="w"> </span>-m<span class="w"> </span><span class="s2">&quot;myapp:main&quot;</span>
  194. <span class="gp">$ </span>python<span class="w"> </span>myapp.pyz
  195. <span class="go">&lt;output from myapp&gt;</span>
  196. </pre></div>
  197. </div>
  198. </section>
  199. <section id="command-line-interface">
  200. <span id="zipapp-command-line-interface"></span><h2>Command-Line Interface<a class="headerlink" href="#command-line-interface" title="Link to this heading">¶</a></h2>
  201. <p>When called as a program from the command line, the following form is used:</p>
  202. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipapp<span class="w"> </span><span class="nb">source</span><span class="w"> </span><span class="o">[</span>options<span class="o">]</span>
  203. </pre></div>
  204. </div>
  205. <p>If <em>source</em> is a directory, this will create an archive from the contents of
  206. <em>source</em>. If <em>source</em> is a file, it should be an archive, and it will be
  207. copied to the target archive (or the contents of its shebang line will be
  208. displayed if the –info option is specified).</p>
  209. <p>The following options are understood:</p>
  210. <dl class="std option">
  211. <dt class="sig sig-object std" id="cmdoption-zipapp-o">
  212. <span id="cmdoption-zipapp-output"></span><span class="sig-name descname"><span class="pre">-o</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;output&gt;</span></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--output</span></span><span class="sig-prename descclassname"><span class="pre">=&lt;output&gt;</span></span><a class="headerlink" href="#cmdoption-zipapp-o" title="Link to this definition">¶</a></dt>
  213. <dd><p>Write the output to a file named <em>output</em>. If this option is not specified,
  214. the output filename will be the same as the input <em>source</em>, with the
  215. extension <code class="docutils literal notranslate"><span class="pre">.pyz</span></code> added. If an explicit filename is given, it is used as
  216. is (so a <code class="docutils literal notranslate"><span class="pre">.pyz</span></code> extension should be included if required).</p>
  217. <p>An output filename must be specified if the <em>source</em> is an archive (and in
  218. that case, <em>output</em> must not be the same as <em>source</em>).</p>
  219. </dd></dl>
  220. <dl class="std option">
  221. <dt class="sig sig-object std" id="cmdoption-zipapp-p">
  222. <span id="cmdoption-zipapp-python"></span><span class="sig-name descname"><span class="pre">-p</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;interpreter&gt;</span></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--python</span></span><span class="sig-prename descclassname"><span class="pre">=&lt;interpreter&gt;</span></span><a class="headerlink" href="#cmdoption-zipapp-p" title="Link to this definition">¶</a></dt>
  223. <dd><p>Add a <code class="docutils literal notranslate"><span class="pre">#!</span></code> line to the archive specifying <em>interpreter</em> as the command
  224. to run. Also, on POSIX, make the archive executable. The default is to
  225. write no <code class="docutils literal notranslate"><span class="pre">#!</span></code> line, and not make the file executable.</p>
  226. </dd></dl>
  227. <dl class="std option">
  228. <dt class="sig sig-object std" id="cmdoption-zipapp-m">
  229. <span id="cmdoption-zipapp-main"></span><span class="sig-name descname"><span class="pre">-m</span></span><span class="sig-prename descclassname"> <span class="pre">&lt;mainfn&gt;</span></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--main</span></span><span class="sig-prename descclassname"><span class="pre">=&lt;mainfn&gt;</span></span><a class="headerlink" href="#cmdoption-zipapp-m" title="Link to this definition">¶</a></dt>
  230. <dd><p>Write a <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> file to the archive that executes <em>mainfn</em>. The
  231. <em>mainfn</em> argument should have the form “pkg.mod:fn”, where “pkg.mod” is a
  232. package/module in the archive, and “fn” is a callable in the given module.
  233. The <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> file will execute that callable.</p>
  234. <p><a class="reference internal" href="#cmdoption-zipapp-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">--main</span></code></a> cannot be specified when copying an archive.</p>
  235. </dd></dl>
  236. <dl class="std option">
  237. <dt class="sig sig-object std" id="cmdoption-zipapp-c">
  238. <span id="cmdoption-zipapp-compress"></span><span class="sig-name descname"><span class="pre">-c</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--compress</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-zipapp-c" title="Link to this definition">¶</a></dt>
  239. <dd><p>Compress files with the deflate method, reducing the size of the output
  240. file. By default, files are stored uncompressed in the archive.</p>
  241. <p><a class="reference internal" href="#cmdoption-zipapp-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">--compress</span></code></a> has no effect when copying an archive.</p>
  242. <div class="versionadded">
  243. <p><span class="versionmodified added">New in version 3.7.</span></p>
  244. </div>
  245. </dd></dl>
  246. <dl class="std option">
  247. <dt class="sig sig-object std" id="cmdoption-zipapp-info">
  248. <span class="sig-name descname"><span class="pre">--info</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-zipapp-info" title="Link to this definition">¶</a></dt>
  249. <dd><p>Display the interpreter embedded in the archive, for diagnostic purposes. In
  250. this case, any other options are ignored and SOURCE must be an archive, not a
  251. directory.</p>
  252. </dd></dl>
  253. <dl class="std option">
  254. <dt class="sig sig-object std" id="cmdoption-zipapp-h">
  255. <span id="cmdoption-zipapp-help"></span><span class="sig-name descname"><span class="pre">-h</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--help</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-zipapp-h" title="Link to this definition">¶</a></dt>
  256. <dd><p>Print a short usage message and exit.</p>
  257. </dd></dl>
  258. </section>
  259. <section id="python-api">
  260. <span id="zipapp-python-api"></span><h2>Python API<a class="headerlink" href="#python-api" title="Link to this heading">¶</a></h2>
  261. <p>The module defines two convenience functions:</p>
  262. <dl class="py function">
  263. <dt class="sig sig-object py" id="zipapp.create_archive">
  264. <span class="sig-prename descclassname"><span class="pre">zipapp.</span></span><span class="sig-name descname"><span class="pre">create_archive</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">target</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">interpreter</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">main</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filter</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compressed</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipapp.create_archive" title="Link to this definition">¶</a></dt>
  265. <dd><p>Create an application archive from <em>source</em>. The source can be any
  266. of the following:</p>
  267. <ul class="simple">
  268. <li><p>The name of a directory, or a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a> referring
  269. to a directory, in which case a new application archive will be
  270. created from the content of that directory.</p></li>
  271. <li><p>The name of an existing application archive file, or a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>
  272. referring to such a file, in which case the file is copied to
  273. the target (modifying it to reflect the value given for the <em>interpreter</em>
  274. argument). The file name should include the <code class="docutils literal notranslate"><span class="pre">.pyz</span></code> extension, if required.</p></li>
  275. <li><p>A file object open for reading in bytes mode. The content of the
  276. file should be an application archive, and the file object is
  277. assumed to be positioned at the start of the archive.</p></li>
  278. </ul>
  279. <p>The <em>target</em> argument determines where the resulting archive will be
  280. written:</p>
  281. <ul class="simple">
  282. <li><p>If it is the name of a file, or a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>,
  283. the archive will be written to that file.</p></li>
  284. <li><p>If it is an open file object, the archive will be written to that
  285. file object, which must be open for writing in bytes mode.</p></li>
  286. <li><p>If the target is omitted (or <code class="docutils literal notranslate"><span class="pre">None</span></code>), the source must be a directory
  287. and the target will be a file with the same name as the source, with
  288. a <code class="docutils literal notranslate"><span class="pre">.pyz</span></code> extension added.</p></li>
  289. </ul>
  290. <p>The <em>interpreter</em> argument specifies the name of the Python
  291. interpreter with which the archive will be executed. It is written as
  292. a “shebang” line at the start of the archive. On POSIX, this will be
  293. interpreted by the OS, and on Windows it will be handled by the Python
  294. launcher. Omitting the <em>interpreter</em> results in no shebang line being
  295. written. If an interpreter is specified, and the target is a
  296. filename, the executable bit of the target file will be set.</p>
  297. <p>The <em>main</em> argument specifies the name of a callable which will be
  298. used as the main program for the archive. It can only be specified if
  299. the source is a directory, and the source does not already contain a
  300. <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> file. The <em>main</em> argument should take the form
  301. “pkg.module:callable” and the archive will be run by importing
  302. “pkg.module” and executing the given callable with no arguments. It
  303. is an error to omit <em>main</em> if the source is a directory and does not
  304. contain a <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> file, as otherwise the resulting archive
  305. would not be executable.</p>
  306. <p>The optional <em>filter</em> argument specifies a callback function that
  307. is passed a Path object representing the path to the file being added
  308. (relative to the source directory). It should return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the
  309. file is to be added.</p>
  310. <p>The optional <em>compressed</em> argument determines whether files are
  311. compressed. If set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, files in the archive are compressed
  312. with the deflate method; otherwise, files are stored uncompressed.
  313. This argument has no effect when copying an existing archive.</p>
  314. <p>If a file object is specified for <em>source</em> or <em>target</em>, it is the
  315. caller’s responsibility to close it after calling create_archive.</p>
  316. <p>When copying an existing archive, file objects supplied only need
  317. <code class="docutils literal notranslate"><span class="pre">read</span></code> and <code class="docutils literal notranslate"><span class="pre">readline</span></code>, or <code class="docutils literal notranslate"><span class="pre">write</span></code> methods. When creating an
  318. archive from a directory, if the target is a file object it will be
  319. passed to the <code class="docutils literal notranslate"><span class="pre">zipfile.ZipFile</span></code> class, and must supply the methods
  320. needed by that class.</p>
  321. <div class="versionchanged">
  322. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>filter</em> and <em>compressed</em> parameters.</p>
  323. </div>
  324. </dd></dl>
  325. <dl class="py function">
  326. <dt class="sig sig-object py" id="zipapp.get_interpreter">
  327. <span class="sig-prename descclassname"><span class="pre">zipapp.</span></span><span class="sig-name descname"><span class="pre">get_interpreter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">archive</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zipapp.get_interpreter" title="Link to this definition">¶</a></dt>
  328. <dd><p>Return the interpreter specified in the <code class="docutils literal notranslate"><span class="pre">#!</span></code> line at the start of the
  329. archive. If there is no <code class="docutils literal notranslate"><span class="pre">#!</span></code> line, return <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.
  330. The <em>archive</em> argument can be a filename or a file-like object open
  331. for reading in bytes mode. It is assumed to be at the start of the archive.</p>
  332. </dd></dl>
  333. </section>
  334. <section id="examples">
  335. <span id="zipapp-examples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  336. <p>Pack up a directory into an archive, and run it.</p>
  337. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipapp<span class="w"> </span>myapp
  338. <span class="gp">$ </span>python<span class="w"> </span>myapp.pyz
  339. <span class="go">&lt;output from myapp&gt;</span>
  340. </pre></div>
  341. </div>
  342. <p>The same can be done using the <a class="reference internal" href="#zipapp.create_archive" title="zipapp.create_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_archive()</span></code></a> function:</p>
  343. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">zipapp</span>
  344. <span class="gp">&gt;&gt;&gt; </span><span class="n">zipapp</span><span class="o">.</span><span class="n">create_archive</span><span class="p">(</span><span class="s1">&#39;myapp&#39;</span><span class="p">,</span> <span class="s1">&#39;myapp.pyz&#39;</span><span class="p">)</span>
  345. </pre></div>
  346. </div>
  347. <p>To make the application directly executable on POSIX, specify an interpreter
  348. to use.</p>
  349. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipapp<span class="w"> </span>myapp<span class="w"> </span>-p<span class="w"> </span><span class="s2">&quot;/usr/bin/env python&quot;</span>
  350. <span class="gp">$ </span>./myapp.pyz
  351. <span class="go">&lt;output from myapp&gt;</span>
  352. </pre></div>
  353. </div>
  354. <p>To replace the shebang line on an existing archive, create a modified archive
  355. using the <a class="reference internal" href="#zipapp.create_archive" title="zipapp.create_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_archive()</span></code></a> function:</p>
  356. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">zipapp</span>
  357. <span class="gp">&gt;&gt;&gt; </span><span class="n">zipapp</span><span class="o">.</span><span class="n">create_archive</span><span class="p">(</span><span class="s1">&#39;old_archive.pyz&#39;</span><span class="p">,</span> <span class="s1">&#39;new_archive.pyz&#39;</span><span class="p">,</span> <span class="s1">&#39;/usr/bin/python3&#39;</span><span class="p">)</span>
  358. </pre></div>
  359. </div>
  360. <p>To update the file in place, do the replacement in memory using a <a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesIO</span></code></a>
  361. object, and then overwrite the source afterwards. Note that there is a risk
  362. when overwriting a file in place that an error will result in the loss of
  363. the original file. This code does not protect against such errors, but
  364. production code should do so. Also, this method will only work if the archive
  365. fits in memory:</p>
  366. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">zipapp</span>
  367. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">io</span>
  368. <span class="gp">&gt;&gt;&gt; </span><span class="n">temp</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">BytesIO</span><span class="p">()</span>
  369. <span class="gp">&gt;&gt;&gt; </span><span class="n">zipapp</span><span class="o">.</span><span class="n">create_archive</span><span class="p">(</span><span class="s1">&#39;myapp.pyz&#39;</span><span class="p">,</span> <span class="n">temp</span><span class="p">,</span> <span class="s1">&#39;/usr/bin/python2&#39;</span><span class="p">)</span>
  370. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;myapp.pyz&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  371. <span class="gp">&gt;&gt;&gt; </span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">temp</span><span class="o">.</span><span class="n">getvalue</span><span class="p">())</span>
  372. </pre></div>
  373. </div>
  374. </section>
  375. <section id="specifying-the-interpreter">
  376. <span id="zipapp-specifying-the-interpreter"></span><h2>Specifying the Interpreter<a class="headerlink" href="#specifying-the-interpreter" title="Link to this heading">¶</a></h2>
  377. <p>Note that if you specify an interpreter and then distribute your application
  378. archive, you need to ensure that the interpreter used is portable. The Python
  379. launcher for Windows supports most common forms of POSIX <code class="docutils literal notranslate"><span class="pre">#!</span></code> line, but there
  380. are other issues to consider:</p>
  381. <ul class="simple">
  382. <li><p>If you use “/usr/bin/env python” (or other forms of the “python” command,
  383. such as “/usr/bin/python”), you need to consider that your users may have
  384. either Python 2 or Python 3 as their default, and write your code to work
  385. under both versions.</p></li>
  386. <li><p>If you use an explicit version, for example “/usr/bin/env python3” your
  387. application will not work for users who do not have that version. (This
  388. may be what you want if you have not made your code Python 2 compatible).</p></li>
  389. <li><p>There is no way to say “python X.Y or later”, so be careful of using an
  390. exact version like “/usr/bin/env python3.4” as you will need to change your
  391. shebang line for users of Python 3.5, for example.</p></li>
  392. </ul>
  393. <p>Typically, you should use an “/usr/bin/env python2” or “/usr/bin/env python3”,
  394. depending on whether your code is written for Python 2 or 3.</p>
  395. </section>
  396. <section id="creating-standalone-applications-with-zipapp">
  397. <h2>Creating Standalone Applications with zipapp<a class="headerlink" href="#creating-standalone-applications-with-zipapp" title="Link to this heading">¶</a></h2>
  398. <p>Using the <a class="reference internal" href="#module-zipapp" title="zipapp: Manage executable Python zip archives"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code></a> module, it is possible to create self-contained Python
  399. programs, which can be distributed to end users who only need to have a
  400. suitable version of Python installed on their system. The key to doing this
  401. is to bundle all of the application’s dependencies into the archive, along
  402. with the application code.</p>
  403. <p>The steps to create a standalone archive are as follows:</p>
  404. <ol class="arabic">
  405. <li><p>Create your application in a directory as normal, so you have a <code class="docutils literal notranslate"><span class="pre">myapp</span></code>
  406. directory containing a <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> file, and any supporting application
  407. code.</p></li>
  408. <li><p>Install all of your application’s dependencies into the <code class="docutils literal notranslate"><span class="pre">myapp</span></code> directory,
  409. using pip:</p>
  410. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>-r<span class="w"> </span>requirements.txt<span class="w"> </span>--target<span class="w"> </span>myapp
  411. </pre></div>
  412. </div>
  413. <p>(this assumes you have your project requirements in a <code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code>
  414. file - if not, you can just list the dependencies manually on the pip command
  415. line).</p>
  416. </li>
  417. <li><p>Package the application using:</p>
  418. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipapp<span class="w"> </span>-p<span class="w"> </span><span class="s2">&quot;interpreter&quot;</span><span class="w"> </span>myapp
  419. </pre></div>
  420. </div>
  421. </li>
  422. </ol>
  423. <p>This will produce a standalone executable, which can be run on any machine with
  424. the appropriate interpreter available. See <a class="reference internal" href="#zipapp-specifying-the-interpreter"><span class="std std-ref">Specifying the Interpreter</span></a>
  425. for details. It can be shipped to users as a single file.</p>
  426. <p>On Unix, the <code class="docutils literal notranslate"><span class="pre">myapp.pyz</span></code> file is executable as it stands. You can rename the
  427. file to remove the <code class="docutils literal notranslate"><span class="pre">.pyz</span></code> extension if you prefer a “plain” command name. On
  428. Windows, the <code class="docutils literal notranslate"><span class="pre">myapp.pyz[w]</span></code> file is executable by virtue of the fact that
  429. the Python interpreter registers the <code class="docutils literal notranslate"><span class="pre">.pyz</span></code> and <code class="docutils literal notranslate"><span class="pre">.pyzw</span></code> file extensions
  430. when installed.</p>
  431. <section id="caveats">
  432. <h3>Caveats<a class="headerlink" href="#caveats" title="Link to this heading">¶</a></h3>
  433. <p>If your application depends on a package that includes a C extension, that
  434. package cannot be run from a zip file (this is an OS limitation, as executable
  435. code must be present in the filesystem for the OS loader to load it). In this
  436. case, you can exclude that dependency from the zipfile, and either require
  437. your users to have it installed, or ship it alongside your zipfile and add code
  438. to your <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> to include the directory containing the unzipped
  439. module in <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. In this case, you will need to make sure to ship
  440. appropriate binaries for your target architecture(s) (and potentially pick the
  441. correct version to add to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> at runtime, based on the user’s machine).</p>
  442. </section>
  443. </section>
  444. <section id="the-python-zip-application-archive-format">
  445. <h2>The Python Zip Application Archive Format<a class="headerlink" href="#the-python-zip-application-archive-format" title="Link to this heading">¶</a></h2>
  446. <p>Python has been able to execute zip files which contain a <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> file
  447. since version 2.6. In order to be executed by Python, an application archive
  448. simply has to be a standard zip file containing a <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> file which
  449. will be run as the entry point for the application. As usual for any Python
  450. script, the parent of the script (in this case the zip file) will be placed on
  451. <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> and thus further modules can be imported from the zip file.</p>
  452. <p>The zip file format allows arbitrary data to be prepended to a zip file. The
  453. zip application format uses this ability to prepend a standard POSIX “shebang”
  454. line to the file (<code class="docutils literal notranslate"><span class="pre">#!/path/to/interpreter</span></code>).</p>
  455. <p>Formally, the Python zip application format is therefore:</p>
  456. <ol class="arabic simple">
  457. <li><p>An optional shebang line, containing the characters <code class="docutils literal notranslate"><span class="pre">b'#!'</span></code> followed by an
  458. interpreter name, and then a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character. The interpreter
  459. name can be anything acceptable to the OS “shebang” processing, or the Python
  460. launcher on Windows. The interpreter should be encoded in UTF-8 on Windows,
  461. and in <a class="reference internal" href="sys.html#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getfilesystemencoding()</span></code></a> on POSIX.</p></li>
  462. <li><p>Standard zipfile data, as generated by the <a class="reference internal" href="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> module. The
  463. zipfile content <em>must</em> include a file called <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> (which must be
  464. in the “root” of the zipfile - i.e., it cannot be in a subdirectory). The
  465. zipfile data can be compressed or uncompressed.</p></li>
  466. </ol>
  467. <p>If an application archive has a shebang line, it may have the executable bit set
  468. on POSIX systems, to allow it to be executed directly.</p>
  469. <p>There is no requirement that the tools in this module are used to create
  470. application archives - the module is a convenience, but archives in the above
  471. format created by any means are acceptable to Python.</p>
  472. </section>
  473. </section>
  474. <div class="clearer"></div>
  475. </div>
  476. </div>
  477. </div>
  478. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  479. <div class="sphinxsidebarwrapper">
  480. <div>
  481. <h3><a href="../contents.html">Table of Contents</a></h3>
  482. <ul>
  483. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code> — Manage executable Python zip archives</a><ul>
  484. <li><a class="reference internal" href="#basic-example">Basic Example</a></li>
  485. <li><a class="reference internal" href="#command-line-interface">Command-Line Interface</a></li>
  486. <li><a class="reference internal" href="#python-api">Python API</a></li>
  487. <li><a class="reference internal" href="#examples">Examples</a></li>
  488. <li><a class="reference internal" href="#specifying-the-interpreter">Specifying the Interpreter</a></li>
  489. <li><a class="reference internal" href="#creating-standalone-applications-with-zipapp">Creating Standalone Applications with zipapp</a><ul>
  490. <li><a class="reference internal" href="#caveats">Caveats</a></li>
  491. </ul>
  492. </li>
  493. <li><a class="reference internal" href="#the-python-zip-application-archive-format">The Python Zip Application Archive Format</a></li>
  494. </ul>
  495. </li>
  496. </ul>
  497. </div>
  498. <div>
  499. <h4>Previous topic</h4>
  500. <p class="topless"><a href="venv.html"
  501. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code> — Creation of virtual environments</a></p>
  502. </div>
  503. <div>
  504. <h4>Next topic</h4>
  505. <p class="topless"><a href="python.html"
  506. title="next chapter">Python Runtime Services</a></p>
  507. </div>
  508. <div role="note" aria-label="source link">
  509. <h3>This Page</h3>
  510. <ul class="this-page-menu">
  511. <li><a href="../bugs.html">Report a Bug</a></li>
  512. <li>
  513. <a href="https://github.com/python/cpython/blob/main/Doc/library/zipapp.rst"
  514. rel="nofollow">Show Source
  515. </a>
  516. </li>
  517. </ul>
  518. </div>
  519. </div>
  520. <div id="sidebarbutton" title="Collapse sidebar">
  521. <span>«</span>
  522. </div>
  523. </div>
  524. <div class="clearer"></div>
  525. </div>
  526. <div class="related" role="navigation" aria-label="related navigation">
  527. <h3>Navigation</h3>
  528. <ul>
  529. <li class="right" style="margin-right: 10px">
  530. <a href="../genindex.html" title="General Index"
  531. >index</a></li>
  532. <li class="right" >
  533. <a href="../py-modindex.html" title="Python Module Index"
  534. >modules</a> |</li>
  535. <li class="right" >
  536. <a href="python.html" title="Python Runtime Services"
  537. >next</a> |</li>
  538. <li class="right" >
  539. <a href="venv.html" title="venv — Creation of virtual environments"
  540. >previous</a> |</li>
  541. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  542. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  543. <li class="switchers">
  544. <div class="language_switcher_placeholder"></div>
  545. <div class="version_switcher_placeholder"></div>
  546. </li>
  547. <li>
  548. </li>
  549. <li id="cpython-language-and-version">
  550. <a href="../index.html">3.12.3 Documentation</a> &#187;
  551. </li>
  552. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  553. <li class="nav-item nav-item-2"><a href="distribution.html" >Software Packaging and Distribution</a> &#187;</li>
  554. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code> — Manage executable Python zip archives</a></li>
  555. <li class="right">
  556. <div class="inline-search" role="search">
  557. <form class="inline-search" action="../search.html" method="get">
  558. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  559. <input type="submit" value="Go" />
  560. </form>
  561. </div>
  562. |
  563. </li>
  564. <li class="right">
  565. <label class="theme-selector-label">
  566. Theme
  567. <select class="theme-selector" oninput="activateTheme(this.value)">
  568. <option value="auto" selected>Auto</option>
  569. <option value="light">Light</option>
  570. <option value="dark">Dark</option>
  571. </select>
  572. </label> |</li>
  573. </ul>
  574. </div>
  575. <div class="footer">
  576. &copy;
  577. <a href="../copyright.html">
  578. Copyright
  579. </a>
  580. 2001-2024, Python Software Foundation.
  581. <br />
  582. This page is licensed under the Python Software Foundation License Version 2.
  583. <br />
  584. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  585. <br />
  586. See <a href="/license.html">History and License</a> for more information.<br />
  587. <br />
  588. The Python Software Foundation is a non-profit corporation.
  589. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  590. <br />
  591. <br />
  592. Last updated on Apr 09, 2024 (13:47 UTC).
  593. <a href="/bugs.html">Found a bug</a>?
  594. <br />
  595. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  596. </div>
  597. </body>
  598. </html>
上海开阖软件有限公司 沪ICP备12045867号-1