Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: none |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 2 | |
| 3 | .. _using-on-windows: |
| 4 | |
| 5 | ************************* |
| 6 | Using Python on Windows |
| 7 | ************************* |
| 8 | |
| 9 | .. sectionauthor:: Robert Lehmann <lehmannro@gmail.com> |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 10 | .. sectionauthor:: Steve Dower <steve.dower@microsoft.com> |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 11 | |
| 12 | This document aims to give an overview of Windows-specific behaviour you should |
| 13 | know about when using Python on Microsoft Windows. |
| 14 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 15 | Unlike most Unix systems and services, Windows does not include a system |
| 16 | supported installation of Python. To make Python available, the CPython team |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 17 | has compiled Windows installers (MSI packages) with every `release |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 18 | <https://www.python.org/download/releases/>`_ for many years. These installers |
Steve Dower | 9a578d9 | 2015-05-02 22:28:58 -0700 | [diff] [blame] | 19 | are primarily intended to add a per-user installation of Python, with the |
| 20 | core interpreter and library being used by a single user. The installer is also |
| 21 | able to install for all users of a single machine, and a separate ZIP file is |
| 22 | available for application-local distributions. |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 23 | |
Brett Cannon | 85622e4 | 2015-12-27 12:08:37 -0800 | [diff] [blame] | 24 | As specified in :pep:`11`, a Python release only supports a Windows platform |
| 25 | while Microsoft considers the platform under extended support. This means that |
Steve Dower | f333fd8 | 2016-09-09 09:40:06 -0700 | [diff] [blame] | 26 | Python |version| supports Windows Vista and newer. If you require Windows XP |
| 27 | support then please install Python 3.4. |
Brett Cannon | 85622e4 | 2015-12-27 12:08:37 -0800 | [diff] [blame] | 28 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 29 | There are a number of different installers available for Windows, each with |
| 30 | certain benefits and downsides. |
| 31 | |
| 32 | :ref:`windows-full` contains all components and is the best option for |
| 33 | developers using Python for any kind of project. |
| 34 | |
| 35 | :ref:`windows-store` is a simple installation of Python that is suitable for |
| 36 | running scripts and packages, and using IDLE or other development environments. |
| 37 | It requires Windows 10, but can be safely installed without corrupting other |
| 38 | programs. It also provides many convenient commands for launching Python and |
| 39 | its tools. |
| 40 | |
| 41 | :ref:`windows-nuget` are lightweight installations intended for continuous |
| 42 | integration systems. It can be used to build Python packages or run scripts, |
| 43 | but is not updateable and has no user interface tools. |
| 44 | |
| 45 | :ref:`windows-embeddable` is a minimal package of Python suitable for |
| 46 | embedding into a larger application. |
| 47 | |
| 48 | |
| 49 | .. _windows-full: |
| 50 | |
| 51 | The full installer |
| 52 | ================== |
| 53 | |
| 54 | Installation steps |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 55 | ------------------ |
| 56 | |
Steve Dower | f333fd8 | 2016-09-09 09:40:06 -0700 | [diff] [blame] | 57 | Four Python |version| installers are available for download - two each for the |
| 58 | 32-bit and 64-bit versions of the interpreter. The *web installer* is a small |
| 59 | initial download, and it will automatically download the required components as |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 60 | necessary. The *offline installer* includes the components necessary for a |
| 61 | default installation and only requires an internet connection for optional |
| 62 | features. See :ref:`install-layout-option` for other ways to avoid downloading |
| 63 | during installation. |
| 64 | |
Steve Dower | 9a578d9 | 2015-05-02 22:28:58 -0700 | [diff] [blame] | 65 | After starting the installer, one of two options may be selected: |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 66 | |
| 67 | .. image:: win_installer.png |
| 68 | |
Steve Dower | 9a578d9 | 2015-05-02 22:28:58 -0700 | [diff] [blame] | 69 | If you select "Install Now": |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 70 | |
Steve Dower | 9a578d9 | 2015-05-02 22:28:58 -0700 | [diff] [blame] | 71 | * You will *not* need to be an administrator (unless a system update for the |
Steve Dower | eb3c16d | 2015-08-08 09:09:01 -0700 | [diff] [blame] | 72 | C Runtime Library is required or you install the :ref:`launcher` for all |
| 73 | users) |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 74 | * Python will be installed into your user directory |
Steve Dower | eb3c16d | 2015-08-08 09:09:01 -0700 | [diff] [blame] | 75 | * The :ref:`launcher` will be installed according to the option at the bottom |
Zachary Ware | 87ec85f | 2016-07-28 19:00:53 -0500 | [diff] [blame] | 76 | of the first page |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 77 | * The standard library, test suite, launcher and pip will be installed |
Steve Dower | 9a578d9 | 2015-05-02 22:28:58 -0700 | [diff] [blame] | 78 | * If selected, the install directory will be added to your :envvar:`PATH` |
| 79 | * Shortcuts will only be visible for the current user |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 80 | |
| 81 | Selecting "Customize installation" will allow you to select the features to |
| 82 | install, the installation location and other options or post-install actions. |
| 83 | To install debugging symbols or binaries, you will need to use this option. |
| 84 | |
Steve Dower | 9a578d9 | 2015-05-02 22:28:58 -0700 | [diff] [blame] | 85 | To perform an all-users installation, you should select "Customize |
| 86 | installation". In this case: |
| 87 | |
| 88 | * You may be required to provide administrative credentials or approval |
| 89 | * Python will be installed into the Program Files directory |
| 90 | * The :ref:`launcher` will be installed into the Windows directory |
| 91 | * Optional features may be selected during installation |
Steve Dower | eb3c16d | 2015-08-08 09:09:01 -0700 | [diff] [blame] | 92 | * The standard library can be pre-compiled to bytecode |
Steve Dower | 9a578d9 | 2015-05-02 22:28:58 -0700 | [diff] [blame] | 93 | * If selected, the install directory will be added to the system :envvar:`PATH` |
| 94 | * Shortcuts are available for all users |
| 95 | |
Steve Dower | 19ab0fd | 2016-09-06 20:40:11 -0700 | [diff] [blame] | 96 | .. _max-path: |
| 97 | |
| 98 | Removing the MAX_PATH Limitation |
| 99 | -------------------------------- |
| 100 | |
| 101 | Windows historically has limited path lengths to 260 characters. This meant that |
| 102 | paths longer than this would not resolve and errors would result. |
| 103 | |
| 104 | In the latest versions of Windows, this limitation can be expanded to |
| 105 | approximately 32,000 characters. Your administrator will need to activate the |
| 106 | "Enable Win32 long paths" group policy, or set the registry value |
| 107 | ``HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled`` |
| 108 | to ``1``. |
| 109 | |
| 110 | This allows the :func:`open` function, the :mod:`os` module and most other |
Miss Islington (bot) | a169961 | 2020-06-01 08:53:27 -0700 | [diff] [blame] | 111 | path functionality to accept and return paths longer than 260 characters. |
Steve Dower | 19ab0fd | 2016-09-06 20:40:11 -0700 | [diff] [blame] | 112 | |
| 113 | After changing the above option, no further configuration is required. |
| 114 | |
| 115 | .. versionchanged:: 3.6 |
| 116 | |
| 117 | Support for long paths was enabled in Python. |
| 118 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 119 | .. _install-quiet-option: |
| 120 | |
| 121 | Installing Without UI |
| 122 | --------------------- |
| 123 | |
| 124 | All of the options available in the installer UI can also be specified from the |
| 125 | command line, allowing scripted installers to replicate an installation on many |
| 126 | machines without user interaction. These options may also be set without |
| 127 | suppressing the UI in order to change some of the defaults. |
| 128 | |
| 129 | To completely hide the installer UI and install Python silently, pass the |
Steve Dower | cb0afc8 | 2015-09-11 08:47:42 -0700 | [diff] [blame] | 130 | ``/quiet`` option. To skip past the user interaction but still display |
| 131 | progress and errors, pass the ``/passive`` option. The ``/uninstall`` |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 132 | option may be passed to immediately begin removing Python - no prompt will be |
| 133 | displayed. |
| 134 | |
| 135 | All other options are passed as ``name=value``, where the value is usually |
| 136 | ``0`` to disable a feature, ``1`` to enable a feature, or a path. The full list |
| 137 | of available options is shown below. |
| 138 | |
| 139 | +---------------------------+--------------------------------------+--------------------------+ |
| 140 | | Name | Description | Default | |
| 141 | +===========================+======================================+==========================+ |
Steve Dower | 9b608e5 | 2015-09-13 14:39:26 -0700 | [diff] [blame] | 142 | | InstallAllUsers | Perform a system-wide installation. | 0 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 143 | +---------------------------+--------------------------------------+--------------------------+ |
| 144 | | TargetDir | The installation directory | Selected based on | |
| 145 | | | | InstallAllUsers | |
| 146 | +---------------------------+--------------------------------------+--------------------------+ |
| 147 | | DefaultAllUsersTargetDir | The default installation directory | :file:`%ProgramFiles%\\\ | |
| 148 | | | for all-user installs | Python X.Y` or :file:`\ | |
| 149 | | | | %ProgramFiles(x86)%\\\ | |
| 150 | | | | Python X.Y` | |
| 151 | +---------------------------+--------------------------------------+--------------------------+ |
| 152 | | DefaultJustForMeTargetDir | The default install directory for | :file:`%LocalAppData%\\\ | |
| 153 | | | just-for-me installs | Programs\\PythonXY` or | |
| 154 | | | | :file:`%LocalAppData%\\\ | |
mrh1997 | 1e2ad6c | 2019-04-13 00:26:47 +0200 | [diff] [blame] | 155 | | | | Programs\\PythonXY-32` or| |
| 156 | | | | :file:`%LocalAppData%\\\ | |
| 157 | | | | Programs\\PythonXY-64` | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 158 | +---------------------------+--------------------------------------+--------------------------+ |
| 159 | | DefaultCustomTargetDir | The default custom install directory | (empty) | |
| 160 | | | displayed in the UI | | |
| 161 | +---------------------------+--------------------------------------+--------------------------+ |
| 162 | | AssociateFiles | Create file associations if the | 1 | |
| 163 | | | launcher is also installed. | | |
| 164 | +---------------------------+--------------------------------------+--------------------------+ |
| 165 | | CompileAll | Compile all ``.py`` files to | 0 | |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 166 | | | ``.pyc``. | | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 167 | +---------------------------+--------------------------------------+--------------------------+ |
| 168 | | PrependPath | Add install and Scripts directories | 0 | |
Segev Finer | 8e7e8bd | 2018-07-30 20:11:30 +0300 | [diff] [blame] | 169 | | | to :envvar:`PATH` and ``.PY`` to | | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 170 | | | :envvar:`PATHEXT` | | |
| 171 | +---------------------------+--------------------------------------+--------------------------+ |
Steve Dower | 1d4880d | 2015-05-03 14:54:32 -0700 | [diff] [blame] | 172 | | Shortcuts | Create shortcuts for the interpreter,| 1 | |
| 173 | | | documentation and IDLE if installed. | | |
| 174 | +---------------------------+--------------------------------------+--------------------------+ |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 175 | | Include_doc | Install Python manual | 1 | |
| 176 | +---------------------------+--------------------------------------+--------------------------+ |
| 177 | | Include_debug | Install debug binaries | 0 | |
| 178 | +---------------------------+--------------------------------------+--------------------------+ |
| 179 | | Include_dev | Install developer headers and | 1 | |
| 180 | | | libraries | | |
| 181 | +---------------------------+--------------------------------------+--------------------------+ |
| 182 | | Include_exe | Install :file:`python.exe` and | 1 | |
| 183 | | | related files | | |
| 184 | +---------------------------+--------------------------------------+--------------------------+ |
| 185 | | Include_launcher | Install :ref:`launcher`. | 1 | |
| 186 | +---------------------------+--------------------------------------+--------------------------+ |
Steve Dower | 2237bdc | 2015-07-16 16:33:55 -0700 | [diff] [blame] | 187 | | InstallLauncherAllUsers | Installs :ref:`launcher` for all | 1 | |
| 188 | | | users. | | |
| 189 | +---------------------------+--------------------------------------+--------------------------+ |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 190 | | Include_lib | Install standard library and | 1 | |
| 191 | | | extension modules | | |
| 192 | +---------------------------+--------------------------------------+--------------------------+ |
| 193 | | Include_pip | Install bundled pip and setuptools | 1 | |
| 194 | +---------------------------+--------------------------------------+--------------------------+ |
| 195 | | Include_symbols | Install debugging symbols (`*`.pdb) | 0 | |
| 196 | +---------------------------+--------------------------------------+--------------------------+ |
| 197 | | Include_tcltk | Install Tcl/Tk support and IDLE | 1 | |
| 198 | +---------------------------+--------------------------------------+--------------------------+ |
| 199 | | Include_test | Install standard library test suite | 1 | |
| 200 | +---------------------------+--------------------------------------+--------------------------+ |
| 201 | | Include_tools | Install utility scripts | 1 | |
| 202 | +---------------------------+--------------------------------------+--------------------------+ |
Steve Dower | a3d03ec | 2015-07-18 09:27:52 -0700 | [diff] [blame] | 203 | | LauncherOnly | Only installs the launcher. This | 0 | |
| 204 | | | will override most other options. | | |
| 205 | +---------------------------+--------------------------------------+--------------------------+ |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 206 | | SimpleInstall | Disable most install UI | 0 | |
| 207 | +---------------------------+--------------------------------------+--------------------------+ |
Steve Dower | 2237bdc | 2015-07-16 16:33:55 -0700 | [diff] [blame] | 208 | | SimpleInstallDescription | A custom message to display when the | (empty) | |
| 209 | | | simplified install UI is used. | | |
| 210 | +---------------------------+--------------------------------------+--------------------------+ |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 211 | |
| 212 | For example, to silently install a default, system-wide Python installation, |
| 213 | you could use the following command (from an elevated command prompt):: |
| 214 | |
Steve Dower | 7a177c0 | 2019-06-26 08:55:57 -0700 | [diff] [blame] | 215 | python-3.9.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 216 | |
| 217 | To allow users to easily install a personal copy of Python without the test |
Steve Dower | 2237bdc | 2015-07-16 16:33:55 -0700 | [diff] [blame] | 218 | suite, you could provide a shortcut with the following command. This will |
| 219 | display a simplified initial page and disallow customization:: |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 220 | |
Steve Dower | 7a177c0 | 2019-06-26 08:55:57 -0700 | [diff] [blame] | 221 | python-3.9.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0 |
Steve Dower | 2237bdc | 2015-07-16 16:33:55 -0700 | [diff] [blame] | 222 | SimpleInstall=1 SimpleInstallDescription="Just for me, no test suite." |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 223 | |
| 224 | (Note that omitting the launcher also omits file associations, and is only |
| 225 | recommended for per-user installs when there is also a system-wide installation |
| 226 | that included the launcher.) |
| 227 | |
Steve Dower | 2434aa2 | 2015-07-18 09:28:19 -0700 | [diff] [blame] | 228 | The options listed above can also be provided in a file named ``unattend.xml`` |
| 229 | alongside the executable. This file specifies a list of options and values. |
| 230 | When a value is provided as an attribute, it will be converted to a number if |
| 231 | possible. Values provided as element text are always left as strings. This |
Segev Finer | 8e7e8bd | 2018-07-30 20:11:30 +0300 | [diff] [blame] | 232 | example file sets the same options as the previous example: |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 233 | |
| 234 | .. code-block:: xml |
Steve Dower | 2434aa2 | 2015-07-18 09:28:19 -0700 | [diff] [blame] | 235 | |
| 236 | <Options> |
| 237 | <Option Name="InstallAllUsers" Value="no" /> |
| 238 | <Option Name="Include_launcher" Value="0" /> |
| 239 | <Option Name="Include_test" Value="no" /> |
| 240 | <Option Name="SimpleInstall" Value="yes" /> |
| 241 | <Option Name="SimpleInstallDescription">Just for me, no test suite</Option> |
| 242 | </Options> |
| 243 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 244 | .. _install-layout-option: |
| 245 | |
| 246 | Installing Without Downloading |
| 247 | ------------------------------ |
| 248 | |
| 249 | As some features of Python are not included in the initial installer download, |
| 250 | selecting those features may require an internet connection. To avoid this |
| 251 | need, all possible components may be downloaded on-demand to create a complete |
| 252 | *layout* that will no longer require an internet connection regardless of the |
| 253 | selected features. Note that this download may be bigger than required, but |
| 254 | where a large number of installations are going to be performed it is very |
| 255 | useful to have a locally cached copy. |
| 256 | |
| 257 | Execute the following command from Command Prompt to download all possible |
Steve Dower | 7a177c0 | 2019-06-26 08:55:57 -0700 | [diff] [blame] | 258 | required files. Remember to substitute ``python-3.9.0.exe`` for the actual |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 259 | name of your installer, and to create layouts in their own directories to |
| 260 | avoid collisions between files with the same name. |
| 261 | |
| 262 | :: |
| 263 | |
Steve Dower | 7a177c0 | 2019-06-26 08:55:57 -0700 | [diff] [blame] | 264 | python-3.9.0.exe /layout [optional target directory] |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 265 | |
| 266 | You may also specify the ``/quiet`` option to hide the progress display. |
| 267 | |
Steve Dower | e248f68 | 2016-01-16 11:58:30 -0800 | [diff] [blame] | 268 | Modifying an install |
| 269 | -------------------- |
| 270 | |
| 271 | Once Python has been installed, you can add or remove features through the |
| 272 | Programs and Features tool that is part of Windows. Select the Python entry and |
| 273 | choose "Uninstall/Change" to open the installer in maintenance mode. |
| 274 | |
| 275 | "Modify" allows you to add or remove features by modifying the checkboxes - |
| 276 | unchanged checkboxes will not install or remove anything. Some options cannot be |
| 277 | changed in this mode, such as the install directory; to modify these, you will |
| 278 | need to remove and then reinstall Python completely. |
| 279 | |
| 280 | "Repair" will verify all the files that should be installed using the current |
| 281 | settings and replace any that have been removed or modified. |
| 282 | |
| 283 | "Uninstall" will remove Python entirely, with the exception of the |
| 284 | :ref:`launcher`, which has its own entry in Programs and Features. |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 285 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 286 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 287 | .. _windows-store: |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 288 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 289 | The Microsoft Store package |
| 290 | =========================== |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 291 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 292 | .. versionadded:: 3.7.2 |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 293 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 294 | The Microsoft Store package is an easily installable Python interpreter that |
| 295 | is intended mainly for interactive use, for example, by students. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 296 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 297 | To install the package, ensure you have the latest Windows 10 updates and |
| 298 | search the Microsoft Store app for "Python |version|". Ensure that the app |
| 299 | you select is published by the Python Software Foundation, and install it. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 300 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 301 | .. warning:: |
| 302 | Python will always be available for free on the Microsoft Store. If you |
| 303 | are asked to pay for it, you have not selected the correct package. |
| 304 | |
| 305 | After installation, Python may be launched by finding it in Start. |
| 306 | Alternatively, it will be available from any Command Prompt or PowerShell |
| 307 | session by typing ``python``. Further, pip and IDLE may be used by typing |
| 308 | ``pip`` or ``idle``. IDLE can also be found in Start. |
| 309 | |
| 310 | All three commands are also available with version number suffixes, for |
| 311 | example, as ``python3.exe`` and ``python3.x.exe`` as well as |
| 312 | ``python.exe`` (where ``3.x`` is the specific version you want to launch, |
Steve Dower | cf9360e | 2019-08-19 10:07:25 -0700 | [diff] [blame] | 313 | such as |version|). Open "Manage App Execution Aliases" through Start to |
| 314 | select which version of Python is associated with each command. It is |
| 315 | recommended to make sure that ``pip`` and ``idle`` are consistent with |
| 316 | whichever version of ``python`` is selected. |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 317 | |
| 318 | Virtual environments can be created with ``python -m venv`` and activated |
| 319 | and used as normal. |
| 320 | |
| 321 | If you have installed another version of Python and added it to your |
| 322 | ``PATH`` variable, it will be available as ``python.exe`` rather than the |
| 323 | one from the Microsoft Store. To access the new installation, use |
| 324 | ``python3.exe`` or ``python3.x.exe``. |
| 325 | |
Steve Dower | 4d20228 | 2019-10-14 15:45:43 -0700 | [diff] [blame] | 326 | The ``py.exe`` launcher will detect this Python installation, but will prefer |
| 327 | installations from the traditional installer. |
| 328 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 329 | To remove Python, open Settings and use Apps and Features, or else find |
| 330 | Python in Start and right-click to select Uninstall. Uninstalling will |
| 331 | remove all packages you installed directly into this Python installation, but |
| 332 | will not remove any virtual environments |
| 333 | |
| 334 | Known Issues |
| 335 | ------------ |
| 336 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 337 | Because of restrictions on Microsoft Store apps, Python scripts may not have |
| 338 | full write access to shared locations such as ``TEMP`` and the registry. |
| 339 | Instead, it will write to a private copy. If your scripts must modify the |
| 340 | shared locations, you will need to install the full installer. |
| 341 | |
| 342 | |
| 343 | .. _windows-nuget: |
| 344 | |
| 345 | The nuget.org packages |
| 346 | ====================== |
| 347 | |
| 348 | .. versionadded:: 3.5.2 |
| 349 | |
| 350 | The nuget.org package is a reduced size Python environment intended for use on |
| 351 | continuous integration and build systems that do not have a system-wide |
| 352 | install of Python. While nuget is "the package manager for .NET", it also works |
| 353 | perfectly fine for packages containing build-time tools. |
| 354 | |
| 355 | Visit `nuget.org <https://www.nuget.org/>`_ for the most up-to-date information |
| 356 | on using nuget. What follows is a summary that is sufficient for Python |
| 357 | developers. |
| 358 | |
| 359 | The ``nuget.exe`` command line tool may be downloaded directly from |
| 360 | ``https://aka.ms/nugetclidl``, for example, using curl or PowerShell. With the |
| 361 | tool, the latest version of Python for 64-bit or 32-bit machines is installed |
| 362 | using:: |
| 363 | |
| 364 | nuget.exe install python -ExcludeVersion -OutputDirectory . |
| 365 | nuget.exe install pythonx86 -ExcludeVersion -OutputDirectory . |
| 366 | |
| 367 | To select a particular version, add a ``-Version 3.x.y``. The output directory |
| 368 | may be changed from ``.``, and the package will be installed into a |
| 369 | subdirectory. By default, the subdirectory is named the same as the package, |
| 370 | and without the ``-ExcludeVersion`` option this name will include the specific |
| 371 | version installed. Inside the subdirectory is a ``tools`` directory that |
| 372 | contains the Python installation:: |
| 373 | |
| 374 | # Without -ExcludeVersion |
| 375 | > .\python.3.5.2\tools\python.exe -V |
| 376 | Python 3.5.2 |
| 377 | |
| 378 | # With -ExcludeVersion |
| 379 | > .\python\tools\python.exe -V |
| 380 | Python 3.5.2 |
| 381 | |
| 382 | In general, nuget packages are not upgradeable, and newer versions should be |
| 383 | installed side-by-side and referenced using the full path. Alternatively, |
| 384 | delete the package directory manually and install it again. Many CI systems |
| 385 | will do this automatically if they do not preserve files between builds. |
| 386 | |
| 387 | Alongside the ``tools`` directory is a ``build\native`` directory. This |
| 388 | contains a MSBuild properties file ``python.props`` that can be used in a |
| 389 | C++ project to reference the Python install. Including the settings will |
| 390 | automatically use the headers and import libraries in your build. |
| 391 | |
| 392 | The package information pages on nuget.org are |
| 393 | `www.nuget.org/packages/python <https://www.nuget.org/packages/python>`_ |
| 394 | for the 64-bit version and `www.nuget.org/packages/pythonx86 |
| 395 | <https://www.nuget.org/packages/pythonx86>`_ for the 32-bit version. |
| 396 | |
| 397 | |
| 398 | .. _windows-embeddable: |
| 399 | |
| 400 | The embeddable package |
| 401 | ====================== |
| 402 | |
| 403 | .. versionadded:: 3.5 |
| 404 | |
| 405 | The embedded distribution is a ZIP file containing a minimal Python environment. |
| 406 | It is intended for acting as part of another application, rather than being |
| 407 | directly accessed by end-users. |
| 408 | |
| 409 | When extracted, the embedded distribution is (almost) fully isolated from the |
| 410 | user's system, including environment variables, system registry settings, and |
| 411 | installed packages. The standard library is included as pre-compiled and |
| 412 | optimized ``.pyc`` files in a ZIP, and ``python3.dll``, ``python37.dll``, |
| 413 | ``python.exe`` and ``pythonw.exe`` are all provided. Tcl/tk (including all |
| 414 | dependants, such as Idle), pip and the Python documentation are not included. |
| 415 | |
| 416 | .. note:: |
| 417 | |
| 418 | The embedded distribution does not include the `Microsoft C Runtime |
| 419 | <https://www.microsoft.com/en-us/download/details.aspx?id=48145>`_ and it is |
| 420 | the responsibility of the application installer to provide this. The |
| 421 | runtime may have already been installed on a user's system previously or |
| 422 | automatically via Windows Update, and can be detected by finding |
| 423 | ``ucrtbase.dll`` in the system directory. |
| 424 | |
| 425 | Third-party packages should be installed by the application installer alongside |
| 426 | the embedded distribution. Using pip to manage dependencies as for a regular |
| 427 | Python installation is not supported with this distribution, though with some |
| 428 | care it may be possible to include and use pip for automatic updates. In |
| 429 | general, third-party packages should be treated as part of the application |
| 430 | ("vendoring") so that the developer can ensure compatibility with newer |
| 431 | versions before providing updates to users. |
| 432 | |
| 433 | The two recommended use cases for this distribution are described below. |
| 434 | |
| 435 | Python Application |
| 436 | ------------------ |
| 437 | |
| 438 | An application written in Python does not necessarily require users to be aware |
| 439 | of that fact. The embedded distribution may be used in this case to include a |
| 440 | private version of Python in an install package. Depending on how transparent it |
| 441 | should be (or conversely, how professional it should appear), there are two |
| 442 | options. |
| 443 | |
| 444 | Using a specialized executable as a launcher requires some coding, but provides |
| 445 | the most transparent experience for users. With a customized launcher, there are |
| 446 | no obvious indications that the program is running on Python: icons can be |
| 447 | customized, company and version information can be specified, and file |
| 448 | associations behave properly. In most cases, a custom launcher should simply be |
| 449 | able to call ``Py_Main`` with a hard-coded command line. |
| 450 | |
| 451 | The simpler approach is to provide a batch file or generated shortcut that |
| 452 | directly calls the ``python.exe`` or ``pythonw.exe`` with the required |
| 453 | command-line arguments. In this case, the application will appear to be Python |
| 454 | and not its actual name, and users may have trouble distinguishing it from other |
| 455 | running Python processes or file associations. |
| 456 | |
| 457 | With the latter approach, packages should be installed as directories alongside |
| 458 | the Python executable to ensure they are available on the path. With the |
| 459 | specialized launcher, packages can be located in other locations as there is an |
| 460 | opportunity to specify the search path before launching the application. |
| 461 | |
| 462 | Embedding Python |
| 463 | ---------------- |
| 464 | |
| 465 | Applications written in native code often require some form of scripting |
| 466 | language, and the embedded Python distribution can be used for this purpose. In |
| 467 | general, the majority of the application is in native code, and some part will |
| 468 | either invoke ``python.exe`` or directly use ``python3.dll``. For either case, |
| 469 | extracting the embedded distribution to a subdirectory of the application |
| 470 | installation is sufficient to provide a loadable Python interpreter. |
| 471 | |
| 472 | As with the application use, packages can be installed to any location as there |
| 473 | is an opportunity to specify search paths before initializing the interpreter. |
| 474 | Otherwise, there is no fundamental differences between using the embedded |
| 475 | distribution and a regular installation. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 476 | |
| 477 | |
| 478 | Alternative bundles |
| 479 | =================== |
| 480 | |
| 481 | Besides the standard CPython distribution, there are modified packages including |
| 482 | additional functionality. The following is a list of popular versions and their |
| 483 | key features: |
| 484 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 485 | `ActivePython <https://www.activestate.com/activepython/>`_ |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 486 | Installer with multi-platform compatibility, documentation, PyWin32 |
| 487 | |
Sanyam Khurana | 338cd83 | 2018-01-20 05:55:37 +0530 | [diff] [blame] | 488 | `Anaconda <https://www.anaconda.com/download/>`_ |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 489 | Popular scientific modules (such as numpy, scipy and pandas) and the |
| 490 | ``conda`` package manager. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 491 | |
Sanyam Khurana | 338cd83 | 2018-01-20 05:55:37 +0530 | [diff] [blame] | 492 | `Canopy <https://www.enthought.com/product/canopy/>`_ |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 493 | A "comprehensive Python analysis environment" with editors and other |
| 494 | development tools. |
| 495 | |
| 496 | `WinPython <https://winpython.github.io/>`_ |
| 497 | Windows-specific distribution with prebuilt scientific packages and |
| 498 | tools for building packages. |
| 499 | |
| 500 | Note that these packages may not include the latest versions of Python or |
| 501 | other libraries, and are not maintained or supported by the core Python team. |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 502 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 503 | |
| 504 | |
| 505 | Configuring Python |
| 506 | ================== |
| 507 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 508 | To run Python conveniently from a command prompt, you might consider changing |
| 509 | some default environment variables in Windows. While the installer provides an |
| 510 | option to configure the PATH and PATHEXT variables for you, this is only |
| 511 | reliable for a single, system-wide installation. If you regularly use multiple |
| 512 | versions of Python, consider using the :ref:`launcher`. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 513 | |
| 514 | |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 515 | .. _setting-envvars: |
| 516 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 517 | Excursus: Setting environment variables |
| 518 | --------------------------------------- |
| 519 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 520 | Windows allows environment variables to be configured permanently at both the |
| 521 | User level and the System level, or temporarily in a command prompt. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 522 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 523 | To temporarily set environment variables, open Command Prompt and use the |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 524 | :command:`set` command: |
| 525 | |
| 526 | .. code-block:: doscon |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 527 | |
Steve Dower | 7a177c0 | 2019-06-26 08:55:57 -0700 | [diff] [blame] | 528 | C:\>set PATH=C:\Program Files\Python 3.9;%PATH% |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 529 | C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib |
| 530 | C:\>python |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 531 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 532 | These changes will apply to any further commands executed in that console, and |
| 533 | will be inherited by any applications started from the console. |
| 534 | |
| 535 | Including the variable name within percent signs will expand to the existing |
| 536 | value, allowing you to add your new value at either the start or the end. |
| 537 | Modifying :envvar:`PATH` by adding the directory containing |
| 538 | :program:`python.exe` to the start is a common way to ensure the correct version |
| 539 | of Python is launched. |
| 540 | |
| 541 | To permanently modify the default environment variables, click Start and search |
| 542 | for 'edit environment variables', or open System properties, :guilabel:`Advanced |
| 543 | system settings` and click the :guilabel:`Environment Variables` button. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 544 | In this dialog, you can add or modify User and System variables. To change |
| 545 | System variables, you need non-restricted access to your machine |
| 546 | (i.e. Administrator rights). |
| 547 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 548 | .. note:: |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 549 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 550 | Windows will concatenate User variables *after* System variables, which may |
| 551 | cause unexpected results when modifying :envvar:`PATH`. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 552 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 553 | The :envvar:`PYTHONPATH` variable is used by all versions of Python 2 and |
| 554 | Python 3, so you should not permanently configure this variable unless it |
Steve Dower | 9a578d9 | 2015-05-02 22:28:58 -0700 | [diff] [blame] | 555 | only includes code that is compatible with all of your installed Python |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 556 | versions. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 557 | |
| 558 | .. seealso:: |
| 559 | |
HiyashiChuka | 46ebe61 | 2018-08-03 11:44:06 +0900 | [diff] [blame] | 560 | https://www.microsoft.com/en-us/wdsi/help/folder-variables |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 561 | Environment variables in Windows NT |
| 562 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 563 | https://technet.microsoft.com/en-us/library/cc754250.aspx |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 564 | The SET command, for temporarily modifying environment variables |
| 565 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 566 | https://technet.microsoft.com/en-us/library/cc755104.aspx |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 567 | The SETX command, for permanently modifying environment variables |
| 568 | |
Sanyam Khurana | 338cd83 | 2018-01-20 05:55:37 +0530 | [diff] [blame] | 569 | https://support.microsoft.com/en-us/help/310519/how-to-manage-environment-variables-in-windows-xp |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 570 | How To Manage Environment Variables in Windows XP |
| 571 | |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 572 | https://www.chem.gla.ac.uk/~louis/software/faq/q1.html |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 573 | Setting Environment variables, Louis J. Farrugia |
| 574 | |
Nick Coghlan | 349c802 | 2012-09-30 13:00:43 +0530 | [diff] [blame] | 575 | .. _windows-path-mod: |
| 576 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 577 | Finding the Python executable |
| 578 | ----------------------------- |
| 579 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 580 | .. versionchanged:: 3.5 |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 581 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 582 | Besides using the automatically created start menu entry for the Python |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 583 | interpreter, you might want to start Python in the command prompt. The |
Steve Dower | f333fd8 | 2016-09-09 09:40:06 -0700 | [diff] [blame] | 584 | installer has an option to set that up for you. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 585 | |
Steve Dower | 4db86bc | 2016-09-09 09:17:35 -0700 | [diff] [blame] | 586 | On the first page of the installer, an option labelled "Add Python to PATH" |
| 587 | may be selected to have the installer add the install location into the |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 588 | :envvar:`PATH`. The location of the :file:`Scripts\\` folder is also added. |
| 589 | This allows you to type :command:`python` to run the interpreter, and |
Zachary Ware | 72e483a | 2016-08-04 12:13:47 -0500 | [diff] [blame] | 590 | :command:`pip` for the package installer. Thus, you can also execute your |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 591 | scripts with command line options, see :ref:`using-on-cmdline` documentation. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 592 | |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 593 | If you don't enable this option at install time, you can always re-run the |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 594 | installer, select Modify, and enable it. Alternatively, you can manually |
| 595 | modify the :envvar:`PATH` using the directions in :ref:`setting-envvars`. You |
| 596 | need to set your :envvar:`PATH` environment variable to include the directory |
| 597 | of your Python installation, delimited by a semicolon from other entries. An |
| 598 | example variable could look like this (assuming the first two entries already |
| 599 | existed):: |
Brian Curtin | a86f185 | 2012-08-19 11:22:20 -0500 | [diff] [blame] | 600 | |
Steve Dower | 7a177c0 | 2019-06-26 08:55:57 -0700 | [diff] [blame] | 601 | C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.9 |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 602 | |
Inada Naoki | 148610d | 2020-01-28 19:12:31 +0900 | [diff] [blame] | 603 | .. _win-utf8-mode: |
| 604 | |
| 605 | UTF-8 mode |
| 606 | ========== |
| 607 | |
| 608 | .. versionadded:: 3.7 |
| 609 | |
| 610 | Windows still uses legacy encodings for the system encoding (the ANSI Code |
| 611 | Page). Python uses it for the default encoding of text files (e.g. |
| 612 | :func:`locale.getpreferredencoding`). |
| 613 | |
| 614 | This may cause issues because UTF-8 is widely used on the internet |
| 615 | and most Unix systems, including WSL (Windows Subsystem for Linux). |
| 616 | |
| 617 | You can use UTF-8 mode to change the default text encoding to UTF-8. |
| 618 | You can enable UTF-8 mode via the ``-X utf8`` command line option, or |
| 619 | the ``PYTHONUTF8=1`` environment variable. See :envvar:`PYTHONUTF8` for |
| 620 | enabling UTF-8 mode, and :ref:`setting-envvars` for how to modify |
| 621 | environment variables. |
| 622 | |
| 623 | When UTF-8 mode is enabled: |
| 624 | |
| 625 | * :func:`locale.getpreferredencoding` returns ``'UTF-8'`` instead of |
| 626 | the system encoding. This function is used for the default text |
| 627 | encoding in many places, including :func:`open`, :class:`Popen`, |
| 628 | :meth:`Path.read_text`, etc. |
| 629 | * :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr` |
| 630 | all use UTF-8 as their text encoding. |
| 631 | * You can still use the system encoding via the "mbcs" codec. |
| 632 | |
| 633 | Note that adding ``PYTHONUTF8=1`` to the default environment variables |
| 634 | will affect all Python 3.7+ applications on your system. |
| 635 | If you have any Python 3.7+ applications which rely on the legacy |
| 636 | system encoding, it is recommended to set the environment variable |
| 637 | temporarily or use the ``-X utf8`` command line option. |
| 638 | |
| 639 | .. note:: |
| 640 | Even when UTF-8 mode is disabled, Python uses UTF-8 by default |
| 641 | on Windows for: |
| 642 | |
| 643 | * Console I/O including standard I/O (see :pep:`528` for details). |
| 644 | * The filesystem encoding (see :pep:`529` for details). |
| 645 | |
| 646 | |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 647 | .. _launcher: |
| 648 | |
| 649 | Python Launcher for Windows |
| 650 | =========================== |
| 651 | |
| 652 | .. versionadded:: 3.3 |
| 653 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 654 | The Python launcher for Windows is a utility which aids in locating and |
| 655 | executing of different Python versions. It allows scripts (or the |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 656 | command-line) to indicate a preference for a specific Python version, and |
| 657 | will locate and execute that version. |
| 658 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 659 | Unlike the :envvar:`PATH` variable, the launcher will correctly select the most |
| 660 | appropriate version of Python. It will prefer per-user installations over |
| 661 | system-wide ones, and orders by language version rather than using the most |
| 662 | recently installed version. |
| 663 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 664 | The launcher was originally specified in :pep:`397`. |
| 665 | |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 666 | Getting started |
| 667 | --------------- |
| 668 | |
| 669 | From the command-line |
| 670 | ^^^^^^^^^^^^^^^^^^^^^ |
| 671 | |
Paul Moore | 835416c | 2016-05-22 12:28:41 +0100 | [diff] [blame] | 672 | .. versionchanged:: 3.6 |
| 673 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 674 | System-wide installations of Python 3.3 and later will put the launcher on your |
| 675 | :envvar:`PATH`. The launcher is compatible with all available versions of |
| 676 | Python, so it does not matter which version is installed. To check that the |
| 677 | launcher is available, execute the following command in Command Prompt: |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 678 | |
| 679 | :: |
| 680 | |
| 681 | py |
| 682 | |
Paul Moore | 835416c | 2016-05-22 12:28:41 +0100 | [diff] [blame] | 683 | You should find that the latest version of Python you have installed is |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 684 | started - it can be exited as normal, and any additional command-line |
| 685 | arguments specified will be sent directly to Python. |
| 686 | |
Steve Dower | f333fd8 | 2016-09-09 09:40:06 -0700 | [diff] [blame] | 687 | If you have multiple versions of Python installed (e.g., 2.7 and |version|) you |
| 688 | will have noticed that Python |version| was started - to launch Python 2.7, try |
| 689 | the command: |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 690 | |
| 691 | :: |
| 692 | |
Paul Moore | 835416c | 2016-05-22 12:28:41 +0100 | [diff] [blame] | 693 | py -2.7 |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 694 | |
Paul Moore | 835416c | 2016-05-22 12:28:41 +0100 | [diff] [blame] | 695 | If you want the latest version of Python 2.x you have installed, try the |
| 696 | command: |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 697 | |
| 698 | :: |
| 699 | |
Paul Moore | 835416c | 2016-05-22 12:28:41 +0100 | [diff] [blame] | 700 | py -2 |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 701 | |
Paul Moore | 835416c | 2016-05-22 12:28:41 +0100 | [diff] [blame] | 702 | You should find the latest version of Python 2.x starts. |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 703 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 704 | If you see the following error, you do not have the launcher installed: |
| 705 | |
| 706 | :: |
| 707 | |
| 708 | 'py' is not recognized as an internal or external command, |
| 709 | operable program or batch file. |
| 710 | |
| 711 | Per-user installations of Python do not add the launcher to :envvar:`PATH` |
| 712 | unless the option was selected on installation. |
| 713 | |
Steve Dower | 76998fe | 2015-02-26 14:25:33 -0800 | [diff] [blame] | 714 | Virtual environments |
| 715 | ^^^^^^^^^^^^^^^^^^^^ |
| 716 | |
Berker Peksag | f2d4e57 | 2015-03-02 05:36:19 +0200 | [diff] [blame] | 717 | .. versionadded:: 3.5 |
| 718 | |
Steve Dower | 76998fe | 2015-02-26 14:25:33 -0800 | [diff] [blame] | 719 | If the launcher is run with no explicit Python version specification, and a |
| 720 | virtual environment (created with the standard library :mod:`venv` module or |
| 721 | the external ``virtualenv`` tool) active, the launcher will run the virtual |
| 722 | environment's interpreter rather than the global one. To run the global |
| 723 | interpreter, either deactivate the virtual environment, or explicitly specify |
| 724 | the global Python version. |
| 725 | |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 726 | From a script |
| 727 | ^^^^^^^^^^^^^ |
| 728 | |
| 729 | Let's create a test Python script - create a file called ``hello.py`` with the |
| 730 | following contents |
| 731 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 732 | .. code-block:: python |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 733 | |
| 734 | #! python |
| 735 | import sys |
| 736 | sys.stdout.write("hello from Python %s\n" % (sys.version,)) |
| 737 | |
| 738 | From the directory in which hello.py lives, execute the command: |
| 739 | |
| 740 | :: |
| 741 | |
| 742 | py hello.py |
| 743 | |
| 744 | You should notice the version number of your latest Python 2.x installation |
| 745 | is printed. Now try changing the first line to be: |
| 746 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 747 | .. code-block:: python |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 748 | |
| 749 | #! python3 |
| 750 | |
| 751 | Re-executing the command should now print the latest Python 3.x information. |
| 752 | As with the above command-line examples, you can specify a more explicit |
| 753 | version qualifier. Assuming you have Python 2.6 installed, try changing the |
| 754 | first line to ``#! python2.6`` and you should find the 2.6 version |
| 755 | information printed. |
| 756 | |
Paul Moore | 835416c | 2016-05-22 12:28:41 +0100 | [diff] [blame] | 757 | Note that unlike interactive use, a bare "python" will use the latest |
| 758 | version of Python 2.x that you have installed. This is for backward |
| 759 | compatibility and for compatibility with Unix, where the command ``python`` |
| 760 | typically refers to Python 2. |
| 761 | |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 762 | From file associations |
| 763 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 764 | |
| 765 | The launcher should have been associated with Python files (i.e. ``.py``, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 766 | ``.pyw``, ``.pyc`` files) when it was installed. This means that |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 767 | when you double-click on one of these files from Windows explorer the launcher |
| 768 | will be used, and therefore you can use the same facilities described above to |
| 769 | have the script specify the version which should be used. |
| 770 | |
| 771 | The key benefit of this is that a single launcher can support multiple Python |
| 772 | versions at the same time depending on the contents of the first line. |
| 773 | |
| 774 | Shebang Lines |
| 775 | ------------- |
| 776 | |
| 777 | If the first line of a script file starts with ``#!``, it is known as a |
| 778 | "shebang" line. Linux and other Unix like operating systems have native |
Wieland Hoffmann | 80a3da4 | 2017-04-28 18:12:57 +0200 | [diff] [blame] | 779 | support for such lines and they are commonly used on such systems to indicate |
| 780 | how a script should be executed. This launcher allows the same facilities to |
| 781 | be used with Python scripts on Windows and the examples above demonstrate their |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 782 | use. |
| 783 | |
| 784 | To allow shebang lines in Python scripts to be portable between Unix and |
| 785 | Windows, this launcher supports a number of 'virtual' commands to specify |
| 786 | which interpreter to use. The supported virtual commands are: |
| 787 | |
| 788 | * ``/usr/bin/env python`` |
| 789 | * ``/usr/bin/python`` |
| 790 | * ``/usr/local/bin/python`` |
| 791 | * ``python`` |
| 792 | |
| 793 | For example, if the first line of your script starts with |
| 794 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 795 | .. code-block:: sh |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 796 | |
| 797 | #! /usr/bin/python |
| 798 | |
| 799 | The default Python will be located and used. As many Python scripts written |
| 800 | to work on Unix will already have this line, you should find these scripts can |
| 801 | be used by the launcher without modification. If you are writing a new script |
| 802 | on Windows which you hope will be useful on Unix, you should use one of the |
| 803 | shebang lines starting with ``/usr``. |
| 804 | |
Steve Dower | 76998fe | 2015-02-26 14:25:33 -0800 | [diff] [blame] | 805 | Any of the above virtual commands can be suffixed with an explicit version |
mrh1997 | 1e2ad6c | 2019-04-13 00:26:47 +0200 | [diff] [blame] | 806 | (either just the major version, or the major and minor version). |
| 807 | Furthermore the 32-bit version can be requested by adding "-32" after the |
| 808 | minor version. I.e. ``/usr/bin/python2.7-32`` will request usage of the |
| 809 | 32-bit python 2.7. |
| 810 | |
| 811 | .. versionadded:: 3.7 |
| 812 | |
| 813 | Beginning with python launcher 3.7 it is possible to request 64-bit version |
| 814 | by the "-64" suffix. Furthermore it is possible to specify a major and |
| 815 | architecture without minor (i.e. ``/usr/bin/python3-64``). |
Steve Dower | 76998fe | 2015-02-26 14:25:33 -0800 | [diff] [blame] | 816 | |
| 817 | The ``/usr/bin/env`` form of shebang line has one further special property. |
| 818 | Before looking for installed Python interpreters, this form will search the |
| 819 | executable :envvar:`PATH` for a Python executable. This corresponds to the |
| 820 | behaviour of the Unix ``env`` program, which performs a :envvar:`PATH` search. |
| 821 | |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 822 | Arguments in shebang lines |
| 823 | -------------------------- |
| 824 | |
| 825 | The shebang lines can also specify additional options to be passed to the |
| 826 | Python interpreter. For example, if you have a shebang line: |
| 827 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 828 | .. code-block:: sh |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 829 | |
| 830 | #! /usr/bin/python -v |
| 831 | |
| 832 | Then Python will be started with the ``-v`` option |
| 833 | |
| 834 | Customization |
| 835 | ------------- |
| 836 | |
| 837 | Customization via INI files |
| 838 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 839 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 840 | Two .ini files will be searched by the launcher - ``py.ini`` in the current |
| 841 | user's "application data" directory (i.e. the directory returned by calling the |
Segev Finer | 8e7e8bd | 2018-07-30 20:11:30 +0300 | [diff] [blame] | 842 | Windows function ``SHGetFolderPath`` with ``CSIDL_LOCAL_APPDATA``) and ``py.ini`` in the |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 843 | same directory as the launcher. The same .ini files are used for both the |
| 844 | 'console' version of the launcher (i.e. py.exe) and for the 'windows' version |
Andre Delfino | 55f41e4 | 2018-12-05 16:45:30 -0300 | [diff] [blame] | 845 | (i.e. pyw.exe). |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 846 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 847 | Customization specified in the "application directory" will have precedence over |
| 848 | the one next to the executable, so a user, who may not have write access to the |
Andre Delfino | 55f41e4 | 2018-12-05 16:45:30 -0300 | [diff] [blame] | 849 | .ini file next to the launcher, can override commands in that global .ini file. |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 850 | |
| 851 | Customizing default Python versions |
| 852 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 853 | |
| 854 | In some cases, a version qualifier can be included in a command to dictate |
| 855 | which version of Python will be used by the command. A version qualifier |
| 856 | starts with a major version number and can optionally be followed by a period |
Xtreak | 0d70227 | 2019-06-03 04:42:33 +0530 | [diff] [blame] | 857 | ('.') and a minor version specifier. Furthermore it is possible to specify |
mrh1997 | 1e2ad6c | 2019-04-13 00:26:47 +0200 | [diff] [blame] | 858 | if a 32 or 64 bit implementation shall be requested by adding "-32" or "-64". |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 859 | |
| 860 | For example, a shebang line of ``#!python`` has no version qualifier, while |
| 861 | ``#!python3`` has a version qualifier which specifies only a major version. |
| 862 | |
mrh1997 | 1e2ad6c | 2019-04-13 00:26:47 +0200 | [diff] [blame] | 863 | If no version qualifiers are found in a command, the environment |
| 864 | variable :envvar:`PY_PYTHON` can be set to specify the default version |
| 865 | qualifier. If it is not set, the default is "3". The variable can |
| 866 | specify any value that may be passed on the command line, such as "3", |
| 867 | "3.7", "3.7-32" or "3.7-64". (Note that the "-64" option is only |
| 868 | available with the launcher included with Python 3.7 or newer.) |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 869 | |
| 870 | If no minor version qualifiers are found, the environment variable |
| 871 | ``PY_PYTHON{major}`` (where ``{major}`` is the current major version qualifier |
| 872 | as determined above) can be set to specify the full version. If no such option |
| 873 | is found, the launcher will enumerate the installed Python versions and use |
| 874 | the latest minor release found for the major version, which is likely, |
| 875 | although not guaranteed, to be the most recently installed version in that |
| 876 | family. |
| 877 | |
| 878 | On 64-bit Windows with both 32-bit and 64-bit implementations of the same |
| 879 | (major.minor) Python version installed, the 64-bit version will always be |
| 880 | preferred. This will be true for both 32-bit and 64-bit implementations of the |
| 881 | launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation |
| 882 | of the specified version if available. This is so the behavior of the launcher |
| 883 | can be predicted knowing only what versions are installed on the PC and |
| 884 | without regard to the order in which they were installed (i.e., without knowing |
| 885 | whether a 32 or 64-bit version of Python and corresponding launcher was |
mrh1997 | 1e2ad6c | 2019-04-13 00:26:47 +0200 | [diff] [blame] | 886 | installed last). As noted above, an optional "-32" or "-64" suffix can be |
| 887 | used on a version specifier to change this behaviour. |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 888 | |
| 889 | Examples: |
| 890 | |
| 891 | * If no relevant options are set, the commands ``python`` and |
| 892 | ``python2`` will use the latest Python 2.x version installed and |
| 893 | the command ``python3`` will use the latest Python 3.x installed. |
| 894 | |
| 895 | * The commands ``python3.1`` and ``python2.7`` will not consult any |
| 896 | options at all as the versions are fully specified. |
| 897 | |
| 898 | * If ``PY_PYTHON=3``, the commands ``python`` and ``python3`` will both use |
| 899 | the latest installed Python 3 version. |
| 900 | |
| 901 | * If ``PY_PYTHON=3.1-32``, the command ``python`` will use the 32-bit |
| 902 | implementation of 3.1 whereas the command ``python3`` will use the latest |
| 903 | installed Python (PY_PYTHON was not considered at all as a major |
| 904 | version was specified.) |
| 905 | |
| 906 | * If ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1``, the commands |
| 907 | ``python`` and ``python3`` will both use specifically 3.1 |
| 908 | |
| 909 | In addition to environment variables, the same settings can be configured |
| 910 | in the .INI file used by the launcher. The section in the INI file is |
| 911 | called ``[defaults]`` and the key name will be the same as the |
Ezio Melotti | 5b69fbd | 2012-10-19 20:40:18 +0300 | [diff] [blame] | 912 | environment variables without the leading ``PY_`` prefix (and note that |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 913 | the key names in the INI file are case insensitive.) The contents of |
| 914 | an environment variable will override things specified in the INI file. |
| 915 | |
| 916 | For example: |
| 917 | |
| 918 | * Setting ``PY_PYTHON=3.1`` is equivalent to the INI file containing: |
| 919 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 920 | .. code-block:: ini |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 921 | |
| 922 | [defaults] |
| 923 | python=3.1 |
| 924 | |
| 925 | * Setting ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1`` is equivalent to the INI file |
| 926 | containing: |
| 927 | |
Serhiy Storchaka | 46936d5 | 2018-04-08 19:18:04 +0300 | [diff] [blame] | 928 | .. code-block:: ini |
Vinay Sajip | d89dae1 | 2012-06-24 11:23:07 +0100 | [diff] [blame] | 929 | |
| 930 | [defaults] |
| 931 | python=3 |
| 932 | python3=3.1 |
| 933 | |
| 934 | Diagnostics |
| 935 | ----------- |
| 936 | |
| 937 | If an environment variable ``PYLAUNCH_DEBUG`` is set (to any value), the |
| 938 | launcher will print diagnostic information to stderr (i.e. to the console). |
| 939 | While this information manages to be simultaneously verbose *and* terse, it |
| 940 | should allow you to see what versions of Python were located, why a |
| 941 | particular version was chosen and the exact command-line used to execute the |
| 942 | target Python. |
| 943 | |
| 944 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 945 | |
Steve Dower | 3cdd7f5 | 2016-09-09 15:22:13 -0700 | [diff] [blame] | 946 | .. _finding_modules: |
Steve Dower | eb3c16d | 2015-08-08 09:09:01 -0700 | [diff] [blame] | 947 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 948 | Finding modules |
| 949 | =============== |
| 950 | |
| 951 | Python usually stores its library (and thereby your site-packages folder) in the |
| 952 | installation directory. So, if you had installed Python to |
| 953 | :file:`C:\\Python\\`, the default library would reside in |
| 954 | :file:`C:\\Python\\Lib\\` and third-party modules should be stored in |
| 955 | :file:`C:\\Python\\Lib\\site-packages\\`. |
| 956 | |
Steve Dower | ed51b26 | 2016-09-17 12:54:06 -0700 | [diff] [blame] | 957 | To completely override :data:`sys.path`, create a ``._pth`` file with the same |
Steve Dower | 15ea3a6 | 2018-01-29 10:34:09 +1100 | [diff] [blame] | 958 | name as the DLL (``python37._pth``) or the executable (``python._pth``) and |
Steve Dower | ed51b26 | 2016-09-17 12:54:06 -0700 | [diff] [blame] | 959 | specify one line for each path to add to :data:`sys.path`. The file based on the |
| 960 | DLL name overrides the one based on the executable, which allows paths to be |
| 961 | restricted for any program loading the runtime if desired. |
Steve Dower | 4db86bc | 2016-09-09 09:17:35 -0700 | [diff] [blame] | 962 | |
Steve Dower | ed51b26 | 2016-09-17 12:54:06 -0700 | [diff] [blame] | 963 | When the file exists, all registry and environment variables are ignored, |
| 964 | isolated mode is enabled, and :mod:`site` is not imported unless one line in the |
| 965 | file specifies ``import site``. Blank paths and lines starting with ``#`` are |
| 966 | ignored. Each path may be absolute or relative to the location of the file. |
| 967 | Import statements other than to ``site`` are not permitted, and arbitrary code |
| 968 | cannot be specified. |
| 969 | |
| 970 | Note that ``.pth`` files (without leading underscore) will be processed normally |
Steve Dower | 15ea3a6 | 2018-01-29 10:34:09 +1100 | [diff] [blame] | 971 | by the :mod:`site` module when ``import site`` has been specified. |
Steve Dower | ed51b26 | 2016-09-17 12:54:06 -0700 | [diff] [blame] | 972 | |
| 973 | When no ``._pth`` file is found, this is how :data:`sys.path` is populated on |
| 974 | Windows: |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 975 | |
| 976 | * An empty entry is added at the start, which corresponds to the current |
| 977 | directory. |
| 978 | |
| 979 | * If the environment variable :envvar:`PYTHONPATH` exists, as described in |
| 980 | :ref:`using-on-envvars`, its entries are added next. Note that on Windows, |
| 981 | paths in this variable must be separated by semicolons, to distinguish them |
| 982 | from the colon used in drive identifiers (``C:\`` etc.). |
| 983 | |
| 984 | * Additional "application paths" can be added in the registry as subkeys of |
| 985 | :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the |
| 986 | ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have |
| 987 | semicolon-delimited path strings as their default value will cause each path |
| 988 | to be added to :data:`sys.path`. (Note that all known installers only use |
| 989 | HKLM, so HKCU is typically empty.) |
| 990 | |
| 991 | * If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as |
| 992 | "Python Home". Otherwise, the path of the main Python executable is used to |
Steve Dower | 814f9ae | 2016-09-09 15:07:46 -0700 | [diff] [blame] | 993 | locate a "landmark file" (either ``Lib\os.py`` or ``pythonXY.zip``) to deduce |
| 994 | the "Python Home". If a Python home is found, the relevant sub-directories |
| 995 | added to :data:`sys.path` (``Lib``, ``plat-win``, etc) are based on that |
| 996 | folder. Otherwise, the core Python path is constructed from the PythonPath |
| 997 | stored in the registry. |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 998 | |
| 999 | * If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in |
| 1000 | the environment, and no registry entries can be found, a default path with |
| 1001 | relative entries is used (e.g. ``.\Lib;.\plat-win``, etc). |
| 1002 | |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 1003 | If a ``pyvenv.cfg`` file is found alongside the main executable or in the |
| 1004 | directory one level above the executable, the following variations apply: |
| 1005 | |
| 1006 | * If ``home`` is an absolute path and :envvar:`PYTHONHOME` is not set, this |
| 1007 | path is used instead of the path to the main executable when deducing the |
| 1008 | home location. |
| 1009 | |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 1010 | The end result of all this is: |
| 1011 | |
| 1012 | * When running :file:`python.exe`, or any other .exe in the main Python |
| 1013 | directory (either an installed version, or directly from the PCbuild |
| 1014 | directory), the core path is deduced, and the core paths in the registry are |
| 1015 | ignored. Other "application paths" in the registry are always read. |
| 1016 | |
| 1017 | * When Python is hosted in another .exe (different directory, embedded via COM, |
| 1018 | etc), the "Python Home" will not be deduced, so the core path from the |
| 1019 | registry is used. Other "application paths" in the registry are always read. |
| 1020 | |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 1021 | * If Python can't find its home and there are no registry value (frozen .exe, |
| 1022 | some very strange installation setup) you get a path with some default, but |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 1023 | relative, paths. |
| 1024 | |
| 1025 | For those who want to bundle Python into their application or distribution, the |
| 1026 | following advice will prevent conflicts with other installations: |
| 1027 | |
Steve Dower | ed51b26 | 2016-09-17 12:54:06 -0700 | [diff] [blame] | 1028 | * Include a ``._pth`` file alongside your executable containing the |
| 1029 | directories to include. This will ignore paths listed in the registry and |
| 1030 | environment variables, and also ignore :mod:`site` unless ``import site`` is |
| 1031 | listed. |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 1032 | |
Steve Dower | 15ea3a6 | 2018-01-29 10:34:09 +1100 | [diff] [blame] | 1033 | * If you are loading :file:`python3.dll` or :file:`python37.dll` in your own |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 1034 | executable, explicitly call :c:func:`Py_SetPath` or (at least) |
| 1035 | :c:func:`Py_SetProgramName` before :c:func:`Py_Initialize`. |
| 1036 | |
| 1037 | * Clear and/or overwrite :envvar:`PYTHONPATH` and set :envvar:`PYTHONHOME` |
| 1038 | before launching :file:`python.exe` from your application. |
| 1039 | |
| 1040 | * If you cannot use the previous suggestions (for example, you are a |
| 1041 | distribution that allows people to run :file:`python.exe` directly), ensure |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 1042 | that the landmark file (:file:`Lib\\os.py`) exists in your install directory. |
Steve Dower | 814f9ae | 2016-09-09 15:07:46 -0700 | [diff] [blame] | 1043 | (Note that it will not be detected inside a ZIP file, but a correctly named |
| 1044 | ZIP file will be detected instead.) |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 1045 | |
| 1046 | These will ensure that the files in a system-wide installation will not take |
| 1047 | precedence over the copy of the standard library bundled with your application. |
Steve Dower | eb3c16d | 2015-08-08 09:09:01 -0700 | [diff] [blame] | 1048 | Otherwise, your users may experience problems using your application. Note that |
Segev Finer | 8e7e8bd | 2018-07-30 20:11:30 +0300 | [diff] [blame] | 1049 | the first suggestion is the best, as the others may still be susceptible to |
Steve Dower | eb3c16d | 2015-08-08 09:09:01 -0700 | [diff] [blame] | 1050 | non-standard paths in the registry and user site-packages. |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 1051 | |
Steve Dower | 814f9ae | 2016-09-09 15:07:46 -0700 | [diff] [blame] | 1052 | .. versionchanged:: |
| 1053 | 3.6 |
Steve Dower | 4db86bc | 2016-09-09 09:17:35 -0700 | [diff] [blame] | 1054 | |
Victor Stinner | c6b1e15 | 2016-10-20 00:45:50 +0200 | [diff] [blame] | 1055 | * Adds ``._pth`` file support and removes ``applocal`` option from |
| 1056 | ``pyvenv.cfg``. |
| 1057 | * Adds ``pythonXX.zip`` as a potential landmark when directly adjacent |
| 1058 | to the executable. |
Steve Dower | 4db86bc | 2016-09-09 09:17:35 -0700 | [diff] [blame] | 1059 | |
Steve Dower | 2036742 | 2016-12-07 13:02:27 -0800 | [diff] [blame] | 1060 | .. deprecated:: |
| 1061 | 3.6 |
| 1062 | |
| 1063 | Modules specified in the registry under ``Modules`` (not ``PythonPath``) |
| 1064 | may be imported by :class:`importlib.machinery.WindowsRegistryFinder`. |
| 1065 | This finder is enabled on Windows in 3.6.0 and earlier, but may need to |
| 1066 | be explicitly added to :attr:`sys.meta_path` in the future. |
| 1067 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1068 | Additional modules |
| 1069 | ================== |
| 1070 | |
| 1071 | Even though Python aims to be portable among all platforms, there are features |
| 1072 | that are unique to Windows. A couple of modules, both in the standard library |
| 1073 | and external, and snippets exist to use these features. |
| 1074 | |
| 1075 | The Windows-specific standard modules are documented in |
| 1076 | :ref:`mswin-specific-services`. |
| 1077 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1078 | PyWin32 |
| 1079 | ------- |
| 1080 | |
Stéphane Wirtel | 19177fb | 2018-05-15 20:58:35 +0200 | [diff] [blame] | 1081 | The `PyWin32 <https://pypi.org/project/pywin32>`_ module by Mark Hammond |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1082 | is a collection of modules for advanced Windows-specific support. This includes |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 1083 | utilities for: |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1084 | |
Julien Palard | 7114c65 | 2019-05-25 20:02:24 +0200 | [diff] [blame] | 1085 | * `Component Object Model |
| 1086 | <https://docs.microsoft.com/en-us/windows/desktop/com/component-object-model--com--portal>`_ |
| 1087 | (COM) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1088 | * Win32 API calls |
| 1089 | * Registry |
| 1090 | * Event log |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 1091 | * `Microsoft Foundation Classes <https://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx>`_ (MFC) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1092 | user interfaces |
| 1093 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 1094 | `PythonWin <https://web.archive.org/web/20060524042422/ |
Georg Brandl | e73778c | 2014-10-29 08:36:35 +0100 | [diff] [blame] | 1095 | https://www.python.org/windows/pythonwin/>`_ is a sample MFC application |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1096 | shipped with PyWin32. It is an embeddable IDE with a built-in debugger. |
| 1097 | |
| 1098 | .. seealso:: |
| 1099 | |
| 1100 | `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_ |
| 1101 | by Tim Golden |
| 1102 | |
| 1103 | `Python and COM <http://www.boddie.org.uk/python/COM.html>`_ |
| 1104 | by David and Paul Boddie |
| 1105 | |
| 1106 | |
Zachary Ware | c3cf97b | 2014-01-17 15:23:42 -0600 | [diff] [blame] | 1107 | cx_Freeze |
| 1108 | --------- |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1109 | |
Sanyam Khurana | 1b4587a | 2017-12-06 22:09:33 +0530 | [diff] [blame] | 1110 | `cx_Freeze <https://anthony-tuininga.github.io/cx_Freeze/>`_ is a :mod:`distutils` |
Zachary Ware | c3cf97b | 2014-01-17 15:23:42 -0600 | [diff] [blame] | 1111 | extension (see :ref:`extending-distutils`) which wraps Python scripts into |
| 1112 | executable Windows programs (:file:`{*}.exe` files). When you have done this, |
| 1113 | you can distribute your application without requiring your users to install |
| 1114 | Python. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1115 | |
| 1116 | |
| 1117 | WConio |
| 1118 | ------ |
| 1119 | |
| 1120 | Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to |
| 1121 | Unix-like systems, there is a library exclusive to Windows as well: Windows |
| 1122 | Console I/O for Python. |
| 1123 | |
| 1124 | `WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for |
| 1125 | Turbo-C's :file:`CONIO.H`, used to create text user interfaces. |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | Compiling Python on Windows |
| 1130 | =========================== |
| 1131 | |
| 1132 | If you want to compile CPython yourself, first thing you should do is get the |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 1133 | `source <https://www.python.org/downloads/source/>`_. You can download either the |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1134 | latest release's source or just grab a fresh `checkout |
Lisa Hewus Fresh | 384899d | 2017-08-30 09:37:43 -0700 | [diff] [blame] | 1135 | <https://devguide.python.org/setup/#getting-the-source-code>`_. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1136 | |
Zachary Ware | f8ceb04 | 2013-11-30 16:59:33 -0600 | [diff] [blame] | 1137 | The source tree contains a build solution and project files for Microsoft |
Steve Dower | bb24087 | 2015-02-05 22:08:48 -0800 | [diff] [blame] | 1138 | Visual Studio 2015, which is the compiler used to build the official Python |
| 1139 | releases. These files are in the :file:`PCbuild` directory. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1140 | |
Zachary Ware | f8ceb04 | 2013-11-30 16:59:33 -0600 | [diff] [blame] | 1141 | Check :file:`PCbuild/readme.txt` for general information on the build process. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1142 | |
| 1143 | |
| 1144 | For extension modules, consult :ref:`building-on-windows`. |
| 1145 | |
| 1146 | .. seealso:: |
| 1147 | |
| 1148 | `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_ |
| 1149 | or "Creating Python extensions in C/C++ with SWIG and compiling them with |
| 1150 | MinGW gcc under Windows" or "Installing Python extension with distutils |
| 1151 | and without Microsoft Visual C++" by Sébastien Sauvage, 2003 |
| 1152 | |
Julien Palard | 7114c65 | 2019-05-25 20:02:24 +0200 | [diff] [blame] | 1153 | `MingW -- Python extensions <http://www.mingw.org/wiki/FAQ#toc14>`_ |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1154 | |
| 1155 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 1156 | Other Platforms |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1157 | =============== |
| 1158 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 1159 | With ongoing development of Python, some platforms that used to be supported |
| 1160 | earlier are no longer supported (due to the lack of users or developers). |
| 1161 | Check :pep:`11` for details on all unsupported platforms. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1162 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 1163 | * `Windows CE <http://pythonce.sourceforge.net/>`_ is still supported. |
| 1164 | * The `Cygwin <https://cygwin.com/>`_ installer offers to install the Python |
| 1165 | interpreter as well (cf. `Cygwin package source |
| 1166 | <ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/ |
| 1167 | release/python>`_, `Maintainer releases |
| 1168 | <http://www.tishler.net/jason/software/python/>`_) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1169 | |
Steve Dower | 0cd6391 | 2018-12-10 18:52:57 -0800 | [diff] [blame] | 1170 | See `Python for Windows <https://www.python.org/downloads/windows/>`_ |
| 1171 | for detailed information about platforms with pre-compiled installers. |