venv.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="12. Virtual Environments and Packages" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/tutorial/venv.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Introduction: Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the ..." />
  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="Introduction: Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the ..." />
  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>12. Virtual Environments and Packages &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="13. What Now?" href="whatnow.html" />
  34. <link rel="prev" title="11. Brief Tour of the Standard Library — Part II" href="stdlib2.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/tutorial/venv.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.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="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="#">12. Virtual Environments and Packages</a><ul>
  86. <li><a class="reference internal" href="#introduction">12.1. Introduction</a></li>
  87. <li><a class="reference internal" href="#creating-virtual-environments">12.2. Creating Virtual Environments</a></li>
  88. <li><a class="reference internal" href="#managing-packages-with-pip">12.3. Managing Packages with pip</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="stdlib2.html"
  96. title="previous chapter"><span class="section-number">11. </span>Brief Tour of the Standard Library — Part II</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="whatnow.html"
  101. title="next chapter"><span class="section-number">13. </span>What Now?</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/venv.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="whatnow.html" title="13. What Now?"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="stdlib2.html" title="11. Brief Tour of the Standard Library — Part II"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.0 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Tutorial</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><span class="section-number">12. </span>Virtual Environments and Packages</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="virtual-environments-and-packages">
  170. <span id="tut-venv"></span><h1><span class="section-number">12. </span>Virtual Environments and Packages<a class="headerlink" href="#virtual-environments-and-packages" title="Permalink to this headline">¶</a></h1>
  171. <section id="introduction">
  172. <h2><span class="section-number">12.1. </span>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
  173. <p>Python applications will often use packages and modules that don’t
  174. come as part of the standard library. Applications will sometimes
  175. need a specific version of a library, because the application may
  176. require that a particular bug has been fixed or the application may be
  177. written using an obsolete version of the library’s interface.</p>
  178. <p>This means it may not be possible for one Python installation to meet
  179. the requirements of every application. If application A needs version
  180. 1.0 of a particular module but application B needs version 2.0, then
  181. the requirements are in conflict and installing either version 1.0 or 2.0
  182. will leave one application unable to run.</p>
  183. <p>The solution for this problem is to create a <a class="reference internal" href="../glossary.html#term-virtual-environment"><span class="xref std std-term">virtual environment</span></a>, a
  184. self-contained directory tree that contains a Python installation for a
  185. particular version of Python, plus a number of additional packages.</p>
  186. <p>Different applications can then use different virtual environments.
  187. To resolve the earlier example of conflicting requirements,
  188. application A can have its own virtual environment with version 1.0
  189. installed while application B has another virtual environment with version 2.0.
  190. If application B requires a library be upgraded to version 3.0, this will
  191. not affect application A’s environment.</p>
  192. </section>
  193. <section id="creating-virtual-environments">
  194. <h2><span class="section-number">12.2. </span>Creating Virtual Environments<a class="headerlink" href="#creating-virtual-environments" title="Permalink to this headline">¶</a></h2>
  195. <p>The module used to create and manage virtual environments is called
  196. <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a>. <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> will usually install the most recent version of
  197. Python that you have available. If you have multiple versions of Python on your
  198. system, you can select a specific Python version by running <code class="docutils literal notranslate"><span class="pre">python3</span></code> or
  199. whichever version you want.</p>
  200. <p>To create a virtual environment, decide upon a directory where you want to
  201. place it, and run the <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> module as a script with the directory path:</p>
  202. <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="n">tutorial</span><span class="o">-</span><span class="n">env</span>
  203. </pre></div>
  204. </div>
  205. <p>This will create the <code class="docutils literal notranslate"><span class="pre">tutorial-env</span></code> directory if it doesn’t exist,
  206. and also create directories inside it containing a copy of the Python
  207. interpreter and various supporting files.</p>
  208. <p>A common directory location for a virtual environment is <code class="docutils literal notranslate"><span class="pre">.venv</span></code>.
  209. This name keeps the directory typically hidden in your shell and thus
  210. out of the way while giving it a name that explains why the directory
  211. exists. It also prevents clashing with <code class="docutils literal notranslate"><span class="pre">.env</span></code> environment variable
  212. definition files that some tooling supports.</p>
  213. <p>Once you’ve created a virtual environment, you may activate it.</p>
  214. <p>On Windows, run:</p>
  215. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">tutorial</span><span class="o">-</span><span class="n">env</span>\<span class="n">Scripts</span>\<span class="n">activate</span>
  216. </pre></div>
  217. </div>
  218. <p>On Unix or MacOS, run:</p>
  219. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">source</span> <span class="n">tutorial</span><span class="o">-</span><span class="n">env</span><span class="o">/</span><span class="nb">bin</span><span class="o">/</span><span class="n">activate</span>
  220. </pre></div>
  221. </div>
  222. <p>(This script is written for the bash shell. If you use the
  223. <strong class="program">csh</strong> or <strong class="program">fish</strong> shells, there are alternate
  224. <code class="docutils literal notranslate"><span class="pre">activate.csh</span></code> and <code class="docutils literal notranslate"><span class="pre">activate.fish</span></code> scripts you should use
  225. instead.)</p>
  226. <p>Activating the virtual environment will change your shell’s prompt to show what
  227. virtual environment you’re using, and modify the environment so that running
  228. <code class="docutils literal notranslate"><span class="pre">python</span></code> will get you that particular version and installation of Python.
  229. For example:</p>
  230. <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span><span class="nb">source</span><span class="w"> </span>~/envs/tutorial-env/bin/activate
  231. <span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>python
  232. Python<span class="w"> </span><span class="m">3</span>.5.1<span class="w"> </span><span class="o">(</span>default,<span class="w"> </span>May<span class="w"> </span><span class="m">6</span><span class="w"> </span><span class="m">2016</span>,<span class="w"> </span><span class="m">10</span>:59:36<span class="o">)</span>
  233. <span class="w"> </span>...
  234. &gt;&gt;&gt;<span class="w"> </span>import<span class="w"> </span>sys
  235. &gt;&gt;&gt;<span class="w"> </span>sys.path
  236. <span class="o">[</span><span class="s1">&#39;&#39;</span>,<span class="w"> </span><span class="s1">&#39;/usr/local/lib/python35.zip&#39;</span>,<span class="w"> </span>...,
  237. <span class="s1">&#39;~/envs/tutorial-env/lib/python3.5/site-packages&#39;</span><span class="o">]</span>
  238. &gt;&gt;&gt;
  239. </pre></div>
  240. </div>
  241. <p>To deactivate a virtual environment, type:</p>
  242. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">deactivate</span>
  243. </pre></div>
  244. </div>
  245. <p>into the terminal.</p>
  246. </section>
  247. <section id="managing-packages-with-pip">
  248. <h2><span class="section-number">12.3. </span>Managing Packages with pip<a class="headerlink" href="#managing-packages-with-pip" title="Permalink to this headline">¶</a></h2>
  249. <p>You can install, upgrade, and remove packages using a program called
  250. <strong class="program">pip</strong>. By default <code class="docutils literal notranslate"><span class="pre">pip</span></code> will install packages from the <a class="reference external" href="https://pypi.org">Python
  251. Package Index</a>. You can browse the Python
  252. Package Index by going to it in your web browser.</p>
  253. <p><code class="docutils literal notranslate"><span class="pre">pip</span></code> has a number of subcommands: “install”, “uninstall”,
  254. “freeze”, etc. (Consult the <a class="reference internal" href="../installing/index.html#installing-index"><span class="std std-ref">Installing Python Modules</span></a> guide for
  255. complete documentation for <code class="docutils literal notranslate"><span class="pre">pip</span></code>.)</p>
  256. <p>You can install the latest version of a package by specifying a package’s name:</p>
  257. <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>novas
  258. Collecting<span class="w"> </span>novas
  259. <span class="w"> </span>Downloading<span class="w"> </span>novas-3.1.1.3.tar.gz<span class="w"> </span><span class="o">(</span>136kB<span class="o">)</span>
  260. Installing<span class="w"> </span>collected<span class="w"> </span>packages:<span class="w"> </span>novas
  261. <span class="w"> </span>Running<span class="w"> </span>setup.py<span class="w"> </span>install<span class="w"> </span><span class="k">for</span><span class="w"> </span>novas
  262. Successfully<span class="w"> </span>installed<span class="w"> </span>novas-3.1.1.3
  263. </pre></div>
  264. </div>
  265. <p>You can also install a specific version of a package by giving the
  266. package name followed by <code class="docutils literal notranslate"><span class="pre">==</span></code> and the version number:</p>
  267. <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span><span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.6.0
  268. Collecting<span class="w"> </span><span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.6.0
  269. <span class="w"> </span>Using<span class="w"> </span>cached<span class="w"> </span>requests-2.6.0-py2.py3-none-any.whl
  270. Installing<span class="w"> </span>collected<span class="w"> </span>packages:<span class="w"> </span>requests
  271. Successfully<span class="w"> </span>installed<span class="w"> </span>requests-2.6.0
  272. </pre></div>
  273. </div>
  274. <p>If you re-run this command, <code class="docutils literal notranslate"><span class="pre">pip</span></code> will notice that the requested
  275. version is already installed and do nothing. You can supply a
  276. different version number to get that version, or you can run <code class="docutils literal notranslate"><span class="pre">python</span>
  277. <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span> <span class="pre">--upgrade</span></code> to upgrade the package to the latest version:</p>
  278. <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>--upgrade<span class="w"> </span>requests
  279. Collecting<span class="w"> </span>requests
  280. Installing<span class="w"> </span>collected<span class="w"> </span>packages:<span class="w"> </span>requests
  281. <span class="w"> </span>Found<span class="w"> </span>existing<span class="w"> </span>installation:<span class="w"> </span>requests<span class="w"> </span><span class="m">2</span>.6.0
  282. <span class="w"> </span>Uninstalling<span class="w"> </span>requests-2.6.0:
  283. <span class="w"> </span>Successfully<span class="w"> </span>uninstalled<span class="w"> </span>requests-2.6.0
  284. Successfully<span class="w"> </span>installed<span class="w"> </span>requests-2.7.0
  285. </pre></div>
  286. </div>
  287. <p><code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">uninstall</span></code> followed by one or more package names will
  288. remove the packages from the virtual environment.</p>
  289. <p><code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">show</span></code> will display information about a particular package:</p>
  290. <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>show<span class="w"> </span>requests
  291. ---
  292. Metadata-Version:<span class="w"> </span><span class="m">2</span>.0
  293. Name:<span class="w"> </span>requests
  294. Version:<span class="w"> </span><span class="m">2</span>.7.0
  295. Summary:<span class="w"> </span>Python<span class="w"> </span>HTTP<span class="w"> </span><span class="k">for</span><span class="w"> </span>Humans.
  296. Home-page:<span class="w"> </span>http://python-requests.org
  297. Author:<span class="w"> </span>Kenneth<span class="w"> </span>Reitz
  298. Author-email:<span class="w"> </span>me@kennethreitz.com
  299. License:<span class="w"> </span>Apache<span class="w"> </span><span class="m">2</span>.0
  300. Location:<span class="w"> </span>/Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
  301. Requires:
  302. </pre></div>
  303. </div>
  304. <p><code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">list</span></code> will display all of the packages installed in
  305. the virtual environment:</p>
  306. <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>list
  307. novas<span class="w"> </span><span class="o">(</span><span class="m">3</span>.1.1.3<span class="o">)</span>
  308. numpy<span class="w"> </span><span class="o">(</span><span class="m">1</span>.9.2<span class="o">)</span>
  309. pip<span class="w"> </span><span class="o">(</span><span class="m">7</span>.0.3<span class="o">)</span>
  310. requests<span class="w"> </span><span class="o">(</span><span class="m">2</span>.7.0<span class="o">)</span>
  311. setuptools<span class="w"> </span><span class="o">(</span><span class="m">16</span>.0<span class="o">)</span>
  312. </pre></div>
  313. </div>
  314. <p><code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">freeze</span></code> will produce a similar list of the installed packages,
  315. but the output uses the format that <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pip</span> <span class="pre">install</span></code> expects.
  316. A common convention is to put this list in a <code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code> file:</p>
  317. <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>freeze<span class="w"> </span>&gt;<span class="w"> </span>requirements.txt
  318. <span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>cat<span class="w"> </span>requirements.txt
  319. <span class="nv">novas</span><span class="o">==</span><span class="m">3</span>.1.1.3
  320. <span class="nv">numpy</span><span class="o">==</span><span class="m">1</span>.9.2
  321. <span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.7.0
  322. </pre></div>
  323. </div>
  324. <p>The <code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code> can then be committed to version control and
  325. shipped as part of an application. Users can then install all the
  326. necessary packages with <code class="docutils literal notranslate"><span class="pre">install</span> <span class="pre">-r</span></code>:</p>
  327. <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">(</span>tutorial-env<span class="o">)</span><span class="w"> </span>$<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>-r<span class="w"> </span>requirements.txt
  328. Collecting<span class="w"> </span><span class="nv">novas</span><span class="o">==</span><span class="m">3</span>.1.1.3<span class="w"> </span><span class="o">(</span>from<span class="w"> </span>-r<span class="w"> </span>requirements.txt<span class="w"> </span><span class="o">(</span>line<span class="w"> </span><span class="m">1</span><span class="o">))</span>
  329. <span class="w"> </span>...
  330. Collecting<span class="w"> </span><span class="nv">numpy</span><span class="o">==</span><span class="m">1</span>.9.2<span class="w"> </span><span class="o">(</span>from<span class="w"> </span>-r<span class="w"> </span>requirements.txt<span class="w"> </span><span class="o">(</span>line<span class="w"> </span><span class="m">2</span><span class="o">))</span>
  331. <span class="w"> </span>...
  332. Collecting<span class="w"> </span><span class="nv">requests</span><span class="o">==</span><span class="m">2</span>.7.0<span class="w"> </span><span class="o">(</span>from<span class="w"> </span>-r<span class="w"> </span>requirements.txt<span class="w"> </span><span class="o">(</span>line<span class="w"> </span><span class="m">3</span><span class="o">))</span>
  333. <span class="w"> </span>...
  334. Installing<span class="w"> </span>collected<span class="w"> </span>packages:<span class="w"> </span>novas,<span class="w"> </span>numpy,<span class="w"> </span>requests
  335. <span class="w"> </span>Running<span class="w"> </span>setup.py<span class="w"> </span>install<span class="w"> </span><span class="k">for</span><span class="w"> </span>novas
  336. Successfully<span class="w"> </span>installed<span class="w"> </span>novas-3.1.1.3<span class="w"> </span>numpy-1.9.2<span class="w"> </span>requests-2.7.0
  337. </pre></div>
  338. </div>
  339. <p><code class="docutils literal notranslate"><span class="pre">pip</span></code> has many more options. Consult the <a class="reference internal" href="../installing/index.html#installing-index"><span class="std std-ref">Installing Python Modules</span></a>
  340. guide for complete documentation for <code class="docutils literal notranslate"><span class="pre">pip</span></code>. When you’ve written
  341. a package and want to make it available on the Python Package Index,
  342. consult the <a class="reference external" href="https://packaging.python.org/en/latest/tutorials/packaging-projects/">Python packaging user guide</a>.</p>
  343. </section>
  344. </section>
  345. <div class="clearer"></div>
  346. </div>
  347. </div>
  348. </div>
  349. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  350. <div class="sphinxsidebarwrapper">
  351. <div>
  352. <h3><a href="../contents.html">Table of Contents</a></h3>
  353. <ul>
  354. <li><a class="reference internal" href="#">12. Virtual Environments and Packages</a><ul>
  355. <li><a class="reference internal" href="#introduction">12.1. Introduction</a></li>
  356. <li><a class="reference internal" href="#creating-virtual-environments">12.2. Creating Virtual Environments</a></li>
  357. <li><a class="reference internal" href="#managing-packages-with-pip">12.3. Managing Packages with pip</a></li>
  358. </ul>
  359. </li>
  360. </ul>
  361. </div>
  362. <div>
  363. <h4>Previous topic</h4>
  364. <p class="topless"><a href="stdlib2.html"
  365. title="previous chapter"><span class="section-number">11. </span>Brief Tour of the Standard Library — Part II</a></p>
  366. </div>
  367. <div>
  368. <h4>Next topic</h4>
  369. <p class="topless"><a href="whatnow.html"
  370. title="next chapter"><span class="section-number">13. </span>What Now?</a></p>
  371. </div>
  372. <div role="note" aria-label="source link">
  373. <h3>This Page</h3>
  374. <ul class="this-page-menu">
  375. <li><a href="../bugs.html">Report a Bug</a></li>
  376. <li>
  377. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/venv.rst"
  378. rel="nofollow">Show Source
  379. </a>
  380. </li>
  381. </ul>
  382. </div>
  383. </div>
  384. </div>
  385. <div class="clearer"></div>
  386. </div>
  387. <div class="related" role="navigation" aria-label="related navigation">
  388. <h3>Navigation</h3>
  389. <ul>
  390. <li class="right" style="margin-right: 10px">
  391. <a href="../genindex.html" title="General Index"
  392. >index</a></li>
  393. <li class="right" >
  394. <a href="../py-modindex.html" title="Python Module Index"
  395. >modules</a> |</li>
  396. <li class="right" >
  397. <a href="whatnow.html" title="13. What Now?"
  398. >next</a> |</li>
  399. <li class="right" >
  400. <a href="stdlib2.html" title="11. Brief Tour of the Standard Library — Part II"
  401. >previous</a> |</li>
  402. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  403. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  404. <li class="switchers">
  405. <div class="language_switcher_placeholder"></div>
  406. <div class="version_switcher_placeholder"></div>
  407. </li>
  408. <li>
  409. </li>
  410. <li id="cpython-language-and-version">
  411. <a href="../index.html">3.12.0 Documentation</a> &#187;
  412. </li>
  413. <li class="nav-item nav-item-1"><a href="index.html" >The Python Tutorial</a> &#187;</li>
  414. <li class="nav-item nav-item-this"><a href=""><span class="section-number">12. </span>Virtual Environments and Packages</a></li>
  415. <li class="right">
  416. <div class="inline-search" role="search">
  417. <form class="inline-search" action="../search.html" method="get">
  418. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  419. <input type="submit" value="Go" />
  420. </form>
  421. </div>
  422. |
  423. </li>
  424. <li class="right">
  425. <label class="theme-selector-label">
  426. Theme
  427. <select class="theme-selector" oninput="activateTheme(this.value)">
  428. <option value="auto" selected>Auto</option>
  429. <option value="light">Light</option>
  430. <option value="dark">Dark</option>
  431. </select>
  432. </label> |</li>
  433. </ul>
  434. </div>
  435. <div class="footer">
  436. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  437. <br />
  438. This page is licensed under the Python Software Foundation License Version 2.
  439. <br />
  440. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  441. <br />
  442. See <a href="/license.html">History and License</a> for more information.<br />
  443. <br />
  444. The Python Software Foundation is a non-profit corporation.
  445. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  446. <br />
  447. <br />
  448. Last updated on Oct 02, 2023.
  449. <a href="/bugs.html">Found a bug</a>?
  450. <br />
  451. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  452. </div>
  453. </body>
  454. </html>