windows.html 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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="4. Using Python on Windows" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/using/windows.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="This document aims to give an overview of Windows-specific behaviour you should know about when using Python on Microsoft Windows. Unlike most Unix systems and services, Windows does not include a ..." />
  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="This document aims to give an overview of Windows-specific behaviour you should know about when using Python on Microsoft Windows. Unlike most Unix systems and services, Windows does not include a ..." />
  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>4. Using Python on Windows &#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="5. Using Python on a Mac" href="mac.html" />
  34. <link rel="prev" title="3. Configure Python" href="configure.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/using/windows.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="#">4. Using Python on Windows</a><ul>
  86. <li><a class="reference internal" href="#the-full-installer">4.1. The full installer</a><ul>
  87. <li><a class="reference internal" href="#installation-steps">4.1.1. Installation steps</a></li>
  88. <li><a class="reference internal" href="#removing-the-max-path-limitation">4.1.2. Removing the MAX_PATH Limitation</a></li>
  89. <li><a class="reference internal" href="#installing-without-ui">4.1.3. Installing Without UI</a></li>
  90. <li><a class="reference internal" href="#installing-without-downloading">4.1.4. Installing Without Downloading</a></li>
  91. <li><a class="reference internal" href="#modifying-an-install">4.1.5. Modifying an install</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#the-microsoft-store-package">4.2. The Microsoft Store package</a><ul>
  95. <li><a class="reference internal" href="#known-issues">4.2.1. Known issues</a><ul>
  96. <li><a class="reference internal" href="#redirection-of-local-data-registry-and-temporary-paths">4.2.1.1. Redirection of local data, registry, and temporary paths</a></li>
  97. </ul>
  98. </li>
  99. </ul>
  100. </li>
  101. <li><a class="reference internal" href="#the-nuget-org-packages">4.3. The nuget.org packages</a></li>
  102. <li><a class="reference internal" href="#the-embeddable-package">4.4. The embeddable package</a><ul>
  103. <li><a class="reference internal" href="#python-application">4.4.1. Python Application</a></li>
  104. <li><a class="reference internal" href="#embedding-python">4.4.2. Embedding Python</a></li>
  105. </ul>
  106. </li>
  107. <li><a class="reference internal" href="#alternative-bundles">4.5. Alternative bundles</a></li>
  108. <li><a class="reference internal" href="#configuring-python">4.6. Configuring Python</a><ul>
  109. <li><a class="reference internal" href="#excursus-setting-environment-variables">4.6.1. Excursus: Setting environment variables</a></li>
  110. <li><a class="reference internal" href="#finding-the-python-executable">4.6.2. Finding the Python executable</a></li>
  111. </ul>
  112. </li>
  113. <li><a class="reference internal" href="#utf-8-mode">4.7. UTF-8 mode</a></li>
  114. <li><a class="reference internal" href="#python-launcher-for-windows">4.8. Python Launcher for Windows</a><ul>
  115. <li><a class="reference internal" href="#getting-started">4.8.1. Getting started</a><ul>
  116. <li><a class="reference internal" href="#from-the-command-line">4.8.1.1. From the command-line</a></li>
  117. <li><a class="reference internal" href="#virtual-environments">4.8.1.2. Virtual environments</a></li>
  118. <li><a class="reference internal" href="#from-a-script">4.8.1.3. From a script</a></li>
  119. <li><a class="reference internal" href="#from-file-associations">4.8.1.4. From file associations</a></li>
  120. </ul>
  121. </li>
  122. <li><a class="reference internal" href="#shebang-lines">4.8.2. Shebang Lines</a></li>
  123. <li><a class="reference internal" href="#arguments-in-shebang-lines">4.8.3. Arguments in shebang lines</a></li>
  124. <li><a class="reference internal" href="#customization">4.8.4. Customization</a><ul>
  125. <li><a class="reference internal" href="#customization-via-ini-files">4.8.4.1. Customization via INI files</a></li>
  126. <li><a class="reference internal" href="#customizing-default-python-versions">4.8.4.2. Customizing default Python versions</a></li>
  127. </ul>
  128. </li>
  129. <li><a class="reference internal" href="#diagnostics">4.8.5. Diagnostics</a></li>
  130. <li><a class="reference internal" href="#dry-run">4.8.6. Dry Run</a></li>
  131. <li><a class="reference internal" href="#install-on-demand">4.8.7. Install on demand</a></li>
  132. <li><a class="reference internal" href="#return-codes">4.8.8. Return codes</a></li>
  133. </ul>
  134. </li>
  135. <li><a class="reference internal" href="#finding-modules">4.9. Finding modules</a></li>
  136. <li><a class="reference internal" href="#additional-modules">4.10. Additional modules</a><ul>
  137. <li><a class="reference internal" href="#pywin32">4.10.1. PyWin32</a></li>
  138. <li><a class="reference internal" href="#cx-freeze">4.10.2. cx_Freeze</a></li>
  139. </ul>
  140. </li>
  141. <li><a class="reference internal" href="#compiling-python-on-windows">4.11. Compiling Python on Windows</a></li>
  142. <li><a class="reference internal" href="#other-platforms">4.12. Other Platforms</a></li>
  143. </ul>
  144. </li>
  145. </ul>
  146. </div>
  147. <div>
  148. <h4>Previous topic</h4>
  149. <p class="topless"><a href="configure.html"
  150. title="previous chapter"><span class="section-number">3. </span>Configure Python</a></p>
  151. </div>
  152. <div>
  153. <h4>Next topic</h4>
  154. <p class="topless"><a href="mac.html"
  155. title="next chapter"><span class="section-number">5. </span>Using Python on a Mac</a></p>
  156. </div>
  157. <div role="note" aria-label="source link">
  158. <h3>This Page</h3>
  159. <ul class="this-page-menu">
  160. <li><a href="../bugs.html">Report a Bug</a></li>
  161. <li>
  162. <a href="https://github.com/python/cpython/blob/main/Doc/using/windows.rst"
  163. rel="nofollow">Show Source
  164. </a>
  165. </li>
  166. </ul>
  167. </div>
  168. </nav>
  169. </div>
  170. </div>
  171. <div class="related" role="navigation" aria-label="related navigation">
  172. <h3>Navigation</h3>
  173. <ul>
  174. <li class="right" style="margin-right: 10px">
  175. <a href="../genindex.html" title="General Index"
  176. accesskey="I">index</a></li>
  177. <li class="right" >
  178. <a href="../py-modindex.html" title="Python Module Index"
  179. >modules</a> |</li>
  180. <li class="right" >
  181. <a href="mac.html" title="5. Using Python on a Mac"
  182. accesskey="N">next</a> |</li>
  183. <li class="right" >
  184. <a href="configure.html" title="3. Configure Python"
  185. accesskey="P">previous</a> |</li>
  186. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  187. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  188. <li class="switchers">
  189. <div class="language_switcher_placeholder"></div>
  190. <div class="version_switcher_placeholder"></div>
  191. </li>
  192. <li>
  193. </li>
  194. <li id="cpython-language-and-version">
  195. <a href="../index.html">3.12.0 Documentation</a> &#187;
  196. </li>
  197. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python Setup and Usage</a> &#187;</li>
  198. <li class="nav-item nav-item-this"><a href=""><span class="section-number">4. </span>Using Python on Windows</a></li>
  199. <li class="right">
  200. <div class="inline-search" role="search">
  201. <form class="inline-search" action="../search.html" method="get">
  202. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  203. <input type="submit" value="Go" />
  204. </form>
  205. </div>
  206. |
  207. </li>
  208. <li class="right">
  209. <label class="theme-selector-label">
  210. Theme
  211. <select class="theme-selector" oninput="activateTheme(this.value)">
  212. <option value="auto" selected>Auto</option>
  213. <option value="light">Light</option>
  214. <option value="dark">Dark</option>
  215. </select>
  216. </label> |</li>
  217. </ul>
  218. </div>
  219. <div class="document">
  220. <div class="documentwrapper">
  221. <div class="bodywrapper">
  222. <div class="body" role="main">
  223. <section id="using-python-on-windows">
  224. <span id="using-on-windows"></span><h1><span class="section-number">4. </span>Using Python on Windows<a class="headerlink" href="#using-python-on-windows" title="Permalink to this headline">¶</a></h1>
  225. <p>This document aims to give an overview of Windows-specific behaviour you should
  226. know about when using Python on Microsoft Windows.</p>
  227. <p>Unlike most Unix systems and services, Windows does not include a system
  228. supported installation of Python. To make Python available, the CPython team
  229. has compiled Windows installers (MSI packages) with every <a class="reference external" href="https://www.python.org/download/releases/">release</a> for many years. These installers
  230. are primarily intended to add a per-user installation of Python, with the
  231. core interpreter and library being used by a single user. The installer is also
  232. able to install for all users of a single machine, and a separate ZIP file is
  233. available for application-local distributions.</p>
  234. <p>As specified in <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0011/"><strong>PEP 11</strong></a>, a Python release only supports a Windows platform
  235. while Microsoft considers the platform under extended support. This means that
  236. Python 3.12 supports Windows 8.1 and newer. If you require Windows 7
  237. support, please install Python 3.8.</p>
  238. <p>There are a number of different installers available for Windows, each with
  239. certain benefits and downsides.</p>
  240. <p><a class="reference internal" href="#windows-full"><span class="std std-ref">The full installer</span></a> contains all components and is the best option for
  241. developers using Python for any kind of project.</p>
  242. <p><a class="reference internal" href="#windows-store"><span class="std std-ref">The Microsoft Store package</span></a> is a simple installation of Python that is suitable for
  243. running scripts and packages, and using IDLE or other development environments.
  244. It requires Windows 10 and above, but can be safely installed without corrupting other
  245. programs. It also provides many convenient commands for launching Python and
  246. its tools.</p>
  247. <p><a class="reference internal" href="#windows-nuget"><span class="std std-ref">The nuget.org packages</span></a> are lightweight installations intended for continuous
  248. integration systems. It can be used to build Python packages or run scripts,
  249. but is not updateable and has no user interface tools.</p>
  250. <p><a class="reference internal" href="#windows-embeddable"><span class="std std-ref">The embeddable package</span></a> is a minimal package of Python suitable for
  251. embedding into a larger application.</p>
  252. <section id="the-full-installer">
  253. <span id="windows-full"></span><h2><span class="section-number">4.1. </span>The full installer<a class="headerlink" href="#the-full-installer" title="Permalink to this headline">¶</a></h2>
  254. <section id="installation-steps">
  255. <h3><span class="section-number">4.1.1. </span>Installation steps<a class="headerlink" href="#installation-steps" title="Permalink to this headline">¶</a></h3>
  256. <p>Four Python 3.12 installers are available for download - two each for the
  257. 32-bit and 64-bit versions of the interpreter. The <em>web installer</em> is a small
  258. initial download, and it will automatically download the required components as
  259. necessary. The <em>offline installer</em> includes the components necessary for a
  260. default installation and only requires an internet connection for optional
  261. features. See <a class="reference internal" href="#install-layout-option"><span class="std std-ref">Installing Without Downloading</span></a> for other ways to avoid downloading
  262. during installation.</p>
  263. <p>After starting the installer, one of two options may be selected:</p>
  264. <img alt="../_images/win_installer.png" src="../_images/win_installer.png" />
  265. <p>If you select “Install Now”:</p>
  266. <ul class="simple">
  267. <li><p>You will <em>not</em> need to be an administrator (unless a system update for the
  268. C Runtime Library is required or you install the <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a> for all
  269. users)</p></li>
  270. <li><p>Python will be installed into your user directory</p></li>
  271. <li><p>The <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a> will be installed according to the option at the bottom
  272. of the first page</p></li>
  273. <li><p>The standard library, test suite, launcher and pip will be installed</p></li>
  274. <li><p>If selected, the install directory will be added to your <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></p></li>
  275. <li><p>Shortcuts will only be visible for the current user</p></li>
  276. </ul>
  277. <p>Selecting “Customize installation” will allow you to select the features to
  278. install, the installation location and other options or post-install actions.
  279. To install debugging symbols or binaries, you will need to use this option.</p>
  280. <p>To perform an all-users installation, you should select “Customize
  281. installation”. In this case:</p>
  282. <ul class="simple">
  283. <li><p>You may be required to provide administrative credentials or approval</p></li>
  284. <li><p>Python will be installed into the Program Files directory</p></li>
  285. <li><p>The <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a> will be installed into the Windows directory</p></li>
  286. <li><p>Optional features may be selected during installation</p></li>
  287. <li><p>The standard library can be pre-compiled to bytecode</p></li>
  288. <li><p>If selected, the install directory will be added to the system <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code></p></li>
  289. <li><p>Shortcuts are available for all users</p></li>
  290. </ul>
  291. </section>
  292. <section id="removing-the-max-path-limitation">
  293. <span id="max-path"></span><h3><span class="section-number">4.1.2. </span>Removing the MAX_PATH Limitation<a class="headerlink" href="#removing-the-max-path-limitation" title="Permalink to this headline">¶</a></h3>
  294. <p>Windows historically has limited path lengths to 260 characters. This meant that
  295. paths longer than this would not resolve and errors would result.</p>
  296. <p>In the latest versions of Windows, this limitation can be expanded to
  297. approximately 32,000 characters. Your administrator will need to activate the
  298. “Enable Win32 long paths” group policy, or set <code class="docutils literal notranslate"><span class="pre">LongPathsEnabled</span></code> to <code class="docutils literal notranslate"><span class="pre">1</span></code>
  299. in the registry key
  300. <code class="docutils literal notranslate"><span class="pre">HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem</span></code>.</p>
  301. <p>This allows the <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function, the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module and most other
  302. path functionality to accept and return paths longer than 260 characters.</p>
  303. <p>After changing the above option, no further configuration is required.</p>
  304. <div class="versionchanged">
  305. <p><span class="versionmodified changed">Changed in version 3.6: </span>Support for long paths was enabled in Python.</p>
  306. </div>
  307. </section>
  308. <section id="installing-without-ui">
  309. <span id="install-quiet-option"></span><h3><span class="section-number">4.1.3. </span>Installing Without UI<a class="headerlink" href="#installing-without-ui" title="Permalink to this headline">¶</a></h3>
  310. <p>All of the options available in the installer UI can also be specified from the
  311. command line, allowing scripted installers to replicate an installation on many
  312. machines without user interaction. These options may also be set without
  313. suppressing the UI in order to change some of the defaults.</p>
  314. <p>The following options (found by executing the installer with <code class="docutils literal notranslate"><span class="pre">/?</span></code>) can be
  315. passed into the installer:</p>
  316. <table class="docutils align-default">
  317. <colgroup>
  318. <col style="width: 27%" />
  319. <col style="width: 73%" />
  320. </colgroup>
  321. <thead>
  322. <tr class="row-odd"><th class="head"><p>Name</p></th>
  323. <th class="head"><p>Description</p></th>
  324. </tr>
  325. </thead>
  326. <tbody>
  327. <tr class="row-even"><td><p>/passive</p></td>
  328. <td><p>to display progress without requiring user interaction</p></td>
  329. </tr>
  330. <tr class="row-odd"><td><p>/quiet</p></td>
  331. <td><p>to install/uninstall without displaying any UI</p></td>
  332. </tr>
  333. <tr class="row-even"><td><p>/simple</p></td>
  334. <td><p>to prevent user customization</p></td>
  335. </tr>
  336. <tr class="row-odd"><td><p>/uninstall</p></td>
  337. <td><p>to remove Python (without confirmation)</p></td>
  338. </tr>
  339. <tr class="row-even"><td><p>/layout [directory]</p></td>
  340. <td><p>to pre-download all components</p></td>
  341. </tr>
  342. <tr class="row-odd"><td><p>/log [filename]</p></td>
  343. <td><p>to specify log files location</p></td>
  344. </tr>
  345. </tbody>
  346. </table>
  347. <p>All other options are passed as <code class="docutils literal notranslate"><span class="pre">name=value</span></code>, where the value is usually
  348. <code class="docutils literal notranslate"><span class="pre">0</span></code> to disable a feature, <code class="docutils literal notranslate"><span class="pre">1</span></code> to enable a feature, or a path. The full list
  349. of available options is shown below.</p>
  350. <table class="docutils align-default">
  351. <colgroup>
  352. <col style="width: 30%" />
  353. <col style="width: 42%" />
  354. <col style="width: 29%" />
  355. </colgroup>
  356. <thead>
  357. <tr class="row-odd"><th class="head"><p>Name</p></th>
  358. <th class="head"><p>Description</p></th>
  359. <th class="head"><p>Default</p></th>
  360. </tr>
  361. </thead>
  362. <tbody>
  363. <tr class="row-even"><td><p>InstallAllUsers</p></td>
  364. <td><p>Perform a system-wide installation.</p></td>
  365. <td><p>0</p></td>
  366. </tr>
  367. <tr class="row-odd"><td><p>TargetDir</p></td>
  368. <td><p>The installation directory</p></td>
  369. <td><p>Selected based on
  370. InstallAllUsers</p></td>
  371. </tr>
  372. <tr class="row-even"><td><p>DefaultAllUsersTargetDir</p></td>
  373. <td><p>The default installation directory
  374. for all-user installs</p></td>
  375. <td><p><code class="file docutils literal notranslate"><span class="pre">%ProgramFiles%\Python</span> <span class="pre">X.Y</span></code> or <code class="file docutils literal notranslate"><span class="pre">%ProgramFiles(x86)%\Python</span> <span class="pre">X.Y</span></code></p></td>
  376. </tr>
  377. <tr class="row-odd"><td><p>DefaultJustForMeTargetDir</p></td>
  378. <td><p>The default install directory for
  379. just-for-me installs</p></td>
  380. <td><p><code class="file docutils literal notranslate"><span class="pre">%LocalAppData%\Programs\Python\PythonXY</span></code> or
  381. <code class="file docutils literal notranslate"><span class="pre">%LocalAppData%\Programs\Python\PythonXY-32</span></code> or
  382. <code class="file docutils literal notranslate"><span class="pre">%LocalAppData%\Programs\Python\PythonXY-64</span></code></p></td>
  383. </tr>
  384. <tr class="row-even"><td><p>DefaultCustomTargetDir</p></td>
  385. <td><p>The default custom install directory
  386. displayed in the UI</p></td>
  387. <td><p>(empty)</p></td>
  388. </tr>
  389. <tr class="row-odd"><td><p>AssociateFiles</p></td>
  390. <td><p>Create file associations if the
  391. launcher is also installed.</p></td>
  392. <td><p>1</p></td>
  393. </tr>
  394. <tr class="row-even"><td><p>CompileAll</p></td>
  395. <td><p>Compile all <code class="docutils literal notranslate"><span class="pre">.py</span></code> files to
  396. <code class="docutils literal notranslate"><span class="pre">.pyc</span></code>.</p></td>
  397. <td><p>0</p></td>
  398. </tr>
  399. <tr class="row-odd"><td><p>PrependPath</p></td>
  400. <td><p>Prepend install and Scripts
  401. directories to <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> and
  402. add <code class="docutils literal notranslate"><span class="pre">.PY</span></code> to <span class="target" id="index-4"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATHEXT</span></code></p></td>
  403. <td><p>0</p></td>
  404. </tr>
  405. <tr class="row-even"><td><p>AppendPath</p></td>
  406. <td><p>Append install and Scripts
  407. directories to <span class="target" id="index-5"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> and
  408. add <code class="docutils literal notranslate"><span class="pre">.PY</span></code> to <span class="target" id="index-6"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATHEXT</span></code></p></td>
  409. <td><p>0</p></td>
  410. </tr>
  411. <tr class="row-odd"><td><p>Shortcuts</p></td>
  412. <td><p>Create shortcuts for the interpreter,
  413. documentation and IDLE if installed.</p></td>
  414. <td><p>1</p></td>
  415. </tr>
  416. <tr class="row-even"><td><p>Include_doc</p></td>
  417. <td><p>Install Python manual</p></td>
  418. <td><p>1</p></td>
  419. </tr>
  420. <tr class="row-odd"><td><p>Include_debug</p></td>
  421. <td><p>Install debug binaries</p></td>
  422. <td><p>0</p></td>
  423. </tr>
  424. <tr class="row-even"><td><p>Include_dev</p></td>
  425. <td><p>Install developer headers and
  426. libraries. Omitting this may lead to
  427. an unusable installation.</p></td>
  428. <td><p>1</p></td>
  429. </tr>
  430. <tr class="row-odd"><td><p>Include_exe</p></td>
  431. <td><p>Install <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> and
  432. related files. Omitting this may
  433. lead to an unusable installation.</p></td>
  434. <td><p>1</p></td>
  435. </tr>
  436. <tr class="row-even"><td><p>Include_launcher</p></td>
  437. <td><p>Install <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a>.</p></td>
  438. <td><p>1</p></td>
  439. </tr>
  440. <tr class="row-odd"><td><p>InstallLauncherAllUsers</p></td>
  441. <td><p>Installs the launcher for all
  442. users. Also requires
  443. <code class="docutils literal notranslate"><span class="pre">Include_launcher</span></code> to be set to 1</p></td>
  444. <td><p>1</p></td>
  445. </tr>
  446. <tr class="row-even"><td><p>Include_lib</p></td>
  447. <td><p>Install standard library and
  448. extension modules. Omitting this may
  449. lead to an unusable installation.</p></td>
  450. <td><p>1</p></td>
  451. </tr>
  452. <tr class="row-odd"><td><p>Include_pip</p></td>
  453. <td><p>Install bundled pip and setuptools</p></td>
  454. <td><p>1</p></td>
  455. </tr>
  456. <tr class="row-even"><td><p>Include_symbols</p></td>
  457. <td><p>Install debugging symbols (<code class="docutils literal notranslate"><span class="pre">*.pdb</span></code>)</p></td>
  458. <td><p>0</p></td>
  459. </tr>
  460. <tr class="row-odd"><td><p>Include_tcltk</p></td>
  461. <td><p>Install Tcl/Tk support and IDLE</p></td>
  462. <td><p>1</p></td>
  463. </tr>
  464. <tr class="row-even"><td><p>Include_test</p></td>
  465. <td><p>Install standard library test suite</p></td>
  466. <td><p>1</p></td>
  467. </tr>
  468. <tr class="row-odd"><td><p>Include_tools</p></td>
  469. <td><p>Install utility scripts</p></td>
  470. <td><p>1</p></td>
  471. </tr>
  472. <tr class="row-even"><td><p>LauncherOnly</p></td>
  473. <td><p>Only installs the launcher. This
  474. will override most other options.</p></td>
  475. <td><p>0</p></td>
  476. </tr>
  477. <tr class="row-odd"><td><p>SimpleInstall</p></td>
  478. <td><p>Disable most install UI</p></td>
  479. <td><p>0</p></td>
  480. </tr>
  481. <tr class="row-even"><td><p>SimpleInstallDescription</p></td>
  482. <td><p>A custom message to display when the
  483. simplified install UI is used.</p></td>
  484. <td><p>(empty)</p></td>
  485. </tr>
  486. </tbody>
  487. </table>
  488. <p>For example, to silently install a default, system-wide Python installation,
  489. you could use the following command (from an elevated command prompt):</p>
  490. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.9.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
  491. </pre></div>
  492. </div>
  493. <p>To allow users to easily install a personal copy of Python without the test
  494. suite, you could provide a shortcut with the following command. This will
  495. display a simplified initial page and disallow customization:</p>
  496. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.9.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
  497. SimpleInstall=1 SimpleInstallDescription=&quot;Just for me, no test suite.&quot;
  498. </pre></div>
  499. </div>
  500. <p>(Note that omitting the launcher also omits file associations, and is only
  501. recommended for per-user installs when there is also a system-wide installation
  502. that included the launcher.)</p>
  503. <p>The options listed above can also be provided in a file named <code class="docutils literal notranslate"><span class="pre">unattend.xml</span></code>
  504. alongside the executable. This file specifies a list of options and values.
  505. When a value is provided as an attribute, it will be converted to a number if
  506. possible. Values provided as element text are always left as strings. This
  507. example file sets the same options as the previous example:</p>
  508. <div class="highlight-xml notranslate"><div class="highlight"><pre><span></span><span class="nt">&lt;Options&gt;</span>
  509. <span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;InstallAllUsers&quot;</span><span class="w"> </span><span class="na">Value=</span><span class="s">&quot;no&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
  510. <span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;Include_launcher&quot;</span><span class="w"> </span><span class="na">Value=</span><span class="s">&quot;0&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
  511. <span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;Include_test&quot;</span><span class="w"> </span><span class="na">Value=</span><span class="s">&quot;no&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
  512. <span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;SimpleInstall&quot;</span><span class="w"> </span><span class="na">Value=</span><span class="s">&quot;yes&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
  513. <span class="w"> </span><span class="nt">&lt;Option</span><span class="w"> </span><span class="na">Name=</span><span class="s">&quot;SimpleInstallDescription&quot;</span><span class="nt">&gt;</span>Just<span class="w"> </span>for<span class="w"> </span>me,<span class="w"> </span>no<span class="w"> </span>test<span class="w"> </span>suite<span class="nt">&lt;/Option&gt;</span>
  514. <span class="nt">&lt;/Options&gt;</span>
  515. </pre></div>
  516. </div>
  517. </section>
  518. <section id="installing-without-downloading">
  519. <span id="install-layout-option"></span><h3><span class="section-number">4.1.4. </span>Installing Without Downloading<a class="headerlink" href="#installing-without-downloading" title="Permalink to this headline">¶</a></h3>
  520. <p>As some features of Python are not included in the initial installer download,
  521. selecting those features may require an internet connection. To avoid this
  522. need, all possible components may be downloaded on-demand to create a complete
  523. <em>layout</em> that will no longer require an internet connection regardless of the
  524. selected features. Note that this download may be bigger than required, but
  525. where a large number of installations are going to be performed it is very
  526. useful to have a locally cached copy.</p>
  527. <p>Execute the following command from Command Prompt to download all possible
  528. required files. Remember to substitute <code class="docutils literal notranslate"><span class="pre">python-3.9.0.exe</span></code> for the actual
  529. name of your installer, and to create layouts in their own directories to
  530. avoid collisions between files with the same name.</p>
  531. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>python-3.9.0.exe /layout [optional target directory]
  532. </pre></div>
  533. </div>
  534. <p>You may also specify the <code class="docutils literal notranslate"><span class="pre">/quiet</span></code> option to hide the progress display.</p>
  535. </section>
  536. <section id="modifying-an-install">
  537. <h3><span class="section-number">4.1.5. </span>Modifying an install<a class="headerlink" href="#modifying-an-install" title="Permalink to this headline">¶</a></h3>
  538. <p>Once Python has been installed, you can add or remove features through the
  539. Programs and Features tool that is part of Windows. Select the Python entry and
  540. choose “Uninstall/Change” to open the installer in maintenance mode.</p>
  541. <p>“Modify” allows you to add or remove features by modifying the checkboxes -
  542. unchanged checkboxes will not install or remove anything. Some options cannot be
  543. changed in this mode, such as the install directory; to modify these, you will
  544. need to remove and then reinstall Python completely.</p>
  545. <p>“Repair” will verify all the files that should be installed using the current
  546. settings and replace any that have been removed or modified.</p>
  547. <p>“Uninstall” will remove Python entirely, with the exception of the
  548. <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a>, which has its own entry in Programs and Features.</p>
  549. </section>
  550. </section>
  551. <section id="the-microsoft-store-package">
  552. <span id="windows-store"></span><h2><span class="section-number">4.2. </span>The Microsoft Store package<a class="headerlink" href="#the-microsoft-store-package" title="Permalink to this headline">¶</a></h2>
  553. <div class="versionadded">
  554. <p><span class="versionmodified added">New in version 3.7.2.</span></p>
  555. </div>
  556. <p>The Microsoft Store package is an easily installable Python interpreter that
  557. is intended mainly for interactive use, for example, by students.</p>
  558. <p>To install the package, ensure you have the latest Windows 10 updates and
  559. search the Microsoft Store app for “Python 3.12”. Ensure that the app
  560. you select is published by the Python Software Foundation, and install it.</p>
  561. <div class="admonition warning">
  562. <p class="admonition-title">Warning</p>
  563. <p>Python will always be available for free on the Microsoft Store. If you
  564. are asked to pay for it, you have not selected the correct package.</p>
  565. </div>
  566. <p>After installation, Python may be launched by finding it in Start.
  567. Alternatively, it will be available from any Command Prompt or PowerShell
  568. session by typing <code class="docutils literal notranslate"><span class="pre">python</span></code>. Further, pip and IDLE may be used by typing
  569. <code class="docutils literal notranslate"><span class="pre">pip</span></code> or <code class="docutils literal notranslate"><span class="pre">idle</span></code>. IDLE can also be found in Start.</p>
  570. <p>All three commands are also available with version number suffixes, for
  571. example, as <code class="docutils literal notranslate"><span class="pre">python3.exe</span></code> and <code class="docutils literal notranslate"><span class="pre">python3.x.exe</span></code> as well as
  572. <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> (where <code class="docutils literal notranslate"><span class="pre">3.x</span></code> is the specific version you want to launch,
  573. such as 3.12). Open “Manage App Execution Aliases” through Start to
  574. select which version of Python is associated with each command. It is
  575. recommended to make sure that <code class="docutils literal notranslate"><span class="pre">pip</span></code> and <code class="docutils literal notranslate"><span class="pre">idle</span></code> are consistent with
  576. whichever version of <code class="docutils literal notranslate"><span class="pre">python</span></code> is selected.</p>
  577. <p>Virtual environments can be created with <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">venv</span></code> and activated
  578. and used as normal.</p>
  579. <p>If you have installed another version of Python and added it to your
  580. <code class="docutils literal notranslate"><span class="pre">PATH</span></code> variable, it will be available as <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> rather than the
  581. one from the Microsoft Store. To access the new installation, use
  582. <code class="docutils literal notranslate"><span class="pre">python3.exe</span></code> or <code class="docutils literal notranslate"><span class="pre">python3.x.exe</span></code>.</p>
  583. <p>The <code class="docutils literal notranslate"><span class="pre">py.exe</span></code> launcher will detect this Python installation, but will prefer
  584. installations from the traditional installer.</p>
  585. <p>To remove Python, open Settings and use Apps and Features, or else find
  586. Python in Start and right-click to select Uninstall. Uninstalling will
  587. remove all packages you installed directly into this Python installation, but
  588. will not remove any virtual environments</p>
  589. <section id="known-issues">
  590. <h3><span class="section-number">4.2.1. </span>Known issues<a class="headerlink" href="#known-issues" title="Permalink to this headline">¶</a></h3>
  591. <section id="redirection-of-local-data-registry-and-temporary-paths">
  592. <h4><span class="section-number">4.2.1.1. </span>Redirection of local data, registry, and temporary paths<a class="headerlink" href="#redirection-of-local-data-registry-and-temporary-paths" title="Permalink to this headline">¶</a></h4>
  593. <p>Because of restrictions on Microsoft Store apps, Python scripts may not have
  594. full write access to shared locations such as <span class="target" id="index-7"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TEMP</span></code> and the registry.
  595. Instead, it will write to a private copy. If your scripts must modify the
  596. shared locations, you will need to install the full installer.</p>
  597. <p>At runtime, Python will use a private copy of well-known Windows folders and the registry.
  598. For example, if the environment variable <span class="target" id="index-8"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">%APPDATA%</span></code> is <code class="file docutils literal notranslate"><span class="pre">c:\Users\&lt;user&gt;\AppData\</span></code>,
  599. then when writing to <code class="file docutils literal notranslate"><span class="pre">C:\Users\&lt;user&gt;\AppData\Local</span></code> will write to
  600. <code class="file docutils literal notranslate"><span class="pre">C:\Users\&lt;user&gt;\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\Local\</span></code>.</p>
  601. <p>When reading files, Windows will return the file from the private folder, or if that does not exist, the
  602. real Windows directory. For example reading <code class="file docutils literal notranslate"><span class="pre">C:\Windows\System32</span></code> returns the contents of <code class="file docutils literal notranslate"><span class="pre">C:\Windows\System32</span></code>
  603. plus the contents of <code class="file docutils literal notranslate"><span class="pre">C:\Program</span> <span class="pre">Files\WindowsApps\package_name\VFS\SystemX86</span></code>.</p>
  604. <p>You can find the real path of any existing file using <a class="reference internal" href="../library/os.path.html#os.path.realpath" title="os.path.realpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.realpath()</span></code></a>:</p>
  605. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">os</span>
  606. <span class="gp">&gt;&gt;&gt; </span><span class="n">test_file</span> <span class="o">=</span> <span class="s1">&#39;C:</span><span class="se">\\</span><span class="s1">Users</span><span class="se">\\</span><span class="s1">example</span><span class="se">\\</span><span class="s1">AppData</span><span class="se">\\</span><span class="s1">Local</span><span class="se">\\</span><span class="s1">test.txt&#39;</span>
  607. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="n">test_file</span><span class="p">)</span>
  608. <span class="go">&#39;C:\\Users\\example\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\\LocalCache\\Local\\test.txt&#39;</span>
  609. </pre></div>
  610. </div>
  611. <p>When writing to the Windows Registry, the following behaviors exist:</p>
  612. <ul class="simple">
  613. <li><p>Reading from <code class="docutils literal notranslate"><span class="pre">HKLM\\Software</span></code> is allowed and results are merged with the <code class="file docutils literal notranslate"><span class="pre">registry.dat</span></code> file in the package.</p></li>
  614. <li><p>Writing to <code class="docutils literal notranslate"><span class="pre">HKLM\\Software</span></code> is not allowed if the corresponding key/value exists, i.e. modifying existing keys.</p></li>
  615. <li><p>Writing to <code class="docutils literal notranslate"><span class="pre">HKLM\\Software</span></code> is allowed as long as a corresponding key/value does not exist in the package
  616. and the user has the correct access permissions.</p></li>
  617. </ul>
  618. <p>For more detail on the technical basis for these limitations, please consult
  619. Microsoft’s documentation on packaged full-trust apps, currently available at
  620. <a class="reference external" href="https://docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-behind-the-scenes">docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-behind-the-scenes</a></p>
  621. </section>
  622. </section>
  623. </section>
  624. <section id="the-nuget-org-packages">
  625. <span id="windows-nuget"></span><h2><span class="section-number">4.3. </span>The nuget.org packages<a class="headerlink" href="#the-nuget-org-packages" title="Permalink to this headline">¶</a></h2>
  626. <div class="versionadded">
  627. <p><span class="versionmodified added">New in version 3.5.2.</span></p>
  628. </div>
  629. <p>The nuget.org package is a reduced size Python environment intended for use on
  630. continuous integration and build systems that do not have a system-wide
  631. install of Python. While nuget is “the package manager for .NET”, it also works
  632. perfectly fine for packages containing build-time tools.</p>
  633. <p>Visit <a class="reference external" href="https://www.nuget.org/">nuget.org</a> for the most up-to-date information
  634. on using nuget. What follows is a summary that is sufficient for Python
  635. developers.</p>
  636. <p>The <code class="docutils literal notranslate"><span class="pre">nuget.exe</span></code> command line tool may be downloaded directly from
  637. <code class="docutils literal notranslate"><span class="pre">https://aka.ms/nugetclidl</span></code>, for example, using curl or PowerShell. With the
  638. tool, the latest version of Python for 64-bit or 32-bit machines is installed
  639. using:</p>
  640. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>nuget.exe install python -ExcludeVersion -OutputDirectory .
  641. nuget.exe install pythonx86 -ExcludeVersion -OutputDirectory .
  642. </pre></div>
  643. </div>
  644. <p>To select a particular version, add a <code class="docutils literal notranslate"><span class="pre">-Version</span> <span class="pre">3.x.y</span></code>. The output directory
  645. may be changed from <code class="docutils literal notranslate"><span class="pre">.</span></code>, and the package will be installed into a
  646. subdirectory. By default, the subdirectory is named the same as the package,
  647. and without the <code class="docutils literal notranslate"><span class="pre">-ExcludeVersion</span></code> option this name will include the specific
  648. version installed. Inside the subdirectory is a <code class="docutils literal notranslate"><span class="pre">tools</span></code> directory that
  649. contains the Python installation:</p>
  650. <div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="go"># Without -ExcludeVersion</span>
  651. <span class="gp">&gt;</span> .\python.3.5.2\tools\python.exe -V
  652. <span class="go">Python 3.5.2</span>
  653. <span class="go"># With -ExcludeVersion</span>
  654. <span class="gp">&gt;</span> .\python\tools\python.exe -V
  655. <span class="go">Python 3.5.2</span>
  656. </pre></div>
  657. </div>
  658. <p>In general, nuget packages are not upgradeable, and newer versions should be
  659. installed side-by-side and referenced using the full path. Alternatively,
  660. delete the package directory manually and install it again. Many CI systems
  661. will do this automatically if they do not preserve files between builds.</p>
  662. <p>Alongside the <code class="docutils literal notranslate"><span class="pre">tools</span></code> directory is a <code class="docutils literal notranslate"><span class="pre">build\native</span></code> directory. This
  663. contains a MSBuild properties file <code class="docutils literal notranslate"><span class="pre">python.props</span></code> that can be used in a
  664. C++ project to reference the Python install. Including the settings will
  665. automatically use the headers and import libraries in your build.</p>
  666. <p>The package information pages on nuget.org are
  667. <a class="reference external" href="https://www.nuget.org/packages/python">www.nuget.org/packages/python</a>
  668. for the 64-bit version and <a class="reference external" href="https://www.nuget.org/packages/pythonx86">www.nuget.org/packages/pythonx86</a> for the 32-bit version.</p>
  669. </section>
  670. <section id="the-embeddable-package">
  671. <span id="windows-embeddable"></span><h2><span class="section-number">4.4. </span>The embeddable package<a class="headerlink" href="#the-embeddable-package" title="Permalink to this headline">¶</a></h2>
  672. <div class="versionadded">
  673. <p><span class="versionmodified added">New in version 3.5.</span></p>
  674. </div>
  675. <p>The embedded distribution is a ZIP file containing a minimal Python environment.
  676. It is intended for acting as part of another application, rather than being
  677. directly accessed by end-users.</p>
  678. <p>When extracted, the embedded distribution is (almost) fully isolated from the
  679. user’s system, including environment variables, system registry settings, and
  680. installed packages. The standard library is included as pre-compiled and
  681. optimized <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files in a ZIP, and <code class="docutils literal notranslate"><span class="pre">python3.dll</span></code>, <code class="docutils literal notranslate"><span class="pre">python37.dll</span></code>,
  682. <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> and <code class="docutils literal notranslate"><span class="pre">pythonw.exe</span></code> are all provided. Tcl/tk (including all
  683. dependents, such as Idle), pip and the Python documentation are not included.</p>
  684. <div class="admonition note">
  685. <p class="admonition-title">Note</p>
  686. <p>The embedded distribution does not include the <a class="reference external" href="https://docs.microsoft.com/en-US/cpp/windows/latest-supported-vc-redist#visual-studio-2015-2017-2019-and-2022">Microsoft C Runtime</a> and it is
  687. the responsibility of the application installer to provide this. The
  688. runtime may have already been installed on a user’s system previously or
  689. automatically via Windows Update, and can be detected by finding
  690. <code class="docutils literal notranslate"><span class="pre">ucrtbase.dll</span></code> in the system directory.</p>
  691. </div>
  692. <p>Third-party packages should be installed by the application installer alongside
  693. the embedded distribution. Using pip to manage dependencies as for a regular
  694. Python installation is not supported with this distribution, though with some
  695. care it may be possible to include and use pip for automatic updates. In
  696. general, third-party packages should be treated as part of the application
  697. (“vendoring”) so that the developer can ensure compatibility with newer
  698. versions before providing updates to users.</p>
  699. <p>The two recommended use cases for this distribution are described below.</p>
  700. <section id="python-application">
  701. <h3><span class="section-number">4.4.1. </span>Python Application<a class="headerlink" href="#python-application" title="Permalink to this headline">¶</a></h3>
  702. <p>An application written in Python does not necessarily require users to be aware
  703. of that fact. The embedded distribution may be used in this case to include a
  704. private version of Python in an install package. Depending on how transparent it
  705. should be (or conversely, how professional it should appear), there are two
  706. options.</p>
  707. <p>Using a specialized executable as a launcher requires some coding, but provides
  708. the most transparent experience for users. With a customized launcher, there are
  709. no obvious indications that the program is running on Python: icons can be
  710. customized, company and version information can be specified, and file
  711. associations behave properly. In most cases, a custom launcher should simply be
  712. able to call <code class="docutils literal notranslate"><span class="pre">Py_Main</span></code> with a hard-coded command line.</p>
  713. <p>The simpler approach is to provide a batch file or generated shortcut that
  714. directly calls the <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> or <code class="docutils literal notranslate"><span class="pre">pythonw.exe</span></code> with the required
  715. command-line arguments. In this case, the application will appear to be Python
  716. and not its actual name, and users may have trouble distinguishing it from other
  717. running Python processes or file associations.</p>
  718. <p>With the latter approach, packages should be installed as directories alongside
  719. the Python executable to ensure they are available on the path. With the
  720. specialized launcher, packages can be located in other locations as there is an
  721. opportunity to specify the search path before launching the application.</p>
  722. </section>
  723. <section id="embedding-python">
  724. <h3><span class="section-number">4.4.2. </span>Embedding Python<a class="headerlink" href="#embedding-python" title="Permalink to this headline">¶</a></h3>
  725. <p>Applications written in native code often require some form of scripting
  726. language, and the embedded Python distribution can be used for this purpose. In
  727. general, the majority of the application is in native code, and some part will
  728. either invoke <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> or directly use <code class="docutils literal notranslate"><span class="pre">python3.dll</span></code>. For either case,
  729. extracting the embedded distribution to a subdirectory of the application
  730. installation is sufficient to provide a loadable Python interpreter.</p>
  731. <p>As with the application use, packages can be installed to any location as there
  732. is an opportunity to specify search paths before initializing the interpreter.
  733. Otherwise, there is no fundamental differences between using the embedded
  734. distribution and a regular installation.</p>
  735. </section>
  736. </section>
  737. <section id="alternative-bundles">
  738. <h2><span class="section-number">4.5. </span>Alternative bundles<a class="headerlink" href="#alternative-bundles" title="Permalink to this headline">¶</a></h2>
  739. <p>Besides the standard CPython distribution, there are modified packages including
  740. additional functionality. The following is a list of popular versions and their
  741. key features:</p>
  742. <dl>
  743. <dt><a class="reference external" href="https://www.activestate.com/products/python/">ActivePython</a></dt><dd><p>Installer with multi-platform compatibility, documentation, PyWin32</p>
  744. </dd>
  745. <dt><a class="reference external" href="https://www.anaconda.com/download/">Anaconda</a></dt><dd><p>Popular scientific modules (such as numpy, scipy and pandas) and the
  746. <code class="docutils literal notranslate"><span class="pre">conda</span></code> package manager.</p>
  747. </dd>
  748. <dt><a class="reference external" href="https://www.enthought.com/edm/">Enthought Deployment Manager</a></dt><dd><p>“The Next Generation Python Environment and Package Manager”.</p>
  749. <p>Previously Enthought provided Canopy, but it <a class="reference external" href="https://support.enthought.com/hc/en-us/articles/360038600051-Canopy-GUI-end-of-life-transition-to-the-Enthought-Deployment-Manager-EDM-and-Visual-Studio-Code">reached end of life in 2016</a>.</p>
  750. </dd>
  751. <dt><a class="reference external" href="https://winpython.github.io/">WinPython</a></dt><dd><p>Windows-specific distribution with prebuilt scientific packages and
  752. tools for building packages.</p>
  753. </dd>
  754. </dl>
  755. <p>Note that these packages may not include the latest versions of Python or
  756. other libraries, and are not maintained or supported by the core Python team.</p>
  757. </section>
  758. <section id="configuring-python">
  759. <h2><span class="section-number">4.6. </span>Configuring Python<a class="headerlink" href="#configuring-python" title="Permalink to this headline">¶</a></h2>
  760. <p>To run Python conveniently from a command prompt, you might consider changing
  761. some default environment variables in Windows. While the installer provides an
  762. option to configure the PATH and PATHEXT variables for you, this is only
  763. reliable for a single, system-wide installation. If you regularly use multiple
  764. versions of Python, consider using the <a class="reference internal" href="#launcher"><span class="std std-ref">Python Launcher for Windows</span></a>.</p>
  765. <section id="excursus-setting-environment-variables">
  766. <span id="setting-envvars"></span><h3><span class="section-number">4.6.1. </span>Excursus: Setting environment variables<a class="headerlink" href="#excursus-setting-environment-variables" title="Permalink to this headline">¶</a></h3>
  767. <p>Windows allows environment variables to be configured permanently at both the
  768. User level and the System level, or temporarily in a command prompt.</p>
  769. <p>To temporarily set environment variables, open Command Prompt and use the
  770. <strong class="command">set</strong> command:</p>
  771. <div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\&gt;</span><span class="k">set</span> <span class="nv">PATH</span><span class="p">=</span>C:\Program Files\Python 3.9;<span class="nv">%PATH%</span>
  772. <span class="gp">C:\&gt;</span><span class="k">set</span> <span class="nv">PYTHONPATH</span><span class="p">=</span><span class="nv">%PYTHONPATH%</span>;C:\My_python_lib
  773. <span class="gp">C:\&gt;</span>python
  774. </pre></div>
  775. </div>
  776. <p>These changes will apply to any further commands executed in that console, and
  777. will be inherited by any applications started from the console.</p>
  778. <p>Including the variable name within percent signs will expand to the existing
  779. value, allowing you to add your new value at either the start or the end.
  780. Modifying <span class="target" id="index-9"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> by adding the directory containing
  781. <strong class="program">python.exe</strong> to the start is a common way to ensure the correct version
  782. of Python is launched.</p>
  783. <p>To permanently modify the default environment variables, click Start and search
  784. for ‘edit environment variables’, or open System properties, <span class="guilabel">Advanced
  785. system settings</span> and click the <span class="guilabel">Environment Variables</span> button.
  786. In this dialog, you can add or modify User and System variables. To change
  787. System variables, you need non-restricted access to your machine
  788. (i.e. Administrator rights).</p>
  789. <div class="admonition note">
  790. <p class="admonition-title">Note</p>
  791. <p>Windows will concatenate User variables <em>after</em> System variables, which may
  792. cause unexpected results when modifying <span class="target" id="index-10"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>.</p>
  793. <p>The <span class="target" id="index-11"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> variable is used by all versions of Python,
  794. so you should not permanently configure it unless the listed paths
  795. only include code that is compatible with all of your installed Python
  796. versions.</p>
  797. </div>
  798. <div class="admonition seealso">
  799. <p class="admonition-title">See also</p>
  800. <dl class="simple">
  801. <dt><a class="reference external" href="https://docs.microsoft.com/en-us/windows/win32/procthread/environment-variables">https://docs.microsoft.com/en-us/windows/win32/procthread/environment-variables</a></dt><dd><p>Overview of environment variables on Windows</p>
  802. </dd>
  803. <dt><a class="reference external" href="https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1">https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1</a></dt><dd><p>The <code class="docutils literal notranslate"><span class="pre">set</span></code> command, for temporarily modifying environment variables</p>
  804. </dd>
  805. <dt><a class="reference external" href="https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/setx">https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/setx</a></dt><dd><p>The <code class="docutils literal notranslate"><span class="pre">setx</span></code> command, for permanently modifying environment variables</p>
  806. </dd>
  807. </dl>
  808. </div>
  809. </section>
  810. <section id="finding-the-python-executable">
  811. <span id="windows-path-mod"></span><h3><span class="section-number">4.6.2. </span>Finding the Python executable<a class="headerlink" href="#finding-the-python-executable" title="Permalink to this headline">¶</a></h3>
  812. <div class="versionchanged">
  813. <p><span class="versionmodified changed">Changed in version 3.5.</span></p>
  814. </div>
  815. <p>Besides using the automatically created start menu entry for the Python
  816. interpreter, you might want to start Python in the command prompt. The
  817. installer has an option to set that up for you.</p>
  818. <p>On the first page of the installer, an option labelled “Add Python to PATH”
  819. may be selected to have the installer add the install location into the
  820. <span class="target" id="index-12"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>. The location of the <code class="file docutils literal notranslate"><span class="pre">Scripts\</span></code> folder is also added.
  821. This allows you to type <strong class="command">python</strong> to run the interpreter, and
  822. <strong class="command">pip</strong> for the package installer. Thus, you can also execute your
  823. scripts with command line options, see <a class="reference internal" href="cmdline.html#using-on-cmdline"><span class="std std-ref">Command line</span></a> documentation.</p>
  824. <p>If you don’t enable this option at install time, you can always re-run the
  825. installer, select Modify, and enable it. Alternatively, you can manually
  826. modify the <span class="target" id="index-13"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> using the directions in <a class="reference internal" href="#setting-envvars"><span class="std std-ref">Excursus: Setting environment variables</span></a>. You
  827. need to set your <span class="target" id="index-14"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> environment variable to include the directory
  828. of your Python installation, delimited by a semicolon from other entries. An
  829. example variable could look like this (assuming the first two entries already
  830. existed):</p>
  831. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.9
  832. </pre></div>
  833. </div>
  834. </section>
  835. </section>
  836. <section id="utf-8-mode">
  837. <span id="win-utf8-mode"></span><h2><span class="section-number">4.7. </span>UTF-8 mode<a class="headerlink" href="#utf-8-mode" title="Permalink to this headline">¶</a></h2>
  838. <div class="versionadded">
  839. <p><span class="versionmodified added">New in version 3.7.</span></p>
  840. </div>
  841. <p>Windows still uses legacy encodings for the system encoding (the ANSI Code
  842. Page). Python uses it for the default encoding of text files (e.g.
  843. <a class="reference internal" href="../library/locale.html#locale.getencoding" title="locale.getencoding"><code class="xref py py-func docutils literal notranslate"><span class="pre">locale.getencoding()</span></code></a>).</p>
  844. <p>This may cause issues because UTF-8 is widely used on the internet
  845. and most Unix systems, including WSL (Windows Subsystem for Linux).</p>
  846. <p>You can use the <a class="reference internal" href="../library/os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> to change the default text
  847. encoding to UTF-8. You can enable the <a class="reference internal" href="../library/os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> via
  848. the <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> command line option, or the <code class="docutils literal notranslate"><span class="pre">PYTHONUTF8=1</span></code> environment
  849. variable. See <span class="target" id="index-15"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONUTF8"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUTF8</span></code></a> for enabling UTF-8 mode, and
  850. <a class="reference internal" href="#setting-envvars"><span class="std std-ref">Excursus: Setting environment variables</span></a> for how to modify environment variables.</p>
  851. <p>When the <a class="reference internal" href="../library/os.html#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled, you can still use the
  852. system encoding (the ANSI Code Page) via the “mbcs” codec.</p>
  853. <p>Note that adding <code class="docutils literal notranslate"><span class="pre">PYTHONUTF8=1</span></code> to the default environment variables
  854. will affect all Python 3.7+ applications on your system.
  855. If you have any Python 3.7+ applications which rely on the legacy
  856. system encoding, it is recommended to set the environment variable
  857. temporarily or use the <code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">utf8</span></code> command line option.</p>
  858. <div class="admonition note">
  859. <p class="admonition-title">Note</p>
  860. <p>Even when UTF-8 mode is disabled, Python uses UTF-8 by default
  861. on Windows for:</p>
  862. <ul class="simple">
  863. <li><p>Console I/O including standard I/O (see <span class="target" id="index-16"></span><a class="pep reference external" href="https://peps.python.org/pep-0528/"><strong>PEP 528</strong></a> for details).</p></li>
  864. <li><p>The <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding</span></a>
  865. (see <span class="target" id="index-17"></span><a class="pep reference external" href="https://peps.python.org/pep-0529/"><strong>PEP 529</strong></a> for details).</p></li>
  866. </ul>
  867. </div>
  868. </section>
  869. <section id="python-launcher-for-windows">
  870. <span id="launcher"></span><h2><span class="section-number">4.8. </span>Python Launcher for Windows<a class="headerlink" href="#python-launcher-for-windows" title="Permalink to this headline">¶</a></h2>
  871. <div class="versionadded">
  872. <p><span class="versionmodified added">New in version 3.3.</span></p>
  873. </div>
  874. <p>The Python launcher for Windows is a utility which aids in locating and
  875. executing of different Python versions. It allows scripts (or the
  876. command-line) to indicate a preference for a specific Python version, and
  877. will locate and execute that version.</p>
  878. <p>Unlike the <span class="target" id="index-18"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> variable, the launcher will correctly select the most
  879. appropriate version of Python. It will prefer per-user installations over
  880. system-wide ones, and orders by language version rather than using the most
  881. recently installed version.</p>
  882. <p>The launcher was originally specified in <span class="target" id="index-19"></span><a class="pep reference external" href="https://peps.python.org/pep-0397/"><strong>PEP 397</strong></a>.</p>
  883. <section id="getting-started">
  884. <h3><span class="section-number">4.8.1. </span>Getting started<a class="headerlink" href="#getting-started" title="Permalink to this headline">¶</a></h3>
  885. <section id="from-the-command-line">
  886. <h4><span class="section-number">4.8.1.1. </span>From the command-line<a class="headerlink" href="#from-the-command-line" title="Permalink to this headline">¶</a></h4>
  887. <div class="versionchanged">
  888. <p><span class="versionmodified changed">Changed in version 3.6.</span></p>
  889. </div>
  890. <p>System-wide installations of Python 3.3 and later will put the launcher on your
  891. <span class="target" id="index-20"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>. The launcher is compatible with all available versions of
  892. Python, so it does not matter which version is installed. To check that the
  893. launcher is available, execute the following command in Command Prompt:</p>
  894. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py
  895. </pre></div>
  896. </div>
  897. <p>You should find that the latest version of Python you have installed is
  898. started - it can be exited as normal, and any additional command-line
  899. arguments specified will be sent directly to Python.</p>
  900. <p>If you have multiple versions of Python installed (e.g., 3.7 and 3.12) you
  901. will have noticed that Python 3.12 was started - to launch Python 3.7, try
  902. the command:</p>
  903. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py -3.7
  904. </pre></div>
  905. </div>
  906. <p>If you want the latest version of Python 2 you have installed, try the
  907. command:</p>
  908. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py -2
  909. </pre></div>
  910. </div>
  911. <p>If you see the following error, you do not have the launcher installed:</p>
  912. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&#39;py&#39; is not recognized as an internal or external command,
  913. operable program or batch file.
  914. </pre></div>
  915. </div>
  916. <p>The command:</p>
  917. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py --list
  918. </pre></div>
  919. </div>
  920. <p>displays the currently installed version(s) of Python.</p>
  921. <p>The <code class="docutils literal notranslate"><span class="pre">-x.y</span></code> argument is the short form of the <code class="docutils literal notranslate"><span class="pre">-V:Company/Tag</span></code> argument,
  922. which allows selecting a specific Python runtime, including those that may have
  923. come from somewhere other than python.org. Any runtime registered by following
  924. <span class="target" id="index-21"></span><a class="pep reference external" href="https://peps.python.org/pep-0514/"><strong>PEP 514</strong></a> will be discoverable. The <code class="docutils literal notranslate"><span class="pre">--list</span></code> command lists all available
  925. runtimes using the <code class="docutils literal notranslate"><span class="pre">-V:</span></code> format.</p>
  926. <p>When using the <code class="docutils literal notranslate"><span class="pre">-V:</span></code> argument, specifying the Company will limit selection to
  927. runtimes from that provider, while specifying only the Tag will select from all
  928. providers. Note that omitting the slash implies a tag:</p>
  929. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span># Select any &#39;3.*&#39; tagged runtime
  930. py -V:3
  931. # Select any &#39;PythonCore&#39; released runtime
  932. py -V:PythonCore/
  933. # Select PythonCore&#39;s latest Python 3 runtime
  934. py -V:PythonCore/3
  935. </pre></div>
  936. </div>
  937. <p>The short form of the argument (<code class="docutils literal notranslate"><span class="pre">-3</span></code>) only ever selects from core Python
  938. releases, and not other distributions. However, the longer form (<code class="docutils literal notranslate"><span class="pre">-V:3</span></code>) will
  939. select from any.</p>
  940. <p>The Company is matched on the full string, case-insenitive. The Tag is matched
  941. oneither the full string, or a prefix, provided the next character is a dot or a
  942. hyphen. This allows <code class="docutils literal notranslate"><span class="pre">-V:3.1</span></code> to match <code class="docutils literal notranslate"><span class="pre">3.1-32</span></code>, but not <code class="docutils literal notranslate"><span class="pre">3.10</span></code>. Tags are
  943. sorted using numerical ordering (<code class="docutils literal notranslate"><span class="pre">3.10</span></code> is newer than <code class="docutils literal notranslate"><span class="pre">3.1</span></code>), but are
  944. compared using text (<code class="docutils literal notranslate"><span class="pre">-V:3.01</span></code> does not match <code class="docutils literal notranslate"><span class="pre">3.1</span></code>).</p>
  945. </section>
  946. <section id="virtual-environments">
  947. <h4><span class="section-number">4.8.1.2. </span>Virtual environments<a class="headerlink" href="#virtual-environments" title="Permalink to this headline">¶</a></h4>
  948. <div class="versionadded">
  949. <p><span class="versionmodified added">New in version 3.5.</span></p>
  950. </div>
  951. <p>If the launcher is run with no explicit Python version specification, and a
  952. virtual environment (created with the standard library <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 or
  953. the external <code class="docutils literal notranslate"><span class="pre">virtualenv</span></code> tool) active, the launcher will run the virtual
  954. environment’s interpreter rather than the global one. To run the global
  955. interpreter, either deactivate the virtual environment, or explicitly specify
  956. the global Python version.</p>
  957. </section>
  958. <section id="from-a-script">
  959. <h4><span class="section-number">4.8.1.3. </span>From a script<a class="headerlink" href="#from-a-script" title="Permalink to this headline">¶</a></h4>
  960. <p>Let’s create a test Python script - create a file called <code class="docutils literal notranslate"><span class="pre">hello.py</span></code> with the
  961. following contents</p>
  962. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ch">#! python</span>
  963. <span class="kn">import</span> <span class="nn">sys</span>
  964. <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;hello from Python </span><span class="si">%s</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">version</span><span class="p">,))</span>
  965. </pre></div>
  966. </div>
  967. <p>From the directory in which hello.py lives, execute the command:</p>
  968. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>py hello.py
  969. </pre></div>
  970. </div>
  971. <p>You should notice the version number of your latest Python 2.x installation
  972. is printed. Now try changing the first line to be:</p>
  973. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ch">#! python3</span>
  974. </pre></div>
  975. </div>
  976. <p>Re-executing the command should now print the latest Python 3.x information.
  977. As with the above command-line examples, you can specify a more explicit
  978. version qualifier. Assuming you have Python 3.7 installed, try changing
  979. the first line to <code class="docutils literal notranslate"><span class="pre">#!</span> <span class="pre">python3.7</span></code> and you should find the 3.7
  980. version information printed.</p>
  981. <p>Note that unlike interactive use, a bare “python” will use the latest
  982. version of Python 2.x that you have installed. This is for backward
  983. compatibility and for compatibility with Unix, where the command <code class="docutils literal notranslate"><span class="pre">python</span></code>
  984. typically refers to Python 2.</p>
  985. </section>
  986. <section id="from-file-associations">
  987. <h4><span class="section-number">4.8.1.4. </span>From file associations<a class="headerlink" href="#from-file-associations" title="Permalink to this headline">¶</a></h4>
  988. <p>The launcher should have been associated with Python files (i.e. <code class="docutils literal notranslate"><span class="pre">.py</span></code>,
  989. <code class="docutils literal notranslate"><span class="pre">.pyw</span></code>, <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files) when it was installed. This means that
  990. when you double-click on one of these files from Windows explorer the launcher
  991. will be used, and therefore you can use the same facilities described above to
  992. have the script specify the version which should be used.</p>
  993. <p>The key benefit of this is that a single launcher can support multiple Python
  994. versions at the same time depending on the contents of the first line.</p>
  995. </section>
  996. </section>
  997. <section id="shebang-lines">
  998. <h3><span class="section-number">4.8.2. </span>Shebang Lines<a class="headerlink" href="#shebang-lines" title="Permalink to this headline">¶</a></h3>
  999. <p>If the first line of a script file starts with <code class="docutils literal notranslate"><span class="pre">#!</span></code>, it is known as a
  1000. “shebang” line. Linux and other Unix like operating systems have native
  1001. support for such lines and they are commonly used on such systems to indicate
  1002. how a script should be executed. This launcher allows the same facilities to
  1003. be used with Python scripts on Windows and the examples above demonstrate their
  1004. use.</p>
  1005. <p>To allow shebang lines in Python scripts to be portable between Unix and
  1006. Windows, this launcher supports a number of ‘virtual’ commands to specify
  1007. which interpreter to use. The supported virtual commands are:</p>
  1008. <ul class="simple">
  1009. <li><p><code class="docutils literal notranslate"><span class="pre">/usr/bin/env</span></code></p></li>
  1010. <li><p><code class="docutils literal notranslate"><span class="pre">/usr/bin/python</span></code></p></li>
  1011. <li><p><code class="docutils literal notranslate"><span class="pre">/usr/local/bin/python</span></code></p></li>
  1012. <li><p><code class="docutils literal notranslate"><span class="pre">python</span></code></p></li>
  1013. </ul>
  1014. <p>For example, if the first line of your script starts with</p>
  1015. <div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="ch">#! /usr/bin/python</span>
  1016. </pre></div>
  1017. </div>
  1018. <p>The default Python will be located and used. As many Python scripts written
  1019. to work on Unix will already have this line, you should find these scripts can
  1020. be used by the launcher without modification. If you are writing a new script
  1021. on Windows which you hope will be useful on Unix, you should use one of the
  1022. shebang lines starting with <code class="docutils literal notranslate"><span class="pre">/usr</span></code>.</p>
  1023. <p>Any of the above virtual commands can be suffixed with an explicit version
  1024. (either just the major version, or the major and minor version).
  1025. Furthermore the 32-bit version can be requested by adding “-32” after the
  1026. minor version. I.e. <code class="docutils literal notranslate"><span class="pre">/usr/bin/python3.7-32</span></code> will request usage of the
  1027. 32-bit python 3.7.</p>
  1028. <div class="versionadded">
  1029. <p><span class="versionmodified added">New in version 3.7: </span>Beginning with python launcher 3.7 it is possible to request 64-bit version
  1030. by the “-64” suffix. Furthermore it is possible to specify a major and
  1031. architecture without minor (i.e. <code class="docutils literal notranslate"><span class="pre">/usr/bin/python3-64</span></code>).</p>
  1032. </div>
  1033. <div class="versionchanged">
  1034. <p><span class="versionmodified changed">Changed in version 3.11: </span>The “-64” suffix is deprecated, and now implies “any architecture that is
  1035. not provably i386/32-bit”. To request a specific environment, use the new
  1036. <code class="samp docutils literal notranslate"><span class="pre">-V:</span><em><span class="pre">TAG</span></em></code> argument with the complete tag.</p>
  1037. </div>
  1038. <p>The <code class="docutils literal notranslate"><span class="pre">/usr/bin/env</span></code> form of shebang line has one further special property.
  1039. Before looking for installed Python interpreters, this form will search the
  1040. executable <span class="target" id="index-22"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> for a Python executable matching the name provided
  1041. as the first argument. This corresponds to the behaviour of the Unix <code class="docutils literal notranslate"><span class="pre">env</span></code>
  1042. program, which performs a <span class="target" id="index-23"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> search.
  1043. If an executable matching the first argument after the <code class="docutils literal notranslate"><span class="pre">env</span></code> command cannot
  1044. be found, but the argument starts with <code class="docutils literal notranslate"><span class="pre">python</span></code>, it will be handled as
  1045. described for the other virtual commands.
  1046. The environment variable <span class="target" id="index-24"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYLAUNCHER_NO_SEARCH_PATH</span></code> may be set
  1047. (to any value) to skip this search of <span class="target" id="index-25"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>.</p>
  1048. <p>Shebang lines that do not match any of these patterns are looked up in the
  1049. <code class="docutils literal notranslate"><span class="pre">[commands]</span></code> section of the launcher’s <a class="reference internal" href="#launcher-ini"><span class="std std-ref">.INI file</span></a>.
  1050. This may be used to handle certain commands in a way that makes sense for your
  1051. system. The name of the command must be a single argument (no spaces in the
  1052. shebang executable), and the value substituted is the full path to the
  1053. executable (additional arguments specified in the .INI will be quoted as part
  1054. of the filename).</p>
  1055. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[commands]</span>
  1056. <span class="na">/bin/xpython</span><span class="o">=</span><span class="s">C:\Program Files\XPython\python.exe</span>
  1057. </pre></div>
  1058. </div>
  1059. <p>Any commands not found in the .INI file are treated as <strong>Windows</strong> executable
  1060. paths that are absolute or relative to the directory containing the script file.
  1061. This is a convenience for Windows-only scripts, such as those generated by an
  1062. installer, since the behavior is not compatible with Unix-style shells.
  1063. These paths may be quoted, and may include multiple arguments, after which the
  1064. path to the script and any additional arguments will be appended.</p>
  1065. </section>
  1066. <section id="arguments-in-shebang-lines">
  1067. <h3><span class="section-number">4.8.3. </span>Arguments in shebang lines<a class="headerlink" href="#arguments-in-shebang-lines" title="Permalink to this headline">¶</a></h3>
  1068. <p>The shebang lines can also specify additional options to be passed to the
  1069. Python interpreter. For example, if you have a shebang line:</p>
  1070. <div class="highlight-sh notranslate"><div class="highlight"><pre><span></span><span class="ch">#! /usr/bin/python -v</span>
  1071. </pre></div>
  1072. </div>
  1073. <p>Then Python will be started with the <code class="docutils literal notranslate"><span class="pre">-v</span></code> option</p>
  1074. </section>
  1075. <section id="customization">
  1076. <h3><span class="section-number">4.8.4. </span>Customization<a class="headerlink" href="#customization" title="Permalink to this headline">¶</a></h3>
  1077. <section id="customization-via-ini-files">
  1078. <span id="launcher-ini"></span><h4><span class="section-number">4.8.4.1. </span>Customization via INI files<a class="headerlink" href="#customization-via-ini-files" title="Permalink to this headline">¶</a></h4>
  1079. <p>Two .ini files will be searched by the launcher - <code class="docutils literal notranslate"><span class="pre">py.ini</span></code> in the current
  1080. user’s application data directory (<code class="docutils literal notranslate"><span class="pre">%LOCALAPPDATA%</span></code> or <code class="docutils literal notranslate"><span class="pre">$env:LocalAppData</span></code>)
  1081. and <code class="docutils literal notranslate"><span class="pre">py.ini</span></code> in the same directory as the launcher. The same .ini files are
  1082. used for both the ‘console’ version of the launcher (i.e. py.exe) and for the
  1083. ‘windows’ version (i.e. pyw.exe).</p>
  1084. <p>Customization specified in the “application directory” will have precedence over
  1085. the one next to the executable, so a user, who may not have write access to the
  1086. .ini file next to the launcher, can override commands in that global .ini file.</p>
  1087. </section>
  1088. <section id="customizing-default-python-versions">
  1089. <h4><span class="section-number">4.8.4.2. </span>Customizing default Python versions<a class="headerlink" href="#customizing-default-python-versions" title="Permalink to this headline">¶</a></h4>
  1090. <p>In some cases, a version qualifier can be included in a command to dictate
  1091. which version of Python will be used by the command. A version qualifier
  1092. starts with a major version number and can optionally be followed by a period
  1093. (‘.’) and a minor version specifier. Furthermore it is possible to specify
  1094. if a 32 or 64 bit implementation shall be requested by adding “-32” or “-64”.</p>
  1095. <p>For example, a shebang line of <code class="docutils literal notranslate"><span class="pre">#!python</span></code> has no version qualifier, while
  1096. <code class="docutils literal notranslate"><span class="pre">#!python3</span></code> has a version qualifier which specifies only a major version.</p>
  1097. <p>If no version qualifiers are found in a command, the environment
  1098. variable <span class="target" id="index-26"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PY_PYTHON</span></code> can be set to specify the default version
  1099. qualifier. If it is not set, the default is “3”. The variable can
  1100. specify any value that may be passed on the command line, such as “3”,
  1101. “3.7”, “3.7-32” or “3.7-64”. (Note that the “-64” option is only
  1102. available with the launcher included with Python 3.7 or newer.)</p>
  1103. <p>If no minor version qualifiers are found, the environment variable
  1104. <code class="docutils literal notranslate"><span class="pre">PY_PYTHON{major}</span></code> (where <code class="docutils literal notranslate"><span class="pre">{major}</span></code> is the current major version qualifier
  1105. as determined above) can be set to specify the full version. If no such option
  1106. is found, the launcher will enumerate the installed Python versions and use
  1107. the latest minor release found for the major version, which is likely,
  1108. although not guaranteed, to be the most recently installed version in that
  1109. family.</p>
  1110. <p>On 64-bit Windows with both 32-bit and 64-bit implementations of the same
  1111. (major.minor) Python version installed, the 64-bit version will always be
  1112. preferred. This will be true for both 32-bit and 64-bit implementations of the
  1113. launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation
  1114. of the specified version if available. This is so the behavior of the launcher
  1115. can be predicted knowing only what versions are installed on the PC and
  1116. without regard to the order in which they were installed (i.e., without knowing
  1117. whether a 32 or 64-bit version of Python and corresponding launcher was
  1118. installed last). As noted above, an optional “-32” or “-64” suffix can be
  1119. used on a version specifier to change this behaviour.</p>
  1120. <p>Examples:</p>
  1121. <ul class="simple">
  1122. <li><p>If no relevant options are set, the commands <code class="docutils literal notranslate"><span class="pre">python</span></code> and
  1123. <code class="docutils literal notranslate"><span class="pre">python2</span></code> will use the latest Python 2.x version installed and
  1124. the command <code class="docutils literal notranslate"><span class="pre">python3</span></code> will use the latest Python 3.x installed.</p></li>
  1125. <li><p>The command <code class="docutils literal notranslate"><span class="pre">python3.7</span></code> will not consult any
  1126. options at all as the versions are fully specified.</p></li>
  1127. <li><p>If <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code>, the commands <code class="docutils literal notranslate"><span class="pre">python</span></code> and <code class="docutils literal notranslate"><span class="pre">python3</span></code> will both use
  1128. the latest installed Python 3 version.</p></li>
  1129. <li><p>If <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3.7-32</span></code>, the command <code class="docutils literal notranslate"><span class="pre">python</span></code> will use the 32-bit
  1130. implementation of 3.7 whereas the command <code class="docutils literal notranslate"><span class="pre">python3</span></code> will use the latest
  1131. installed Python (PY_PYTHON was not considered at all as a major
  1132. version was specified.)</p></li>
  1133. <li><p>If <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code> and <code class="docutils literal notranslate"><span class="pre">PY_PYTHON3=3.7</span></code>, the commands
  1134. <code class="docutils literal notranslate"><span class="pre">python</span></code> and <code class="docutils literal notranslate"><span class="pre">python3</span></code> will both use specifically 3.7</p></li>
  1135. </ul>
  1136. <p>In addition to environment variables, the same settings can be configured
  1137. in the .INI file used by the launcher. The section in the INI file is
  1138. called <code class="docutils literal notranslate"><span class="pre">[defaults]</span></code> and the key name will be the same as the
  1139. environment variables without the leading <code class="docutils literal notranslate"><span class="pre">PY_</span></code> prefix (and note that
  1140. the key names in the INI file are case insensitive.) The contents of
  1141. an environment variable will override things specified in the INI file.</p>
  1142. <p>For example:</p>
  1143. <ul class="simple">
  1144. <li><p>Setting <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3.7</span></code> is equivalent to the INI file containing:</p></li>
  1145. </ul>
  1146. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[defaults]</span>
  1147. <span class="na">python</span><span class="o">=</span><span class="s">3.7</span>
  1148. </pre></div>
  1149. </div>
  1150. <ul class="simple">
  1151. <li><p>Setting <code class="docutils literal notranslate"><span class="pre">PY_PYTHON=3</span></code> and <code class="docutils literal notranslate"><span class="pre">PY_PYTHON3=3.7</span></code> is equivalent to the INI file
  1152. containing:</p></li>
  1153. </ul>
  1154. <div class="highlight-ini notranslate"><div class="highlight"><pre><span></span><span class="k">[defaults]</span>
  1155. <span class="na">python</span><span class="o">=</span><span class="s">3</span>
  1156. <span class="na">python3</span><span class="o">=</span><span class="s">3.7</span>
  1157. </pre></div>
  1158. </div>
  1159. </section>
  1160. </section>
  1161. <section id="diagnostics">
  1162. <h3><span class="section-number">4.8.5. </span>Diagnostics<a class="headerlink" href="#diagnostics" title="Permalink to this headline">¶</a></h3>
  1163. <p>If an environment variable <span class="target" id="index-27"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYLAUNCHER_DEBUG</span></code> is set (to any value), the
  1164. launcher will print diagnostic information to stderr (i.e. to the console).
  1165. While this information manages to be simultaneously verbose <em>and</em> terse, it
  1166. should allow you to see what versions of Python were located, why a
  1167. particular version was chosen and the exact command-line used to execute the
  1168. target Python. It is primarily intended for testing and debugging.</p>
  1169. </section>
  1170. <section id="dry-run">
  1171. <h3><span class="section-number">4.8.6. </span>Dry Run<a class="headerlink" href="#dry-run" title="Permalink to this headline">¶</a></h3>
  1172. <p>If an environment variable <span class="target" id="index-28"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYLAUNCHER_DRYRUN</span></code> is set (to any value),
  1173. the launcher will output the command it would have run, but will not actually
  1174. launch Python. This may be useful for tools that want to use the launcher to
  1175. detect and then launch Python directly. Note that the command written to
  1176. standard output is always encoded using UTF-8, and may not render correctly in
  1177. the console.</p>
  1178. </section>
  1179. <section id="install-on-demand">
  1180. <h3><span class="section-number">4.8.7. </span>Install on demand<a class="headerlink" href="#install-on-demand" title="Permalink to this headline">¶</a></h3>
  1181. <p>If an environment variable <span class="target" id="index-29"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYLAUNCHER_ALLOW_INSTALL</span></code> is set (to any
  1182. value), and the requested Python version is not installed but is available on
  1183. the Microsoft Store, the launcher will attempt to install it. This may require
  1184. user interaction to complete, and you may need to run the command again.</p>
  1185. <p>An additional <span class="target" id="index-30"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYLAUNCHER_ALWAYS_INSTALL</span></code> variable causes the launcher
  1186. to always try to install Python, even if it is detected. This is mainly intended
  1187. for testing (and should be used with <span class="target" id="index-31"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYLAUNCHER_DRYRUN</span></code>).</p>
  1188. </section>
  1189. <section id="return-codes">
  1190. <h3><span class="section-number">4.8.8. </span>Return codes<a class="headerlink" href="#return-codes" title="Permalink to this headline">¶</a></h3>
  1191. <p>The following exit codes may be returned by the Python launcher. Unfortunately,
  1192. there is no way to distinguish these from the exit code of Python itself.</p>
  1193. <p>The names of codes are as used in the sources, and are only for reference. There
  1194. is no way to access or resolve them apart from reading this page. Entries are
  1195. listed in alphabetical order of names.</p>
  1196. <table class="docutils align-default">
  1197. <colgroup>
  1198. <col style="width: 26%" />
  1199. <col style="width: 10%" />
  1200. <col style="width: 64%" />
  1201. </colgroup>
  1202. <thead>
  1203. <tr class="row-odd"><th class="head"><p>Name</p></th>
  1204. <th class="head"><p>Value</p></th>
  1205. <th class="head"><p>Description</p></th>
  1206. </tr>
  1207. </thead>
  1208. <tbody>
  1209. <tr class="row-even"><td><p>RC_BAD_VENV_CFG</p></td>
  1210. <td><p>107</p></td>
  1211. <td><p>A <code class="file docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> was found but is corrupt.</p></td>
  1212. </tr>
  1213. <tr class="row-odd"><td><p>RC_CREATE_PROCESS</p></td>
  1214. <td><p>101</p></td>
  1215. <td><p>Failed to launch Python.</p></td>
  1216. </tr>
  1217. <tr class="row-even"><td><p>RC_INSTALLING</p></td>
  1218. <td><p>111</p></td>
  1219. <td><p>An install was started, but the command will
  1220. need to be re-run after it completes.</p></td>
  1221. </tr>
  1222. <tr class="row-odd"><td><p>RC_INTERNAL_ERROR</p></td>
  1223. <td><p>109</p></td>
  1224. <td><p>Unexpected error. Please report a bug.</p></td>
  1225. </tr>
  1226. <tr class="row-even"><td><p>RC_NO_COMMANDLINE</p></td>
  1227. <td><p>108</p></td>
  1228. <td><p>Unable to obtain command line from the
  1229. operating system.</p></td>
  1230. </tr>
  1231. <tr class="row-odd"><td><p>RC_NO_PYTHON</p></td>
  1232. <td><p>103</p></td>
  1233. <td><p>Unable to locate the requested version.</p></td>
  1234. </tr>
  1235. <tr class="row-even"><td><p>RC_NO_VENV_CFG</p></td>
  1236. <td><p>106</p></td>
  1237. <td><p>A <code class="file docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> was required but not
  1238. found.</p></td>
  1239. </tr>
  1240. </tbody>
  1241. </table>
  1242. </section>
  1243. </section>
  1244. <section id="finding-modules">
  1245. <span id="windows-finding-modules"></span><h2><span class="section-number">4.9. </span>Finding modules<a class="headerlink" href="#finding-modules" title="Permalink to this headline">¶</a></h2>
  1246. <p>These notes supplement the description at <a class="reference internal" href="../library/sys_path_init.html#sys-path-init"><span class="std std-ref">The initialization of the sys.path module search path</span></a> with
  1247. detailed Windows notes.</p>
  1248. <p>When no <code class="docutils literal notranslate"><span class="pre">._pth</span></code> file is found, this is how <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> is populated on
  1249. Windows:</p>
  1250. <ul class="simple">
  1251. <li><p>An empty entry is added at the start, which corresponds to the current
  1252. directory.</p></li>
  1253. <li><p>If the environment variable <span class="target" id="index-32"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> exists, as described in
  1254. <a class="reference internal" href="cmdline.html#using-on-envvars"><span class="std std-ref">Environment variables</span></a>, its entries are added next. Note that on Windows,
  1255. paths in this variable must be separated by semicolons, to distinguish them
  1256. from the colon used in drive identifiers (<code class="docutils literal notranslate"><span class="pre">C:\</span></code> etc.).</p></li>
  1257. <li><p>Additional “application paths” can be added in the registry as subkeys of
  1258. <code class="samp docutils literal notranslate"><span class="pre">\SOFTWARE\Python\PythonCore{version}\PythonPath</span></code> under both the
  1259. <code class="docutils literal notranslate"><span class="pre">HKEY_CURRENT_USER</span></code> and <code class="docutils literal notranslate"><span class="pre">HKEY_LOCAL_MACHINE</span></code> hives. Subkeys which have
  1260. semicolon-delimited path strings as their default value will cause each path
  1261. to be added to <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>. (Note that all known installers only use
  1262. HKLM, so HKCU is typically empty.)</p></li>
  1263. <li><p>If the environment variable <span class="target" id="index-33"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> is set, it is assumed as
  1264. “Python Home”. Otherwise, the path of the main Python executable is used to
  1265. locate a “landmark file” (either <code class="docutils literal notranslate"><span class="pre">Lib\os.py</span></code> or <code class="docutils literal notranslate"><span class="pre">pythonXY.zip</span></code>) to deduce
  1266. the “Python Home”. If a Python home is found, the relevant sub-directories
  1267. added to <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> (<code class="docutils literal notranslate"><span class="pre">Lib</span></code>, <code class="docutils literal notranslate"><span class="pre">plat-win</span></code>, etc) are based on that
  1268. folder. Otherwise, the core Python path is constructed from the PythonPath
  1269. stored in the registry.</p></li>
  1270. <li><p>If the Python Home cannot be located, no <span class="target" id="index-34"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> is specified in
  1271. the environment, and no registry entries can be found, a default path with
  1272. relative entries is used (e.g. <code class="docutils literal notranslate"><span class="pre">.\Lib;.\plat-win</span></code>, etc).</p></li>
  1273. </ul>
  1274. <p>If a <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> file is found alongside the main executable or in the
  1275. directory one level above the executable, the following variations apply:</p>
  1276. <ul class="simple">
  1277. <li><p>If <code class="docutils literal notranslate"><span class="pre">home</span></code> is an absolute path and <span class="target" id="index-35"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> is not set, this
  1278. path is used instead of the path to the main executable when deducing the
  1279. home location.</p></li>
  1280. </ul>
  1281. <p>The end result of all this is:</p>
  1282. <ul class="simple">
  1283. <li><p>When running <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code>, or any other .exe in the main Python
  1284. directory (either an installed version, or directly from the PCbuild
  1285. directory), the core path is deduced, and the core paths in the registry are
  1286. ignored. Other “application paths” in the registry are always read.</p></li>
  1287. <li><p>When Python is hosted in another .exe (different directory, embedded via COM,
  1288. etc), the “Python Home” will not be deduced, so the core path from the
  1289. registry is used. Other “application paths” in the registry are always read.</p></li>
  1290. <li><p>If Python can’t find its home and there are no registry value (frozen .exe,
  1291. some very strange installation setup) you get a path with some default, but
  1292. relative, paths.</p></li>
  1293. </ul>
  1294. <p>For those who want to bundle Python into their application or distribution, the
  1295. following advice will prevent conflicts with other installations:</p>
  1296. <ul class="simple">
  1297. <li><p>Include a <code class="docutils literal notranslate"><span class="pre">._pth</span></code> file alongside your executable containing the
  1298. directories to include. This will ignore paths listed in the registry and
  1299. environment variables, and also ignore <a class="reference internal" href="../library/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> unless <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code> is
  1300. listed.</p></li>
  1301. <li><p>If you are loading <code class="file docutils literal notranslate"><span class="pre">python3.dll</span></code> or <code class="file docutils literal notranslate"><span class="pre">python37.dll</span></code> in your own
  1302. executable, explicitly call <a class="reference internal" href="../c-api/init.html#c.Py_SetPath" title="Py_SetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetPath()</span></code></a> or (at least)
  1303. <a class="reference internal" href="../c-api/init.html#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a> before <a class="reference internal" href="../c-api/init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p></li>
  1304. <li><p>Clear and/or overwrite <span class="target" id="index-36"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> and set <span class="target" id="index-37"></span><a class="reference internal" href="cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a>
  1305. before launching <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> from your application.</p></li>
  1306. <li><p>If you cannot use the previous suggestions (for example, you are a
  1307. distribution that allows people to run <code class="file docutils literal notranslate"><span class="pre">python.exe</span></code> directly), ensure
  1308. that the landmark file (<code class="file docutils literal notranslate"><span class="pre">Lib\os.py</span></code>) exists in your install directory.
  1309. (Note that it will not be detected inside a ZIP file, but a correctly named
  1310. ZIP file will be detected instead.)</p></li>
  1311. </ul>
  1312. <p>These will ensure that the files in a system-wide installation will not take
  1313. precedence over the copy of the standard library bundled with your application.
  1314. Otherwise, your users may experience problems using your application. Note that
  1315. the first suggestion is the best, as the others may still be susceptible to
  1316. non-standard paths in the registry and user site-packages.</p>
  1317. <div class="versionchanged">
  1318. <p><span class="versionmodified changed">Changed in version 3.6: </span></p>
  1319. <blockquote>
  1320. <div><ul class="simple">
  1321. <li><p>Adds <code class="docutils literal notranslate"><span class="pre">._pth</span></code> file support and removes <code class="docutils literal notranslate"><span class="pre">applocal</span></code> option from
  1322. <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code>.</p></li>
  1323. <li><p>Adds <code class="file docutils literal notranslate"><span class="pre">python</span><em><span class="pre">XX</span></em><span class="pre">.zip</span></code> as a potential landmark when directly adjacent
  1324. to the executable.</p></li>
  1325. </ul>
  1326. </div></blockquote>
  1327. </div>
  1328. <div class="deprecated">
  1329. <p><span class="versionmodified deprecated">Deprecated since version 3.6: </span></p>
  1330. <blockquote>
  1331. <div><p>Modules specified in the registry under <code class="docutils literal notranslate"><span class="pre">Modules</span></code> (not <code class="docutils literal notranslate"><span class="pre">PythonPath</span></code>)
  1332. may be imported by <a class="reference internal" href="../library/importlib.html#importlib.machinery.WindowsRegistryFinder" title="importlib.machinery.WindowsRegistryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.WindowsRegistryFinder</span></code></a>.
  1333. This finder is enabled on Windows in 3.6.0 and earlier, but may need to
  1334. be explicitly added to <a class="reference internal" href="../library/sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> in the future.</p>
  1335. </div></blockquote>
  1336. </div>
  1337. </section>
  1338. <section id="additional-modules">
  1339. <h2><span class="section-number">4.10. </span>Additional modules<a class="headerlink" href="#additional-modules" title="Permalink to this headline">¶</a></h2>
  1340. <p>Even though Python aims to be portable among all platforms, there are features
  1341. that are unique to Windows. A couple of modules, both in the standard library
  1342. and external, and snippets exist to use these features.</p>
  1343. <p>The Windows-specific standard modules are documented in
  1344. <a class="reference internal" href="../library/windows.html#mswin-specific-services"><span class="std std-ref">MS Windows Specific Services</span></a>.</p>
  1345. <section id="pywin32">
  1346. <h3><span class="section-number">4.10.1. </span>PyWin32<a class="headerlink" href="#pywin32" title="Permalink to this headline">¶</a></h3>
  1347. <p>The <a class="reference external" href="https://pypi.org/project/pywin32">PyWin32</a> module by Mark Hammond
  1348. is a collection of modules for advanced Windows-specific support. This includes
  1349. utilities for:</p>
  1350. <ul class="simple">
  1351. <li><p><a class="reference external" href="https://docs.microsoft.com/en-us/windows/win32/com/component-object-model--com--portal">Component Object Model</a>
  1352. (COM)</p></li>
  1353. <li><p>Win32 API calls</p></li>
  1354. <li><p>Registry</p></li>
  1355. <li><p>Event log</p></li>
  1356. <li><p><a class="reference external" href="https://docs.microsoft.com/en-us/cpp/mfc/mfc-desktop-applications">Microsoft Foundation Classes</a>
  1357. (MFC) user interfaces</p></li>
  1358. </ul>
  1359. <p><a class="reference external" href="https://web.archive.org/web/20060524042422/https://www.python.org/windows/pythonwin/">PythonWin</a> is a sample MFC application
  1360. shipped with PyWin32. It is an embeddable IDE with a built-in debugger.</p>
  1361. <div class="admonition seealso">
  1362. <p class="admonition-title">See also</p>
  1363. <dl class="simple">
  1364. <dt><a class="reference external" href="http://timgolden.me.uk/python/win32_how_do_i.html">Win32 How Do I…?</a></dt><dd><p>by Tim Golden</p>
  1365. </dd>
  1366. <dt><a class="reference external" href="https://www.boddie.org.uk/python/COM.html">Python and COM</a></dt><dd><p>by David and Paul Boddie</p>
  1367. </dd>
  1368. </dl>
  1369. </div>
  1370. </section>
  1371. <section id="cx-freeze">
  1372. <h3><span class="section-number">4.10.2. </span>cx_Freeze<a class="headerlink" href="#cx-freeze" title="Permalink to this headline">¶</a></h3>
  1373. <p><a class="reference external" href="https://cx-freeze.readthedocs.io/en/latest/">cx_Freeze</a>
  1374. wraps Python scripts into executable Windows programs
  1375. (<code class="file docutils literal notranslate"><em><span class="pre">*</span></em><span class="pre">.exe</span></code> files). When you have done this, you can distribute your
  1376. application without requiring your users to install Python.</p>
  1377. </section>
  1378. </section>
  1379. <section id="compiling-python-on-windows">
  1380. <h2><span class="section-number">4.11. </span>Compiling Python on Windows<a class="headerlink" href="#compiling-python-on-windows" title="Permalink to this headline">¶</a></h2>
  1381. <p>If you want to compile CPython yourself, first thing you should do is get the
  1382. <a class="reference external" href="https://www.python.org/downloads/source/">source</a>. You can download either the
  1383. latest release’s source or just grab a fresh <a class="reference external" href="https://devguide.python.org/setup/#get-the-source-code">checkout</a>.</p>
  1384. <p>The source tree contains a build solution and project files for Microsoft
  1385. Visual Studio, which is the compiler used to build the official Python
  1386. releases. These files are in the <code class="file docutils literal notranslate"><span class="pre">PCbuild</span></code> directory.</p>
  1387. <p>Check <code class="file docutils literal notranslate"><span class="pre">PCbuild/readme.txt</span></code> for general information on the build process.</p>
  1388. <p>For extension modules, consult <a class="reference internal" href="../extending/windows.html#building-on-windows"><span class="std std-ref">Building C and C++ Extensions on Windows</span></a>.</p>
  1389. </section>
  1390. <section id="other-platforms">
  1391. <h2><span class="section-number">4.12. </span>Other Platforms<a class="headerlink" href="#other-platforms" title="Permalink to this headline">¶</a></h2>
  1392. <p>With ongoing development of Python, some platforms that used to be supported
  1393. earlier are no longer supported (due to the lack of users or developers).
  1394. Check <span class="target" id="index-38"></span><a class="pep reference external" href="https://peps.python.org/pep-0011/"><strong>PEP 11</strong></a> for details on all unsupported platforms.</p>
  1395. <ul class="simple">
  1396. <li><p><a class="reference external" href="https://pythonce.sourceforge.net/">Windows CE</a> is
  1397. <a class="reference external" href="https://github.com/python/cpython/issues/71542">no longer supported</a>
  1398. since Python 3 (if it ever was).</p></li>
  1399. <li><p>The <a class="reference external" href="https://cygwin.com/">Cygwin</a> installer offers to install the
  1400. <a class="reference external" href="https://cygwin.com/packages/summary/python3.html">Python interpreter</a>
  1401. as well</p></li>
  1402. </ul>
  1403. <p>See <a class="reference external" href="https://www.python.org/downloads/windows/">Python for Windows</a>
  1404. for detailed information about platforms with pre-compiled installers.</p>
  1405. </section>
  1406. </section>
  1407. <div class="clearer"></div>
  1408. </div>
  1409. </div>
  1410. </div>
  1411. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1412. <div class="sphinxsidebarwrapper">
  1413. <div>
  1414. <h3><a href="../contents.html">Table of Contents</a></h3>
  1415. <ul>
  1416. <li><a class="reference internal" href="#">4. Using Python on Windows</a><ul>
  1417. <li><a class="reference internal" href="#the-full-installer">4.1. The full installer</a><ul>
  1418. <li><a class="reference internal" href="#installation-steps">4.1.1. Installation steps</a></li>
  1419. <li><a class="reference internal" href="#removing-the-max-path-limitation">4.1.2. Removing the MAX_PATH Limitation</a></li>
  1420. <li><a class="reference internal" href="#installing-without-ui">4.1.3. Installing Without UI</a></li>
  1421. <li><a class="reference internal" href="#installing-without-downloading">4.1.4. Installing Without Downloading</a></li>
  1422. <li><a class="reference internal" href="#modifying-an-install">4.1.5. Modifying an install</a></li>
  1423. </ul>
  1424. </li>
  1425. <li><a class="reference internal" href="#the-microsoft-store-package">4.2. The Microsoft Store package</a><ul>
  1426. <li><a class="reference internal" href="#known-issues">4.2.1. Known issues</a><ul>
  1427. <li><a class="reference internal" href="#redirection-of-local-data-registry-and-temporary-paths">4.2.1.1. Redirection of local data, registry, and temporary paths</a></li>
  1428. </ul>
  1429. </li>
  1430. </ul>
  1431. </li>
  1432. <li><a class="reference internal" href="#the-nuget-org-packages">4.3. The nuget.org packages</a></li>
  1433. <li><a class="reference internal" href="#the-embeddable-package">4.4. The embeddable package</a><ul>
  1434. <li><a class="reference internal" href="#python-application">4.4.1. Python Application</a></li>
  1435. <li><a class="reference internal" href="#embedding-python">4.4.2. Embedding Python</a></li>
  1436. </ul>
  1437. </li>
  1438. <li><a class="reference internal" href="#alternative-bundles">4.5. Alternative bundles</a></li>
  1439. <li><a class="reference internal" href="#configuring-python">4.6. Configuring Python</a><ul>
  1440. <li><a class="reference internal" href="#excursus-setting-environment-variables">4.6.1. Excursus: Setting environment variables</a></li>
  1441. <li><a class="reference internal" href="#finding-the-python-executable">4.6.2. Finding the Python executable</a></li>
  1442. </ul>
  1443. </li>
  1444. <li><a class="reference internal" href="#utf-8-mode">4.7. UTF-8 mode</a></li>
  1445. <li><a class="reference internal" href="#python-launcher-for-windows">4.8. Python Launcher for Windows</a><ul>
  1446. <li><a class="reference internal" href="#getting-started">4.8.1. Getting started</a><ul>
  1447. <li><a class="reference internal" href="#from-the-command-line">4.8.1.1. From the command-line</a></li>
  1448. <li><a class="reference internal" href="#virtual-environments">4.8.1.2. Virtual environments</a></li>
  1449. <li><a class="reference internal" href="#from-a-script">4.8.1.3. From a script</a></li>
  1450. <li><a class="reference internal" href="#from-file-associations">4.8.1.4. From file associations</a></li>
  1451. </ul>
  1452. </li>
  1453. <li><a class="reference internal" href="#shebang-lines">4.8.2. Shebang Lines</a></li>
  1454. <li><a class="reference internal" href="#arguments-in-shebang-lines">4.8.3. Arguments in shebang lines</a></li>
  1455. <li><a class="reference internal" href="#customization">4.8.4. Customization</a><ul>
  1456. <li><a class="reference internal" href="#customization-via-ini-files">4.8.4.1. Customization via INI files</a></li>
  1457. <li><a class="reference internal" href="#customizing-default-python-versions">4.8.4.2. Customizing default Python versions</a></li>
  1458. </ul>
  1459. </li>
  1460. <li><a class="reference internal" href="#diagnostics">4.8.5. Diagnostics</a></li>
  1461. <li><a class="reference internal" href="#dry-run">4.8.6. Dry Run</a></li>
  1462. <li><a class="reference internal" href="#install-on-demand">4.8.7. Install on demand</a></li>
  1463. <li><a class="reference internal" href="#return-codes">4.8.8. Return codes</a></li>
  1464. </ul>
  1465. </li>
  1466. <li><a class="reference internal" href="#finding-modules">4.9. Finding modules</a></li>
  1467. <li><a class="reference internal" href="#additional-modules">4.10. Additional modules</a><ul>
  1468. <li><a class="reference internal" href="#pywin32">4.10.1. PyWin32</a></li>
  1469. <li><a class="reference internal" href="#cx-freeze">4.10.2. cx_Freeze</a></li>
  1470. </ul>
  1471. </li>
  1472. <li><a class="reference internal" href="#compiling-python-on-windows">4.11. Compiling Python on Windows</a></li>
  1473. <li><a class="reference internal" href="#other-platforms">4.12. Other Platforms</a></li>
  1474. </ul>
  1475. </li>
  1476. </ul>
  1477. </div>
  1478. <div>
  1479. <h4>Previous topic</h4>
  1480. <p class="topless"><a href="configure.html"
  1481. title="previous chapter"><span class="section-number">3. </span>Configure Python</a></p>
  1482. </div>
  1483. <div>
  1484. <h4>Next topic</h4>
  1485. <p class="topless"><a href="mac.html"
  1486. title="next chapter"><span class="section-number">5. </span>Using Python on a Mac</a></p>
  1487. </div>
  1488. <div role="note" aria-label="source link">
  1489. <h3>This Page</h3>
  1490. <ul class="this-page-menu">
  1491. <li><a href="../bugs.html">Report a Bug</a></li>
  1492. <li>
  1493. <a href="https://github.com/python/cpython/blob/main/Doc/using/windows.rst"
  1494. rel="nofollow">Show Source
  1495. </a>
  1496. </li>
  1497. </ul>
  1498. </div>
  1499. </div>
  1500. </div>
  1501. <div class="clearer"></div>
  1502. </div>
  1503. <div class="related" role="navigation" aria-label="related navigation">
  1504. <h3>Navigation</h3>
  1505. <ul>
  1506. <li class="right" style="margin-right: 10px">
  1507. <a href="../genindex.html" title="General Index"
  1508. >index</a></li>
  1509. <li class="right" >
  1510. <a href="../py-modindex.html" title="Python Module Index"
  1511. >modules</a> |</li>
  1512. <li class="right" >
  1513. <a href="mac.html" title="5. Using Python on a Mac"
  1514. >next</a> |</li>
  1515. <li class="right" >
  1516. <a href="configure.html" title="3. Configure Python"
  1517. >previous</a> |</li>
  1518. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1519. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1520. <li class="switchers">
  1521. <div class="language_switcher_placeholder"></div>
  1522. <div class="version_switcher_placeholder"></div>
  1523. </li>
  1524. <li>
  1525. </li>
  1526. <li id="cpython-language-and-version">
  1527. <a href="../index.html">3.12.0 Documentation</a> &#187;
  1528. </li>
  1529. <li class="nav-item nav-item-1"><a href="index.html" >Python Setup and Usage</a> &#187;</li>
  1530. <li class="nav-item nav-item-this"><a href=""><span class="section-number">4. </span>Using Python on Windows</a></li>
  1531. <li class="right">
  1532. <div class="inline-search" role="search">
  1533. <form class="inline-search" action="../search.html" method="get">
  1534. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  1535. <input type="submit" value="Go" />
  1536. </form>
  1537. </div>
  1538. |
  1539. </li>
  1540. <li class="right">
  1541. <label class="theme-selector-label">
  1542. Theme
  1543. <select class="theme-selector" oninput="activateTheme(this.value)">
  1544. <option value="auto" selected>Auto</option>
  1545. <option value="light">Light</option>
  1546. <option value="dark">Dark</option>
  1547. </select>
  1548. </label> |</li>
  1549. </ul>
  1550. </div>
  1551. <div class="footer">
  1552. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  1553. <br />
  1554. This page is licensed under the Python Software Foundation License Version 2.
  1555. <br />
  1556. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1557. <br />
  1558. See <a href="/license.html">History and License</a> for more information.<br />
  1559. <br />
  1560. The Python Software Foundation is a non-profit corporation.
  1561. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1562. <br />
  1563. <br />
  1564. Last updated on Oct 02, 2023.
  1565. <a href="/bugs.html">Found a bug</a>?
  1566. <br />
  1567. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  1568. </div>
  1569. </body>
  1570. </html>