gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1008 行
94KB

  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="venv — Creation of virtual environments" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/venv.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/venv/ The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site directories. A virtual en..." />
  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/venv/ The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site directories. A virtual en..." />
  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>venv — Creation of virtual environments &#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="zipapp — Manage executable Python zip archives" href="zipapp.html" />
  33. <link rel="prev" title="ensurepip — Bootstrapping the pip installer" href="ensurepip.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/venv.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">venv</span></code> — Creation of virtual environments</a><ul>
  86. <li><a class="reference internal" href="#creating-virtual-environments">Creating virtual environments</a></li>
  87. <li><a class="reference internal" href="#how-venvs-work">How venvs work</a></li>
  88. <li><a class="reference internal" href="#api">API</a></li>
  89. <li><a class="reference internal" href="#an-example-of-extending-envbuilder">An example of extending <code class="docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a></li>
  90. </ul>
  91. </li>
  92. </ul>
  93. </div>
  94. <div>
  95. <h4>Previous topic</h4>
  96. <p class="topless"><a href="ensurepip.html"
  97. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code> — Bootstrapping the <code class="docutils literal notranslate"><span class="pre">pip</span></code> installer</a></p>
  98. </div>
  99. <div>
  100. <h4>Next topic</h4>
  101. <p class="topless"><a href="zipapp.html"
  102. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code> — Manage executable Python zip archives</a></p>
  103. </div>
  104. <div role="note" aria-label="source link">
  105. <h3>This Page</h3>
  106. <ul class="this-page-menu">
  107. <li><a href="../bugs.html">Report a Bug</a></li>
  108. <li>
  109. <a href="https://github.com/python/cpython/blob/main/Doc/library/venv.rst"
  110. rel="nofollow">Show Source
  111. </a>
  112. </li>
  113. </ul>
  114. </div>
  115. </nav>
  116. </div>
  117. </div>
  118. <div class="related" role="navigation" aria-label="related navigation">
  119. <h3>Navigation</h3>
  120. <ul>
  121. <li class="right" style="margin-right: 10px">
  122. <a href="../genindex.html" title="General Index"
  123. accesskey="I">index</a></li>
  124. <li class="right" >
  125. <a href="../py-modindex.html" title="Python Module Index"
  126. >modules</a> |</li>
  127. <li class="right" >
  128. <a href="zipapp.html" title="zipapp — Manage executable Python zip archives"
  129. accesskey="N">next</a> |</li>
  130. <li class="right" >
  131. <a href="ensurepip.html" title="ensurepip — Bootstrapping the pip installer"
  132. accesskey="P">previous</a> |</li>
  133. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  134. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  135. <li class="switchers">
  136. <div class="language_switcher_placeholder"></div>
  137. <div class="version_switcher_placeholder"></div>
  138. </li>
  139. <li>
  140. </li>
  141. <li id="cpython-language-and-version">
  142. <a href="../index.html">3.12.3 Documentation</a> &#187;
  143. </li>
  144. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  145. <li class="nav-item nav-item-2"><a href="distribution.html" accesskey="U">Software Packaging and Distribution</a> &#187;</li>
  146. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code> — Creation of virtual environments</a></li>
  147. <li class="right">
  148. <div class="inline-search" role="search">
  149. <form class="inline-search" action="../search.html" method="get">
  150. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  151. <input type="submit" value="Go" />
  152. </form>
  153. </div>
  154. |
  155. </li>
  156. <li class="right">
  157. <label class="theme-selector-label">
  158. Theme
  159. <select class="theme-selector" oninput="activateTheme(this.value)">
  160. <option value="auto" selected>Auto</option>
  161. <option value="light">Light</option>
  162. <option value="dark">Dark</option>
  163. </select>
  164. </label> |</li>
  165. </ul>
  166. </div>
  167. <div class="document">
  168. <div class="documentwrapper">
  169. <div class="bodywrapper">
  170. <div class="body" role="main">
  171. <section id="module-venv">
  172. <span id="venv-creation-of-virtual-environments"></span><h1><a class="reference internal" href="#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> — Creation of virtual environments<a class="headerlink" href="#module-venv" title="Link to this heading">¶</a></h1>
  173. <div class="versionadded">
  174. <p><span class="versionmodified added">New in version 3.3.</span></p>
  175. </div>
  176. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/venv/">Lib/venv/</a></p>
  177. <hr class="docutils" id="index-0" />
  178. <p id="venv-intro"><span id="venv-def"></span>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code> module supports creating lightweight “virtual environments”,
  179. each with their own independent set of Python packages installed in
  180. their <a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> directories.
  181. A virtual environment is created on top of an existing
  182. Python installation, known as the virtual environment’s “base” Python, and may
  183. optionally be isolated from the packages in the base environment,
  184. so only those explicitly installed in the virtual environment are available.</p>
  185. <p>When used from within a virtual environment, common installation tools such as
  186. <a class="reference external" href="https://pypi.org/project/pip/">pip</a> will install Python packages into a virtual environment
  187. without needing to be told to do so explicitly.</p>
  188. <p>A virtual environment is (amongst other things):</p>
  189. <ul class="simple">
  190. <li><p>Used to contain a specific Python interpreter and software libraries and
  191. binaries which are needed to support a project (library or application). These
  192. are by default isolated from software in other virtual environments and Python
  193. interpreters and libraries installed in the operating system.</p></li>
  194. <li><p>Contained in a directory, conventionally either named <code class="docutils literal notranslate"><span class="pre">venv</span></code> or <code class="docutils literal notranslate"><span class="pre">.venv</span></code> in
  195. the project directory, or under a container directory for lots of virtual
  196. environments, such as <code class="docutils literal notranslate"><span class="pre">~/.virtualenvs</span></code>.</p></li>
  197. <li><p>Not checked into source control systems such as Git.</p></li>
  198. <li><p>Considered as disposable – it should be simple to delete and recreate it from
  199. scratch. You don’t place any project code in the environment</p></li>
  200. <li><p>Not considered as movable or copyable – you just recreate the same
  201. environment in the target location.</p></li>
  202. </ul>
  203. <p>See <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0405/"><strong>PEP 405</strong></a> for more background on Python virtual environments.</p>
  204. <div class="admonition seealso">
  205. <p class="admonition-title">See also</p>
  206. <p><a class="reference external" href="https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#create-and-use-virtual-environments">Python Packaging User Guide: Creating and using virtual environments</a></p>
  207. </div>
  208. <div class="availability docutils container">
  209. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  210. <p>This module does not work or is not available on WebAssembly platforms
  211. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  212. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  213. </div>
  214. <section id="creating-virtual-environments">
  215. <h2>Creating virtual environments<a class="headerlink" href="#creating-virtual-environments" title="Link to this heading">¶</a></h2>
  216. <p>Creation of <a class="reference internal" href="#venv-def"><span class="std std-ref">virtual environments</span></a> is done by executing the
  217. command <code class="docutils literal notranslate"><span class="pre">venv</span></code>:</p>
  218. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">venv</span> <span class="o">/</span><span class="n">path</span><span class="o">/</span><span class="n">to</span><span class="o">/</span><span class="n">new</span><span class="o">/</span><span class="n">virtual</span><span class="o">/</span><span class="n">environment</span>
  219. </pre></div>
  220. </div>
  221. <p>Running this command creates the target directory (creating any parent
  222. directories that don’t exist already) and places a <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> file in it
  223. with a <code class="docutils literal notranslate"><span class="pre">home</span></code> key pointing to the Python installation from which the command
  224. was run (a common name for the target directory is <code class="docutils literal notranslate"><span class="pre">.venv</span></code>). It also creates
  225. a <code class="docutils literal notranslate"><span class="pre">bin</span></code> (or <code class="docutils literal notranslate"><span class="pre">Scripts</span></code> on Windows) subdirectory containing a copy/symlink
  226. of the Python binary/binaries (as appropriate for the platform or arguments
  227. used at environment creation time). It also creates an (initially empty)
  228. <code class="docutils literal notranslate"><span class="pre">lib/pythonX.Y/site-packages</span></code> subdirectory (on Windows, this is
  229. <code class="docutils literal notranslate"><span class="pre">Lib\site-packages</span></code>). If an existing directory is specified, it will be
  230. re-used.</p>
  231. <div class="versionchanged">
  232. <p><span class="versionmodified changed">Changed in version 3.5: </span>The use of <code class="docutils literal notranslate"><span class="pre">venv</span></code> is now recommended for creating virtual environments.</p>
  233. </div>
  234. <div class="deprecated">
  235. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span><code class="docutils literal notranslate"><span class="pre">pyvenv</span></code> was the recommended tool for creating virtual environments for
  236. Python 3.3 and 3.4, and is
  237. <a class="reference internal" href="../whatsnew/3.6.html#whatsnew36-venv"><span class="std std-ref">deprecated in Python 3.6</span></a>.</p>
  238. </div>
  239. <p>On Windows, invoke the <code class="docutils literal notranslate"><span class="pre">venv</span></code> command as follows:</p>
  240. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>c:\&gt;Python35\python -m venv c:\path\to\myenv
  241. </pre></div>
  242. </div>
  243. <p>Alternatively, if you configured the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> and <code class="docutils literal notranslate"><span class="pre">PATHEXT</span></code> variables for
  244. your <a class="reference internal" href="../using/windows.html#using-on-windows"><span class="std std-ref">Python installation</span></a>:</p>
  245. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>c:\&gt;python -m venv c:\path\to\myenv
  246. </pre></div>
  247. </div>
  248. <p>The command, if run with <code class="docutils literal notranslate"><span class="pre">-h</span></code>, will show the available options:</p>
  249. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
  250. [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps]
  251. ENV_DIR [ENV_DIR ...]
  252. Creates virtual Python environments in one or more target directories.
  253. positional arguments:
  254. ENV_DIR A directory to create the environment in.
  255. optional arguments:
  256. -h, --help show this help message and exit
  257. --system-site-packages
  258. Give the virtual environment access to the system
  259. site-packages dir.
  260. --symlinks Try to use symlinks rather than copies, when symlinks
  261. are not the default for the platform.
  262. --copies Try to use copies rather than symlinks, even when
  263. symlinks are the default for the platform.
  264. --clear Delete the contents of the environment directory if it
  265. already exists, before environment creation.
  266. --upgrade Upgrade the environment directory to use this version
  267. of Python, assuming Python has been upgraded in-place.
  268. --without-pip Skips installing or upgrading pip in the virtual
  269. environment (pip is bootstrapped by default)
  270. --prompt PROMPT Provides an alternative prompt prefix for this
  271. environment.
  272. --upgrade-deps Upgrade core dependencies (pip) to the
  273. latest version in PyPI
  274. Once an environment has been created, you may wish to activate it, e.g. by
  275. sourcing an activate script in its bin directory.
  276. </pre></div>
  277. </div>
  278. <div class="versionchanged">
  279. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">setuptools</span></code> is no longer a core venv dependency.</p>
  280. </div>
  281. <div class="versionchanged">
  282. <p><span class="versionmodified changed">Changed in version 3.9: </span>Add <code class="docutils literal notranslate"><span class="pre">--upgrade-deps</span></code> option to upgrade pip + setuptools to the latest on PyPI</p>
  283. </div>
  284. <div class="versionchanged">
  285. <p><span class="versionmodified changed">Changed in version 3.4: </span>Installs pip by default, added the <code class="docutils literal notranslate"><span class="pre">--without-pip</span></code> and <code class="docutils literal notranslate"><span class="pre">--copies</span></code>
  286. options</p>
  287. </div>
  288. <div class="versionchanged">
  289. <p><span class="versionmodified changed">Changed in version 3.4: </span>In earlier versions, if the target directory already existed, an error was
  290. raised, unless the <code class="docutils literal notranslate"><span class="pre">--clear</span></code> or <code class="docutils literal notranslate"><span class="pre">--upgrade</span></code> option was provided.</p>
  291. </div>
  292. <div class="admonition note">
  293. <p class="admonition-title">Note</p>
  294. <p>While symlinks are supported on Windows, they are not recommended. Of
  295. particular note is that double-clicking <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> in File Explorer
  296. will resolve the symlink eagerly and ignore the virtual environment.</p>
  297. </div>
  298. <div class="admonition note">
  299. <p class="admonition-title">Note</p>
  300. <p>On Microsoft Windows, it may be required to enable the <code class="docutils literal notranslate"><span class="pre">Activate.ps1</span></code>
  301. script by setting the execution policy for the user. You can do this by
  302. issuing the following PowerShell command:</p>
  303. <p>PS C:&gt; Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser</p>
  304. <p>See <a class="reference external" href="https://go.microsoft.com/fwlink/?LinkID=135170">About Execution Policies</a>
  305. for more information.</p>
  306. </div>
  307. <p>The created <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> file also includes the
  308. <code class="docutils literal notranslate"><span class="pre">include-system-site-packages</span></code> key, set to <code class="docutils literal notranslate"><span class="pre">true</span></code> if <code class="docutils literal notranslate"><span class="pre">venv</span></code> is
  309. run with the <code class="docutils literal notranslate"><span class="pre">--system-site-packages</span></code> option, <code class="docutils literal notranslate"><span class="pre">false</span></code> otherwise.</p>
  310. <p>Unless the <code class="docutils literal notranslate"><span class="pre">--without-pip</span></code> option is given, <a class="reference internal" href="ensurepip.html#module-ensurepip" title="ensurepip: Bootstrapping the &quot;pip&quot; installer into an existing Python installation or virtual environment."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code></a> will be
  311. invoked to bootstrap <code class="docutils literal notranslate"><span class="pre">pip</span></code> into the virtual environment.</p>
  312. <p>Multiple paths can be given to <code class="docutils literal notranslate"><span class="pre">venv</span></code>, in which case an identical virtual
  313. environment will be created, according to the given options, at each provided
  314. path.</p>
  315. </section>
  316. <section id="how-venvs-work">
  317. <span id="venv-explanation"></span><h2>How venvs work<a class="headerlink" href="#how-venvs-work" title="Link to this heading">¶</a></h2>
  318. <p>When a Python interpreter is running from a virtual environment,
  319. <a class="reference internal" href="sys.html#sys.prefix" title="sys.prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.prefix</span></code></a> and <a class="reference internal" href="sys.html#sys.exec_prefix" title="sys.exec_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code></a>
  320. point to the directories of the virtual environment,
  321. whereas <a class="reference internal" href="sys.html#sys.base_prefix" title="sys.base_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.base_prefix</span></code></a> and <a class="reference internal" href="sys.html#sys.base_exec_prefix" title="sys.base_exec_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.base_exec_prefix</span></code></a>
  322. point to those of the base Python used to create the environment.
  323. It is sufficient to check
  324. <code class="docutils literal notranslate"><span class="pre">sys.prefix</span> <span class="pre">!=</span> <span class="pre">sys.base_prefix</span></code> to determine if the current interpreter is
  325. running from a virtual environment.</p>
  326. <p>A virtual environment may be “activated” using a script in its binary directory
  327. (<code class="docutils literal notranslate"><span class="pre">bin</span></code> on POSIX; <code class="docutils literal notranslate"><span class="pre">Scripts</span></code> on Windows).
  328. This will prepend that directory to your <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>, so that running
  329. <strong class="program">python</strong> will invoke the environment’s Python interpreter
  330. and you can run installed scripts without having to use their full path.
  331. The invocation of the activation script is platform-specific
  332. (<code class="samp docutils literal notranslate"><em><span class="pre">&lt;venv&gt;</span></em></code> must be replaced by the path to the directory
  333. containing the virtual environment):</p>
  334. <table class="docutils align-default">
  335. <thead>
  336. <tr class="row-odd"><th class="head"><p>Platform</p></th>
  337. <th class="head"><p>Shell</p></th>
  338. <th class="head"><p>Command to activate virtual environment</p></th>
  339. </tr>
  340. </thead>
  341. <tbody>
  342. <tr class="row-even"><td rowspan="4"><p>POSIX</p></td>
  343. <td><p>bash/zsh</p></td>
  344. <td><p><code class="samp docutils literal notranslate"><span class="pre">$</span> <span class="pre">source</span> <em><span class="pre">&lt;venv&gt;</span></em><span class="pre">/bin/activate</span></code></p></td>
  345. </tr>
  346. <tr class="row-odd"><td><p>fish</p></td>
  347. <td><p><code class="samp docutils literal notranslate"><span class="pre">$</span> <span class="pre">source</span> <em><span class="pre">&lt;venv&gt;</span></em><span class="pre">/bin/activate.fish</span></code></p></td>
  348. </tr>
  349. <tr class="row-even"><td><p>csh/tcsh</p></td>
  350. <td><p><code class="samp docutils literal notranslate"><span class="pre">$</span> <span class="pre">source</span> <em><span class="pre">&lt;venv&gt;</span></em><span class="pre">/bin/activate.csh</span></code></p></td>
  351. </tr>
  352. <tr class="row-odd"><td><p>PowerShell</p></td>
  353. <td><p><code class="samp docutils literal notranslate"><span class="pre">$</span> <em><span class="pre">&lt;venv&gt;</span></em><span class="pre">/bin/Activate.ps1</span></code></p></td>
  354. </tr>
  355. <tr class="row-even"><td rowspan="2"><p>Windows</p></td>
  356. <td><p>cmd.exe</p></td>
  357. <td><p><code class="samp docutils literal notranslate"><span class="pre">C:\&gt;</span> <em><span class="pre">&lt;venv&gt;</span></em><span class="pre">\Scripts\activate.bat</span></code></p></td>
  358. </tr>
  359. <tr class="row-odd"><td><p>PowerShell</p></td>
  360. <td><p><code class="samp docutils literal notranslate"><span class="pre">PS</span> <span class="pre">C:\&gt;</span> <em><span class="pre">&lt;venv&gt;</span></em><span class="pre">\Scripts\Activate.ps1</span></code></p></td>
  361. </tr>
  362. </tbody>
  363. </table>
  364. <div class="versionadded">
  365. <p><span class="versionmodified added">New in version 3.4: </span><strong class="program">fish</strong> and <strong class="program">csh</strong> activation scripts.</p>
  366. </div>
  367. <div class="versionadded">
  368. <p><span class="versionmodified added">New in version 3.8: </span>PowerShell activation scripts installed under POSIX for PowerShell Core
  369. support.</p>
  370. </div>
  371. <p>You don’t specifically <em>need</em> to activate a virtual environment,
  372. as you can just specify the full path to that environment’s
  373. Python interpreter when invoking Python.
  374. Furthermore, all scripts installed in the environment
  375. should be runnable without activating it.</p>
  376. <p>In order to achieve this, scripts installed into virtual environments have
  377. a “shebang” line which points to the environment’s Python interpreter,
  378. i.e. <code class="samp docutils literal notranslate"><span class="pre">#!/</span><em><span class="pre">&lt;path-to-venv&gt;</span></em><span class="pre">/bin/python</span></code>.
  379. This means that the script will run with that interpreter regardless of the
  380. value of <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>. On Windows, “shebang” line processing is supported if
  381. you have the <a class="reference internal" href="../using/windows.html#launcher"><span class="std std-ref">Python Launcher for Windows</span></a> installed. Thus, double-clicking an installed
  382. script in a Windows Explorer window should run it with the correct interpreter
  383. without the environment needing to be activated or on the <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>.</p>
  384. <p>When a virtual environment has been activated, the <code class="xref std std-envvar docutils literal notranslate"><span class="pre">VIRTUAL_ENV</span></code>
  385. environment variable is set to the path of the environment.
  386. Since explicitly activating a virtual environment is not required to use it,
  387. <code class="xref std std-envvar docutils literal notranslate"><span class="pre">VIRTUAL_ENV</span></code> cannot be relied upon to determine
  388. whether a virtual environment is being used.</p>
  389. <div class="admonition warning">
  390. <p class="admonition-title">Warning</p>
  391. <p>Because scripts installed in environments should not expect the
  392. environment to be activated, their shebang lines contain the absolute paths
  393. to their environment’s interpreters. Because of this, environments are
  394. inherently non-portable, in the general case. You should always have a
  395. simple means of recreating an environment (for example, if you have a
  396. requirements file <code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code>, you can invoke <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">-r</span>
  397. <span class="pre">requirements.txt</span></code> using the environment’s <code class="docutils literal notranslate"><span class="pre">pip</span></code> to install all of the
  398. packages needed by the environment). If for any reason you need to move the
  399. environment to a new location, you should recreate it at the desired
  400. location and delete the one at the old location. If you move an environment
  401. because you moved a parent directory of it, you should recreate the
  402. environment in its new location. Otherwise, software installed into the
  403. environment may not work as expected.</p>
  404. </div>
  405. <p>You can deactivate a virtual environment by typing <code class="docutils literal notranslate"><span class="pre">deactivate</span></code> in your shell.
  406. The exact mechanism is platform-specific and is an internal implementation
  407. detail (typically, a script or shell function will be used).</p>
  408. </section>
  409. <section id="api">
  410. <span id="venv-api"></span><h2>API<a class="headerlink" href="#api" title="Link to this heading">¶</a></h2>
  411. <p>The high-level method described above makes use of a simple API which provides
  412. mechanisms for third-party virtual environment creators to customize environment
  413. creation according to their needs, the <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> class.</p>
  414. <dl class="py class">
  415. <dt class="sig sig-object py" id="venv.EnvBuilder">
  416. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">venv.</span></span><span class="sig-name descname"><span class="pre">EnvBuilder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">system_site_packages</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">clear</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">upgrade</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">with_pip</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prompt</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">upgrade_deps</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="#venv.EnvBuilder" title="Link to this definition">¶</a></dt>
  417. <dd><p>The <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> class accepts the following keyword arguments on
  418. instantiation:</p>
  419. <ul class="simple">
  420. <li><p><code class="docutils literal notranslate"><span class="pre">system_site_packages</span></code> – a Boolean value indicating that the system Python
  421. site-packages should be available to the environment (defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>).</p></li>
  422. <li><p><code class="docutils literal notranslate"><span class="pre">clear</span></code> – a Boolean value which, if true, will delete the contents of
  423. any existing target directory, before creating the environment.</p></li>
  424. <li><p><code class="docutils literal notranslate"><span class="pre">symlinks</span></code> – a Boolean value indicating whether to attempt to symlink the
  425. Python binary rather than copying.</p></li>
  426. <li><p><code class="docutils literal notranslate"><span class="pre">upgrade</span></code> – a Boolean value which, if true, will upgrade an existing
  427. environment with the running Python - for use when that Python has been
  428. upgraded in-place (defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>).</p></li>
  429. <li><p><code class="docutils literal notranslate"><span class="pre">with_pip</span></code> – a Boolean value which, if true, ensures pip is
  430. installed in the virtual environment. This uses <a class="reference internal" href="ensurepip.html#module-ensurepip" title="ensurepip: Bootstrapping the &quot;pip&quot; installer into an existing Python installation or virtual environment."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code></a> with
  431. the <code class="docutils literal notranslate"><span class="pre">--default-pip</span></code> option.</p></li>
  432. <li><p><code class="docutils literal notranslate"><span class="pre">prompt</span></code> – a String to be used after virtual environment is activated
  433. (defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code> which means directory name of the environment would
  434. be used). If the special string <code class="docutils literal notranslate"><span class="pre">&quot;.&quot;</span></code> is provided, the basename of the
  435. current directory is used as the prompt.</p></li>
  436. <li><p><code class="docutils literal notranslate"><span class="pre">upgrade_deps</span></code> – Update the base venv modules to the latest on PyPI</p></li>
  437. </ul>
  438. <div class="versionchanged">
  439. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <code class="docutils literal notranslate"><span class="pre">with_pip</span></code> parameter</p>
  440. </div>
  441. <div class="versionchanged">
  442. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <code class="docutils literal notranslate"><span class="pre">prompt</span></code> parameter</p>
  443. </div>
  444. <div class="versionchanged">
  445. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added the <code class="docutils literal notranslate"><span class="pre">upgrade_deps</span></code> parameter</p>
  446. </div>
  447. <p>Creators of third-party virtual environment tools will be free to use the
  448. provided <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> class as a base class.</p>
  449. <p>The returned env-builder is an object which has a method, <code class="docutils literal notranslate"><span class="pre">create</span></code>:</p>
  450. <dl class="py method">
  451. <dt class="sig sig-object py" id="venv.EnvBuilder.create">
  452. <span class="sig-name descname"><span class="pre">create</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">env_dir</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.create" title="Link to this definition">¶</a></dt>
  453. <dd><p>Create a virtual environment by specifying the target directory
  454. (absolute or relative to the current directory) which is to contain the
  455. virtual environment. The <code class="docutils literal notranslate"><span class="pre">create</span></code> method will either create the
  456. environment in the specified directory, or raise an appropriate
  457. exception.</p>
  458. <p>The <code class="docutils literal notranslate"><span class="pre">create</span></code> method of the <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> class illustrates the
  459. hooks available for subclass customization:</p>
  460. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">env_dir</span><span class="p">):</span>
  461. <span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
  462. <span class="sd"> Create a virtualized Python environment in a directory.</span>
  463. <span class="sd"> env_dir is the target directory to create an environment in.</span>
  464. <span class="sd"> &quot;&quot;&quot;</span>
  465. <span class="n">env_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">env_dir</span><span class="p">)</span>
  466. <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ensure_directories</span><span class="p">(</span><span class="n">env_dir</span><span class="p">)</span>
  467. <span class="bp">self</span><span class="o">.</span><span class="n">create_configuration</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
  468. <span class="bp">self</span><span class="o">.</span><span class="n">setup_python</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
  469. <span class="bp">self</span><span class="o">.</span><span class="n">setup_scripts</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
  470. <span class="bp">self</span><span class="o">.</span><span class="n">post_setup</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
  471. </pre></div>
  472. </div>
  473. <p>Each of the methods <a class="reference internal" href="#venv.EnvBuilder.ensure_directories" title="venv.EnvBuilder.ensure_directories"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ensure_directories()</span></code></a>,
  474. <a class="reference internal" href="#venv.EnvBuilder.create_configuration" title="venv.EnvBuilder.create_configuration"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_configuration()</span></code></a>, <a class="reference internal" href="#venv.EnvBuilder.setup_python" title="venv.EnvBuilder.setup_python"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_python()</span></code></a>,
  475. <a class="reference internal" href="#venv.EnvBuilder.setup_scripts" title="venv.EnvBuilder.setup_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_scripts()</span></code></a> and <a class="reference internal" href="#venv.EnvBuilder.post_setup" title="venv.EnvBuilder.post_setup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">post_setup()</span></code></a> can be overridden.</p>
  476. </dd></dl>
  477. <dl class="py method">
  478. <dt class="sig sig-object py" id="venv.EnvBuilder.ensure_directories">
  479. <span class="sig-name descname"><span class="pre">ensure_directories</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">env_dir</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.ensure_directories" title="Link to this definition">¶</a></dt>
  480. <dd><p>Creates the environment directory and all necessary subdirectories that
  481. don’t already exist, and returns a context object. This context object
  482. is just a holder for attributes (such as paths) for use by the other
  483. methods. If the <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> is created with the arg
  484. <code class="docutils literal notranslate"><span class="pre">clear=True</span></code>, contents of the environment directory will be cleared
  485. and then all necessary subdirectories will be recreated.</p>
  486. <p>The returned context object is a <a class="reference internal" href="types.html#types.SimpleNamespace" title="types.SimpleNamespace"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.SimpleNamespace</span></code></a> with the
  487. following attributes:</p>
  488. <ul class="simple">
  489. <li><p><code class="docutils literal notranslate"><span class="pre">env_dir</span></code> - The location of the virtual environment. Used for
  490. <code class="docutils literal notranslate"><span class="pre">__VENV_DIR__</span></code> in activation scripts (see <a class="reference internal" href="#venv.EnvBuilder.install_scripts" title="venv.EnvBuilder.install_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">install_scripts()</span></code></a>).</p></li>
  491. <li><p><code class="docutils literal notranslate"><span class="pre">env_name</span></code> - The name of the virtual environment. Used for
  492. <code class="docutils literal notranslate"><span class="pre">__VENV_NAME__</span></code> in activation scripts (see <a class="reference internal" href="#venv.EnvBuilder.install_scripts" title="venv.EnvBuilder.install_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">install_scripts()</span></code></a>).</p></li>
  493. <li><p><code class="docutils literal notranslate"><span class="pre">prompt</span></code> - The prompt to be used by the activation scripts. Used for
  494. <code class="docutils literal notranslate"><span class="pre">__VENV_PROMPT__</span></code> in activation scripts (see <a class="reference internal" href="#venv.EnvBuilder.install_scripts" title="venv.EnvBuilder.install_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">install_scripts()</span></code></a>).</p></li>
  495. <li><p><code class="docutils literal notranslate"><span class="pre">executable</span></code> - The underlying Python executable used by the virtual
  496. environment. This takes into account the case where a virtual environment
  497. is created from another virtual environment.</p></li>
  498. <li><p><code class="docutils literal notranslate"><span class="pre">inc_path</span></code> - The include path for the virtual environment.</p></li>
  499. <li><p><code class="docutils literal notranslate"><span class="pre">lib_path</span></code> - The purelib path for the virtual environment.</p></li>
  500. <li><p><code class="docutils literal notranslate"><span class="pre">bin_path</span></code> - The script path for the virtual environment.</p></li>
  501. <li><p><code class="docutils literal notranslate"><span class="pre">bin_name</span></code> - The name of the script path relative to the virtual
  502. environment location. Used for <code class="docutils literal notranslate"><span class="pre">__VENV_BIN_NAME__</span></code> in activation
  503. scripts (see <a class="reference internal" href="#venv.EnvBuilder.install_scripts" title="venv.EnvBuilder.install_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">install_scripts()</span></code></a>).</p></li>
  504. <li><p><code class="docutils literal notranslate"><span class="pre">env_exe</span></code> - The name of the Python interpreter in the virtual
  505. environment. Used for <code class="docutils literal notranslate"><span class="pre">__VENV_PYTHON__</span></code> in activation scripts
  506. (see <a class="reference internal" href="#venv.EnvBuilder.install_scripts" title="venv.EnvBuilder.install_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">install_scripts()</span></code></a>).</p></li>
  507. <li><p><code class="docutils literal notranslate"><span class="pre">env_exec_cmd</span></code> - The name of the Python interpreter, taking into
  508. account filesystem redirections. This can be used to run Python in
  509. the virtual environment.</p></li>
  510. </ul>
  511. <div class="versionchanged">
  512. <p><span class="versionmodified changed">Changed in version 3.11: </span>The <em>venv</em>
  513. <a class="reference internal" href="sysconfig.html#installation-paths"><span class="std std-ref">sysconfig installation scheme</span></a>
  514. is used to construct the paths of the created directories.</p>
  515. </div>
  516. <div class="versionchanged">
  517. <p><span class="versionmodified changed">Changed in version 3.12: </span>The attribute <code class="docutils literal notranslate"><span class="pre">lib_path</span></code> was added to the context, and the context
  518. object was documented.</p>
  519. </div>
  520. </dd></dl>
  521. <dl class="py method">
  522. <dt class="sig sig-object py" id="venv.EnvBuilder.create_configuration">
  523. <span class="sig-name descname"><span class="pre">create_configuration</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.create_configuration" title="Link to this definition">¶</a></dt>
  524. <dd><p>Creates the <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> configuration file in the environment.</p>
  525. </dd></dl>
  526. <dl class="py method">
  527. <dt class="sig sig-object py" id="venv.EnvBuilder.setup_python">
  528. <span class="sig-name descname"><span class="pre">setup_python</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.setup_python" title="Link to this definition">¶</a></dt>
  529. <dd><p>Creates a copy or symlink to the Python executable in the environment.
  530. On POSIX systems, if a specific executable <code class="docutils literal notranslate"><span class="pre">python3.x</span></code> was used,
  531. symlinks to <code class="docutils literal notranslate"><span class="pre">python</span></code> and <code class="docutils literal notranslate"><span class="pre">python3</span></code> will be created pointing to that
  532. executable, unless files with those names already exist.</p>
  533. </dd></dl>
  534. <dl class="py method">
  535. <dt class="sig sig-object py" id="venv.EnvBuilder.setup_scripts">
  536. <span class="sig-name descname"><span class="pre">setup_scripts</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.setup_scripts" title="Link to this definition">¶</a></dt>
  537. <dd><p>Installs activation scripts appropriate to the platform into the virtual
  538. environment.</p>
  539. </dd></dl>
  540. <dl class="py method">
  541. <dt class="sig sig-object py" id="venv.EnvBuilder.upgrade_dependencies">
  542. <span class="sig-name descname"><span class="pre">upgrade_dependencies</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.upgrade_dependencies" title="Link to this definition">¶</a></dt>
  543. <dd><p>Upgrades the core venv dependency packages (currently <code class="docutils literal notranslate"><span class="pre">pip</span></code>)
  544. in the environment. This is done by shelling out to the
  545. <code class="docutils literal notranslate"><span class="pre">pip</span></code> executable in the environment.</p>
  546. <div class="versionadded">
  547. <p><span class="versionmodified added">New in version 3.9.</span></p>
  548. </div>
  549. <div class="versionchanged">
  550. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">setuptools</span></code> is no longer a core venv dependency.</p>
  551. </div>
  552. </dd></dl>
  553. <dl class="py method">
  554. <dt class="sig sig-object py" id="venv.EnvBuilder.post_setup">
  555. <span class="sig-name descname"><span class="pre">post_setup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.post_setup" title="Link to this definition">¶</a></dt>
  556. <dd><p>A placeholder method which can be overridden in third party
  557. implementations to pre-install packages in the virtual environment or
  558. perform other post-creation steps.</p>
  559. </dd></dl>
  560. <div class="versionchanged">
  561. <p><span class="versionmodified changed">Changed in version 3.7.2: </span>Windows now uses redirector scripts for <code class="docutils literal notranslate"><span class="pre">python[w].exe</span></code> instead of
  562. copying the actual binaries. In 3.7.2 only <a class="reference internal" href="#venv.EnvBuilder.setup_python" title="venv.EnvBuilder.setup_python"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_python()</span></code></a> does
  563. nothing unless running from a build in the source tree.</p>
  564. </div>
  565. <div class="versionchanged">
  566. <p><span class="versionmodified changed">Changed in version 3.7.3: </span>Windows copies the redirector scripts as part of <a class="reference internal" href="#venv.EnvBuilder.setup_python" title="venv.EnvBuilder.setup_python"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_python()</span></code></a>
  567. instead of <a class="reference internal" href="#venv.EnvBuilder.setup_scripts" title="venv.EnvBuilder.setup_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_scripts()</span></code></a>. This was not the case in 3.7.2.
  568. When using symlinks, the original executables will be linked.</p>
  569. </div>
  570. <p>In addition, <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> provides this utility method that can be
  571. called from <a class="reference internal" href="#venv.EnvBuilder.setup_scripts" title="venv.EnvBuilder.setup_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_scripts()</span></code></a> or <a class="reference internal" href="#venv.EnvBuilder.post_setup" title="venv.EnvBuilder.post_setup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">post_setup()</span></code></a> in subclasses to
  572. assist in installing custom scripts into the virtual environment.</p>
  573. <dl class="py method">
  574. <dt class="sig sig-object py" id="venv.EnvBuilder.install_scripts">
  575. <span class="sig-name descname"><span class="pre">install_scripts</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.install_scripts" title="Link to this definition">¶</a></dt>
  576. <dd><p><em>path</em> is the path to a directory that should contain subdirectories
  577. “common”, “posix”, “nt”, each containing scripts destined for the bin
  578. directory in the environment. The contents of “common” and the
  579. directory corresponding to <a class="reference internal" href="os.html#os.name" title="os.name"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.name</span></code></a> are copied after some text
  580. replacement of placeholders:</p>
  581. <ul class="simple">
  582. <li><p><code class="docutils literal notranslate"><span class="pre">__VENV_DIR__</span></code> is replaced with the absolute path of the environment
  583. directory.</p></li>
  584. <li><p><code class="docutils literal notranslate"><span class="pre">__VENV_NAME__</span></code> is replaced with the environment name (final path
  585. segment of environment directory).</p></li>
  586. <li><p><code class="docutils literal notranslate"><span class="pre">__VENV_PROMPT__</span></code> is replaced with the prompt (the environment
  587. name surrounded by parentheses and with a following space)</p></li>
  588. <li><p><code class="docutils literal notranslate"><span class="pre">__VENV_BIN_NAME__</span></code> is replaced with the name of the bin directory
  589. (either <code class="docutils literal notranslate"><span class="pre">bin</span></code> or <code class="docutils literal notranslate"><span class="pre">Scripts</span></code>).</p></li>
  590. <li><p><code class="docutils literal notranslate"><span class="pre">__VENV_PYTHON__</span></code> is replaced with the absolute path of the
  591. environment’s executable.</p></li>
  592. </ul>
  593. <p>The directories are allowed to exist (for when an existing environment
  594. is being upgraded).</p>
  595. </dd></dl>
  596. </dd></dl>
  597. <p>There is also a module-level convenience function:</p>
  598. <dl class="py function">
  599. <dt class="sig sig-object py" id="venv.create">
  600. <span class="sig-prename descclassname"><span class="pre">venv.</span></span><span class="sig-name descname"><span class="pre">create</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">env_dir</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">system_site_packages</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">clear</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symlinks</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">with_pip</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prompt</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">upgrade_deps</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="#venv.create" title="Link to this definition">¶</a></dt>
  601. <dd><p>Create an <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> with the given keyword arguments, and call its
  602. <a class="reference internal" href="#venv.EnvBuilder.create" title="venv.EnvBuilder.create"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create()</span></code></a> method with the <em>env_dir</em> argument.</p>
  603. <div class="versionadded">
  604. <p><span class="versionmodified added">New in version 3.3.</span></p>
  605. </div>
  606. <div class="versionchanged">
  607. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <code class="docutils literal notranslate"><span class="pre">with_pip</span></code> parameter</p>
  608. </div>
  609. <div class="versionchanged">
  610. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <code class="docutils literal notranslate"><span class="pre">prompt</span></code> parameter</p>
  611. </div>
  612. <div class="versionchanged">
  613. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added the <code class="docutils literal notranslate"><span class="pre">upgrade_deps</span></code> parameter</p>
  614. </div>
  615. </dd></dl>
  616. </section>
  617. <section id="an-example-of-extending-envbuilder">
  618. <h2>An example of extending <code class="docutils literal notranslate"><span class="pre">EnvBuilder</span></code><a class="headerlink" href="#an-example-of-extending-envbuilder" title="Link to this heading">¶</a></h2>
  619. <p>The following script shows how to extend <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> by implementing a
  620. subclass which installs setuptools and pip into a created virtual environment:</p>
  621. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
  622. <span class="kn">import</span> <span class="nn">os.path</span>
  623. <span class="kn">from</span> <span class="nn">subprocess</span> <span class="kn">import</span> <span class="n">Popen</span><span class="p">,</span> <span class="n">PIPE</span>
  624. <span class="kn">import</span> <span class="nn">sys</span>
  625. <span class="kn">from</span> <span class="nn">threading</span> <span class="kn">import</span> <span class="n">Thread</span>
  626. <span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">urlparse</span>
  627. <span class="kn">from</span> <span class="nn">urllib.request</span> <span class="kn">import</span> <span class="n">urlretrieve</span>
  628. <span class="kn">import</span> <span class="nn">venv</span>
  629. <span class="k">class</span> <span class="nc">ExtendedEnvBuilder</span><span class="p">(</span><span class="n">venv</span><span class="o">.</span><span class="n">EnvBuilder</span><span class="p">):</span>
  630. <span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
  631. <span class="sd"> This builder installs setuptools and pip so that you can pip or</span>
  632. <span class="sd"> easy_install other packages into the created virtual environment.</span>
  633. <span class="sd"> :param nodist: If true, setuptools and pip are not installed into the</span>
  634. <span class="sd"> created virtual environment.</span>
  635. <span class="sd"> :param nopip: If true, pip is not installed into the created</span>
  636. <span class="sd"> virtual environment.</span>
  637. <span class="sd"> :param progress: If setuptools or pip are installed, the progress of the</span>
  638. <span class="sd"> installation can be monitored by passing a progress</span>
  639. <span class="sd"> callable. If specified, it is called with two</span>
  640. <span class="sd"> arguments: a string indicating some progress, and a</span>
  641. <span class="sd"> context indicating where the string is coming from.</span>
  642. <span class="sd"> The context argument can have one of three values:</span>
  643. <span class="sd"> &#39;main&#39;, indicating that it is called from virtualize()</span>
  644. <span class="sd"> itself, and &#39;stdout&#39; and &#39;stderr&#39;, which are obtained</span>
  645. <span class="sd"> by reading lines from the output streams of a subprocess</span>
  646. <span class="sd"> which is used to install the app.</span>
  647. <span class="sd"> If a callable is not specified, default progress</span>
  648. <span class="sd"> information is output to sys.stderr.</span>
  649. <span class="sd"> &quot;&quot;&quot;</span>
  650. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
  651. <span class="bp">self</span><span class="o">.</span><span class="n">nodist</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;nodist&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
  652. <span class="bp">self</span><span class="o">.</span><span class="n">nopip</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;nopip&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
  653. <span class="bp">self</span><span class="o">.</span><span class="n">progress</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;progress&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
  654. <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;verbose&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
  655. <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
  656. <span class="k">def</span> <span class="nf">post_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
  657. <span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
  658. <span class="sd"> Set up any packages which need to be pre-installed into the</span>
  659. <span class="sd"> virtual environment being created.</span>
  660. <span class="sd"> :param context: The information for the virtual environment</span>
  661. <span class="sd"> creation request being processed.</span>
  662. <span class="sd"> &quot;&quot;&quot;</span>
  663. <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;VIRTUAL_ENV&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">env_dir</span>
  664. <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodist</span><span class="p">:</span>
  665. <span class="bp">self</span><span class="o">.</span><span class="n">install_setuptools</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
  666. <span class="c1"># Can&#39;t install pip without setuptools</span>
  667. <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">nopip</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodist</span><span class="p">:</span>
  668. <span class="bp">self</span><span class="o">.</span><span class="n">install_pip</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
  669. <span class="k">def</span> <span class="nf">reader</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stream</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
  670. <span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
  671. <span class="sd"> Read lines from a subprocess&#39; output stream and either pass to a progress</span>
  672. <span class="sd"> callable (if specified) or write progress information to sys.stderr.</span>
  673. <span class="sd"> &quot;&quot;&quot;</span>
  674. <span class="n">progress</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">progress</span>
  675. <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
  676. <span class="n">s</span> <span class="o">=</span> <span class="n">stream</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
  677. <span class="k">if</span> <span class="ow">not</span> <span class="n">s</span><span class="p">:</span>
  678. <span class="k">break</span>
  679. <span class="k">if</span> <span class="n">progress</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  680. <span class="n">progress</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
  681. <span class="k">else</span><span class="p">:</span>
  682. <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span><span class="p">:</span>
  683. <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
  684. <span class="k">else</span><span class="p">:</span>
  685. <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
  686. <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
  687. <span class="n">stream</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  688. <span class="k">def</span> <span class="nf">install_script</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">url</span><span class="p">):</span>
  689. <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
  690. <span class="n">fn</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">path</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
  691. <span class="n">binpath</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">bin_path</span>
  692. <span class="n">distpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">binpath</span><span class="p">,</span> <span class="n">fn</span><span class="p">)</span>
  693. <span class="c1"># Download script into the virtual environment&#39;s binaries folder</span>
  694. <span class="n">urlretrieve</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">distpath</span><span class="p">)</span>
  695. <span class="n">progress</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">progress</span>
  696. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span><span class="p">:</span>
  697. <span class="n">term</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>
  698. <span class="k">else</span><span class="p">:</span>
  699. <span class="n">term</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
  700. <span class="k">if</span> <span class="n">progress</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  701. <span class="n">progress</span><span class="p">(</span><span class="s1">&#39;Installing </span><span class="si">%s</span><span class="s1"> ...</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">term</span><span class="p">),</span> <span class="s1">&#39;main&#39;</span><span class="p">)</span>
  702. <span class="k">else</span><span class="p">:</span>
  703. <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;Installing </span><span class="si">%s</span><span class="s1"> ...</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">term</span><span class="p">))</span>
  704. <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
  705. <span class="c1"># Install in the virtual environment</span>
  706. <span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="n">context</span><span class="o">.</span><span class="n">env_exe</span><span class="p">,</span> <span class="n">fn</span><span class="p">]</span>
  707. <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">cwd</span><span class="o">=</span><span class="n">binpath</span><span class="p">)</span>
  708. <span class="n">t1</span> <span class="o">=</span> <span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reader</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="s1">&#39;stdout&#39;</span><span class="p">))</span>
  709. <span class="n">t1</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  710. <span class="n">t2</span> <span class="o">=</span> <span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reader</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s1">&#39;stderr&#39;</span><span class="p">))</span>
  711. <span class="n">t2</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  712. <span class="n">p</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
  713. <span class="n">t1</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  714. <span class="n">t2</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
  715. <span class="k">if</span> <span class="n">progress</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  716. <span class="n">progress</span><span class="p">(</span><span class="s1">&#39;done.&#39;</span><span class="p">,</span> <span class="s1">&#39;main&#39;</span><span class="p">)</span>
  717. <span class="k">else</span><span class="p">:</span>
  718. <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;done.</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
  719. <span class="c1"># Clean up - no longer needed</span>
  720. <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">distpath</span><span class="p">)</span>
  721. <span class="k">def</span> <span class="nf">install_setuptools</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
  722. <span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
  723. <span class="sd"> Install setuptools in the virtual environment.</span>
  724. <span class="sd"> :param context: The information for the virtual environment</span>
  725. <span class="sd"> creation request being processed.</span>
  726. <span class="sd"> &quot;&quot;&quot;</span>
  727. <span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;https://bootstrap.pypa.io/ez_setup.py&quot;</span>
  728. <span class="bp">self</span><span class="o">.</span><span class="n">install_script</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="s1">&#39;setuptools&#39;</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span>
  729. <span class="c1"># clear up the setuptools archive which gets downloaded</span>
  730. <span class="n">pred</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">o</span><span class="p">:</span> <span class="n">o</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;setuptools-&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">o</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;.tar.gz&#39;</span><span class="p">)</span>
  731. <span class="n">files</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="n">pred</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">bin_path</span><span class="p">))</span>
  732. <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
  733. <span class="n">f</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">bin_path</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
  734. <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  735. <span class="k">def</span> <span class="nf">install_pip</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
  736. <span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
  737. <span class="sd"> Install pip in the virtual environment.</span>
  738. <span class="sd"> :param context: The information for the virtual environment</span>
  739. <span class="sd"> creation request being processed.</span>
  740. <span class="sd"> &quot;&quot;&quot;</span>
  741. <span class="n">url</span> <span class="o">=</span> <span class="s1">&#39;https://bootstrap.pypa.io/get-pip.py&#39;</span>
  742. <span class="bp">self</span><span class="o">.</span><span class="n">install_script</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="s1">&#39;pip&#39;</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span>
  743. <span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  744. <span class="kn">import</span> <span class="nn">argparse</span>
  745. <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="vm">__name__</span><span class="p">,</span>
  746. <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Creates virtual Python &#39;</span>
  747. <span class="s1">&#39;environments in one or &#39;</span>
  748. <span class="s1">&#39;more target &#39;</span>
  749. <span class="s1">&#39;directories.&#39;</span><span class="p">)</span>
  750. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;dirs&#39;</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s1">&#39;ENV_DIR&#39;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s1">&#39;+&#39;</span><span class="p">,</span>
  751. <span class="n">help</span><span class="o">=</span><span class="s1">&#39;A directory in which to create the &#39;</span>
  752. <span class="s1">&#39;virtual environment.&#39;</span><span class="p">)</span>
  753. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--no-setuptools&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
  754. <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;nodist&#39;</span><span class="p">,</span>
  755. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Don&#39;t install setuptools or pip in the &quot;</span>
  756. <span class="s2">&quot;virtual environment.&quot;</span><span class="p">)</span>
  757. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--no-pip&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
  758. <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;nopip&#39;</span><span class="p">,</span>
  759. <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Don&#39;t install pip in the virtual &quot;</span>
  760. <span class="s2">&quot;environment.&quot;</span><span class="p">)</span>
  761. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--system-site-packages&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
  762. <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;system_site&#39;</span><span class="p">,</span>
  763. <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Give the virtual environment access to the &#39;</span>
  764. <span class="s1">&#39;system site-packages dir.&#39;</span><span class="p">)</span>
  765. <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;nt&#39;</span><span class="p">:</span>
  766. <span class="n">use_symlinks</span> <span class="o">=</span> <span class="kc">False</span>
  767. <span class="k">else</span><span class="p">:</span>
  768. <span class="n">use_symlinks</span> <span class="o">=</span> <span class="kc">True</span>
  769. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--symlinks&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">use_symlinks</span><span class="p">,</span>
  770. <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;symlinks&#39;</span><span class="p">,</span>
  771. <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Try to use symlinks rather than copies, &#39;</span>
  772. <span class="s1">&#39;when symlinks are not the default for &#39;</span>
  773. <span class="s1">&#39;the platform.&#39;</span><span class="p">)</span>
  774. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--clear&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span>
  775. <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;clear&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Delete the contents of the &#39;</span>
  776. <span class="s1">&#39;virtual environment &#39;</span>
  777. <span class="s1">&#39;directory if it already &#39;</span>
  778. <span class="s1">&#39;exists, before virtual &#39;</span>
  779. <span class="s1">&#39;environment creation.&#39;</span><span class="p">)</span>
  780. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--upgrade&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span>
  781. <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;upgrade&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Upgrade the virtual &#39;</span>
  782. <span class="s1">&#39;environment directory to &#39;</span>
  783. <span class="s1">&#39;use this version of &#39;</span>
  784. <span class="s1">&#39;Python, assuming Python &#39;</span>
  785. <span class="s1">&#39;has been upgraded &#39;</span>
  786. <span class="s1">&#39;in-place.&#39;</span><span class="p">)</span>
  787. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--verbose&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span>
  788. <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;verbose&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Display the output &#39;</span>
  789. <span class="s1">&#39;from the scripts which &#39;</span>
  790. <span class="s1">&#39;install setuptools and pip.&#39;</span><span class="p">)</span>
  791. <span class="n">options</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
  792. <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">upgrade</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">clear</span><span class="p">:</span>
  793. <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;you cannot supply --upgrade and --clear together.&#39;</span><span class="p">)</span>
  794. <span class="n">builder</span> <span class="o">=</span> <span class="n">ExtendedEnvBuilder</span><span class="p">(</span><span class="n">system_site_packages</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">system_site</span><span class="p">,</span>
  795. <span class="n">clear</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">clear</span><span class="p">,</span>
  796. <span class="n">symlinks</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">symlinks</span><span class="p">,</span>
  797. <span class="n">upgrade</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">upgrade</span><span class="p">,</span>
  798. <span class="n">nodist</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">nodist</span><span class="p">,</span>
  799. <span class="n">nopip</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">nopip</span><span class="p">,</span>
  800. <span class="n">verbose</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">verbose</span><span class="p">)</span>
  801. <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">options</span><span class="o">.</span><span class="n">dirs</span><span class="p">:</span>
  802. <span class="n">builder</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  803. <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
  804. <span class="n">rc</span> <span class="o">=</span> <span class="mi">1</span>
  805. <span class="k">try</span><span class="p">:</span>
  806. <span class="n">main</span><span class="p">()</span>
  807. <span class="n">rc</span> <span class="o">=</span> <span class="mi">0</span>
  808. <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  809. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Error: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">e</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
  810. <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">rc</span><span class="p">)</span>
  811. </pre></div>
  812. </div>
  813. <p>This script is also available for download <a class="reference external" href="https://gist.github.com/vsajip/4673395">online</a>.</p>
  814. </section>
  815. </section>
  816. <div class="clearer"></div>
  817. </div>
  818. </div>
  819. </div>
  820. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  821. <div class="sphinxsidebarwrapper">
  822. <div>
  823. <h3><a href="../contents.html">Table of Contents</a></h3>
  824. <ul>
  825. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code> — Creation of virtual environments</a><ul>
  826. <li><a class="reference internal" href="#creating-virtual-environments">Creating virtual environments</a></li>
  827. <li><a class="reference internal" href="#how-venvs-work">How venvs work</a></li>
  828. <li><a class="reference internal" href="#api">API</a></li>
  829. <li><a class="reference internal" href="#an-example-of-extending-envbuilder">An example of extending <code class="docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a></li>
  830. </ul>
  831. </li>
  832. </ul>
  833. </div>
  834. <div>
  835. <h4>Previous topic</h4>
  836. <p class="topless"><a href="ensurepip.html"
  837. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code> — Bootstrapping the <code class="docutils literal notranslate"><span class="pre">pip</span></code> installer</a></p>
  838. </div>
  839. <div>
  840. <h4>Next topic</h4>
  841. <p class="topless"><a href="zipapp.html"
  842. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code> — Manage executable Python zip archives</a></p>
  843. </div>
  844. <div role="note" aria-label="source link">
  845. <h3>This Page</h3>
  846. <ul class="this-page-menu">
  847. <li><a href="../bugs.html">Report a Bug</a></li>
  848. <li>
  849. <a href="https://github.com/python/cpython/blob/main/Doc/library/venv.rst"
  850. rel="nofollow">Show Source
  851. </a>
  852. </li>
  853. </ul>
  854. </div>
  855. </div>
  856. <div id="sidebarbutton" title="Collapse sidebar">
  857. <span>«</span>
  858. </div>
  859. </div>
  860. <div class="clearer"></div>
  861. </div>
  862. <div class="related" role="navigation" aria-label="related navigation">
  863. <h3>Navigation</h3>
  864. <ul>
  865. <li class="right" style="margin-right: 10px">
  866. <a href="../genindex.html" title="General Index"
  867. >index</a></li>
  868. <li class="right" >
  869. <a href="../py-modindex.html" title="Python Module Index"
  870. >modules</a> |</li>
  871. <li class="right" >
  872. <a href="zipapp.html" title="zipapp — Manage executable Python zip archives"
  873. >next</a> |</li>
  874. <li class="right" >
  875. <a href="ensurepip.html" title="ensurepip — Bootstrapping the pip installer"
  876. >previous</a> |</li>
  877. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  878. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  879. <li class="switchers">
  880. <div class="language_switcher_placeholder"></div>
  881. <div class="version_switcher_placeholder"></div>
  882. </li>
  883. <li>
  884. </li>
  885. <li id="cpython-language-and-version">
  886. <a href="../index.html">3.12.3 Documentation</a> &#187;
  887. </li>
  888. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  889. <li class="nav-item nav-item-2"><a href="distribution.html" >Software Packaging and Distribution</a> &#187;</li>
  890. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code> — Creation of virtual environments</a></li>
  891. <li class="right">
  892. <div class="inline-search" role="search">
  893. <form class="inline-search" action="../search.html" method="get">
  894. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  895. <input type="submit" value="Go" />
  896. </form>
  897. </div>
  898. |
  899. </li>
  900. <li class="right">
  901. <label class="theme-selector-label">
  902. Theme
  903. <select class="theme-selector" oninput="activateTheme(this.value)">
  904. <option value="auto" selected>Auto</option>
  905. <option value="light">Light</option>
  906. <option value="dark">Dark</option>
  907. </select>
  908. </label> |</li>
  909. </ul>
  910. </div>
  911. <div class="footer">
  912. &copy;
  913. <a href="../copyright.html">
  914. Copyright
  915. </a>
  916. 2001-2024, Python Software Foundation.
  917. <br />
  918. This page is licensed under the Python Software Foundation License Version 2.
  919. <br />
  920. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  921. <br />
  922. See <a href="/license.html">History and License</a> for more information.<br />
  923. <br />
  924. The Python Software Foundation is a non-profit corporation.
  925. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  926. <br />
  927. <br />
  928. Last updated on Apr 09, 2024 (13:47 UTC).
  929. <a href="/bugs.html">Found a bug</a>?
  930. <br />
  931. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  932. </div>
  933. </body>
  934. </html>
上海开阖软件有限公司 沪ICP备12045867号-1