1 .. highlightlang:: none 2 3 .. _using-on-windows: 4 5 ************************* 6 Using Python on Windows 7 ************************* 8 9 .. sectionauthor:: Robert Lehmann <lehmannro (a] gmail.com> 10 .. sectionauthor:: Steve Dower <steve.dower (a] microsoft.com> 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 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 17 has compiled Windows installers (MSI packages) with every `release 18 <https://www.python.org/download/releases/>`_ for many years. These installers 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. 23 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 26 Python |version| supports Windows Vista and newer. If you require Windows XP 27 support then please install Python 3.4. 28 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 55 ------------------ 56 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 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 65 After starting the installer, one of two options may be selected: 66 67 .. image:: win_installer.png 68 69 If you select "Install Now": 70 71 * You will *not* need to be an administrator (unless a system update for the 72 C Runtime Library is required or you install the :ref:`launcher` for all 73 users) 74 * Python will be installed into your user directory 75 * The :ref:`launcher` will be installed according to the option at the bottom 76 of the first page 77 * The standard library, test suite, launcher and pip will be installed 78 * If selected, the install directory will be added to your :envvar:`PATH` 79 * Shortcuts will only be visible for the current user 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 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 92 * The standard library can be pre-compiled to bytecode 93 * If selected, the install directory will be added to the system :envvar:`PATH` 94 * Shortcuts are available for all users 95 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 111 path functionality to accept and return paths longer than 260 characters when 112 using strings. (Use of bytes as paths is deprecated on Windows, and this feature 113 is not available when using bytes.) 114 115 After changing the above option, no further configuration is required. 116 117 .. versionchanged:: 3.6 118 119 Support for long paths was enabled in Python. 120 121 .. _install-quiet-option: 122 123 Installing Without UI 124 --------------------- 125 126 All of the options available in the installer UI can also be specified from the 127 command line, allowing scripted installers to replicate an installation on many 128 machines without user interaction. These options may also be set without 129 suppressing the UI in order to change some of the defaults. 130 131 To completely hide the installer UI and install Python silently, pass the 132 ``/quiet`` option. To skip past the user interaction but still display 133 progress and errors, pass the ``/passive`` option. The ``/uninstall`` 134 option may be passed to immediately begin removing Python - no prompt will be 135 displayed. 136 137 All other options are passed as ``name=value``, where the value is usually 138 ``0`` to disable a feature, ``1`` to enable a feature, or a path. The full list 139 of available options is shown below. 140 141 +---------------------------+--------------------------------------+--------------------------+ 142 | Name | Description | Default | 143 +===========================+======================================+==========================+ 144 | InstallAllUsers | Perform a system-wide installation. | 0 | 145 +---------------------------+--------------------------------------+--------------------------+ 146 | TargetDir | The installation directory | Selected based on | 147 | | | InstallAllUsers | 148 +---------------------------+--------------------------------------+--------------------------+ 149 | DefaultAllUsersTargetDir | The default installation directory | :file:`%ProgramFiles%\\\ | 150 | | for all-user installs | Python X.Y` or :file:`\ | 151 | | | %ProgramFiles(x86)%\\\ | 152 | | | Python X.Y` | 153 +---------------------------+--------------------------------------+--------------------------+ 154 | DefaultJustForMeTargetDir | The default install directory for | :file:`%LocalAppData%\\\ | 155 | | just-for-me installs | Programs\\PythonXY` or | 156 | | | :file:`%LocalAppData%\\\ | 157 | | | Programs\\PythonXY-32` | 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 | 166 | | ``.pyc``. | | 167 +---------------------------+--------------------------------------+--------------------------+ 168 | PrependPath | Add install and Scripts directories | 0 | 169 | | to :envvar:`PATH` and ``.PY`` to | | 170 | | :envvar:`PATHEXT` | | 171 +---------------------------+--------------------------------------+--------------------------+ 172 | Shortcuts | Create shortcuts for the interpreter,| 1 | 173 | | documentation and IDLE if installed. | | 174 +---------------------------+--------------------------------------+--------------------------+ 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 +---------------------------+--------------------------------------+--------------------------+ 187 | InstallLauncherAllUsers | Installs :ref:`launcher` for all | 1 | 188 | | users. | | 189 +---------------------------+--------------------------------------+--------------------------+ 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 +---------------------------+--------------------------------------+--------------------------+ 203 | LauncherOnly | Only installs the launcher. This | 0 | 204 | | will override most other options. | | 205 +---------------------------+--------------------------------------+--------------------------+ 206 | SimpleInstall | Disable most install UI | 0 | 207 +---------------------------+--------------------------------------+--------------------------+ 208 | SimpleInstallDescription | A custom message to display when the | (empty) | 209 | | simplified install UI is used. | | 210 +---------------------------+--------------------------------------+--------------------------+ 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 215 python-3.7.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 216 217 To allow users to easily install a personal copy of Python without the test 218 suite, you could provide a shortcut with the following command. This will 219 display a simplified initial page and disallow customization:: 220 221 python-3.7.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0 222 SimpleInstall=1 SimpleInstallDescription="Just for me, no test suite." 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 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 232 example file sets the same options as the previous example: 233 234 .. code-block:: xml 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 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 258 required files. Remember to substitute ``python-3.7.0.exe`` for the actual 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 264 python-3.7.0.exe /layout [optional target directory] 265 266 You may also specify the ``/quiet`` option to hide the progress display. 267 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. 285 286 287 .. _windows-store: 288 289 The Microsoft Store package 290 =========================== 291 292 .. versionadded:: 3.7.2 293 294 .. note:: 295 The Microsoft Store package is currently considered unstable while its 296 interactions with other tools and other copies of Python are evaluated. 297 While Python itself is stable, this installation method may change its 298 behavior and capabilities during Python 3.7 releases. 299 300 The Microsoft Store package is an easily installable Python interpreter that 301 is intended mainly for interactive use, for example, by students. 302 303 To install the package, ensure you have the latest Windows 10 updates and 304 search the Microsoft Store app for "Python |version|". Ensure that the app 305 you select is published by the Python Software Foundation, and install it. 306 307 .. warning:: 308 Python will always be available for free on the Microsoft Store. If you 309 are asked to pay for it, you have not selected the correct package. 310 311 After installation, Python may be launched by finding it in Start. 312 Alternatively, it will be available from any Command Prompt or PowerShell 313 session by typing ``python``. Further, pip and IDLE may be used by typing 314 ``pip`` or ``idle``. IDLE can also be found in Start. 315 316 All three commands are also available with version number suffixes, for 317 example, as ``python3.exe`` and ``python3.x.exe`` as well as 318 ``python.exe`` (where ``3.x`` is the specific version you want to launch, 319 such as |version|). 320 321 Virtual environments can be created with ``python -m venv`` and activated 322 and used as normal. 323 324 If you have installed another version of Python and added it to your 325 ``PATH`` variable, it will be available as ``python.exe`` rather than the 326 one from the Microsoft Store. To access the new installation, use 327 ``python3.exe`` or ``python3.x.exe``. 328 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 337 Currently, the ``py.exe`` launcher cannot be used to start Python when it 338 has been installed from the Microsoft Store. 339 340 Because of restrictions on Microsoft Store apps, Python scripts may not have 341 full write access to shared locations such as ``TEMP`` and the registry. 342 Instead, it will write to a private copy. If your scripts must modify the 343 shared locations, you will need to install the full installer. 344 345 346 .. _windows-nuget: 347 348 The nuget.org packages 349 ====================== 350 351 .. versionadded:: 3.5.2 352 353 The nuget.org package is a reduced size Python environment intended for use on 354 continuous integration and build systems that do not have a system-wide 355 install of Python. While nuget is "the package manager for .NET", it also works 356 perfectly fine for packages containing build-time tools. 357 358 Visit `nuget.org <https://www.nuget.org/>`_ for the most up-to-date information 359 on using nuget. What follows is a summary that is sufficient for Python 360 developers. 361 362 The ``nuget.exe`` command line tool may be downloaded directly from 363 ``https://aka.ms/nugetclidl``, for example, using curl or PowerShell. With the 364 tool, the latest version of Python for 64-bit or 32-bit machines is installed 365 using:: 366 367 nuget.exe install python -ExcludeVersion -OutputDirectory . 368 nuget.exe install pythonx86 -ExcludeVersion -OutputDirectory . 369 370 To select a particular version, add a ``-Version 3.x.y``. The output directory 371 may be changed from ``.``, and the package will be installed into a 372 subdirectory. By default, the subdirectory is named the same as the package, 373 and without the ``-ExcludeVersion`` option this name will include the specific 374 version installed. Inside the subdirectory is a ``tools`` directory that 375 contains the Python installation:: 376 377 # Without -ExcludeVersion 378 > .\python.3.5.2\tools\python.exe -V 379 Python 3.5.2 380 381 # With -ExcludeVersion 382 > .\python\tools\python.exe -V 383 Python 3.5.2 384 385 In general, nuget packages are not upgradeable, and newer versions should be 386 installed side-by-side and referenced using the full path. Alternatively, 387 delete the package directory manually and install it again. Many CI systems 388 will do this automatically if they do not preserve files between builds. 389 390 Alongside the ``tools`` directory is a ``build\native`` directory. This 391 contains a MSBuild properties file ``python.props`` that can be used in a 392 C++ project to reference the Python install. Including the settings will 393 automatically use the headers and import libraries in your build. 394 395 The package information pages on nuget.org are 396 `www.nuget.org/packages/python <https://www.nuget.org/packages/python>`_ 397 for the 64-bit version and `www.nuget.org/packages/pythonx86 398 <https://www.nuget.org/packages/pythonx86>`_ for the 32-bit version. 399 400 401 .. _windows-embeddable: 402 403 The embeddable package 404 ====================== 405 406 .. versionadded:: 3.5 407 408 The embedded distribution is a ZIP file containing a minimal Python environment. 409 It is intended for acting as part of another application, rather than being 410 directly accessed by end-users. 411 412 When extracted, the embedded distribution is (almost) fully isolated from the 413 user's system, including environment variables, system registry settings, and 414 installed packages. The standard library is included as pre-compiled and 415 optimized ``.pyc`` files in a ZIP, and ``python3.dll``, ``python37.dll``, 416 ``python.exe`` and ``pythonw.exe`` are all provided. Tcl/tk (including all 417 dependants, such as Idle), pip and the Python documentation are not included. 418 419 .. note:: 420 421 The embedded distribution does not include the `Microsoft C Runtime 422 <https://www.microsoft.com/en-us/download/details.aspx?id=48145>`_ and it is 423 the responsibility of the application installer to provide this. The 424 runtime may have already been installed on a user's system previously or 425 automatically via Windows Update, and can be detected by finding 426 ``ucrtbase.dll`` in the system directory. 427 428 Third-party packages should be installed by the application installer alongside 429 the embedded distribution. Using pip to manage dependencies as for a regular 430 Python installation is not supported with this distribution, though with some 431 care it may be possible to include and use pip for automatic updates. In 432 general, third-party packages should be treated as part of the application 433 ("vendoring") so that the developer can ensure compatibility with newer 434 versions before providing updates to users. 435 436 The two recommended use cases for this distribution are described below. 437 438 Python Application 439 ------------------ 440 441 An application written in Python does not necessarily require users to be aware 442 of that fact. The embedded distribution may be used in this case to include a 443 private version of Python in an install package. Depending on how transparent it 444 should be (or conversely, how professional it should appear), there are two 445 options. 446 447 Using a specialized executable as a launcher requires some coding, but provides 448 the most transparent experience for users. With a customized launcher, there are 449 no obvious indications that the program is running on Python: icons can be 450 customized, company and version information can be specified, and file 451 associations behave properly. In most cases, a custom launcher should simply be 452 able to call ``Py_Main`` with a hard-coded command line. 453 454 The simpler approach is to provide a batch file or generated shortcut that 455 directly calls the ``python.exe`` or ``pythonw.exe`` with the required 456 command-line arguments. In this case, the application will appear to be Python 457 and not its actual name, and users may have trouble distinguishing it from other 458 running Python processes or file associations. 459 460 With the latter approach, packages should be installed as directories alongside 461 the Python executable to ensure they are available on the path. With the 462 specialized launcher, packages can be located in other locations as there is an 463 opportunity to specify the search path before launching the application. 464 465 Embedding Python 466 ---------------- 467 468 Applications written in native code often require some form of scripting 469 language, and the embedded Python distribution can be used for this purpose. In 470 general, the majority of the application is in native code, and some part will 471 either invoke ``python.exe`` or directly use ``python3.dll``. For either case, 472 extracting the embedded distribution to a subdirectory of the application 473 installation is sufficient to provide a loadable Python interpreter. 474 475 As with the application use, packages can be installed to any location as there 476 is an opportunity to specify search paths before initializing the interpreter. 477 Otherwise, there is no fundamental differences between using the embedded 478 distribution and a regular installation. 479 480 481 Alternative bundles 482 =================== 483 484 Besides the standard CPython distribution, there are modified packages including 485 additional functionality. The following is a list of popular versions and their 486 key features: 487 488 `ActivePython <https://www.activestate.com/activepython/>`_ 489 Installer with multi-platform compatibility, documentation, PyWin32 490 491 `Anaconda <https://www.anaconda.com/download/>`_ 492 Popular scientific modules (such as numpy, scipy and pandas) and the 493 ``conda`` package manager. 494 495 `Canopy <https://www.enthought.com/product/canopy/>`_ 496 A "comprehensive Python analysis environment" with editors and other 497 development tools. 498 499 `WinPython <https://winpython.github.io/>`_ 500 Windows-specific distribution with prebuilt scientific packages and 501 tools for building packages. 502 503 Note that these packages may not include the latest versions of Python or 504 other libraries, and are not maintained or supported by the core Python team. 505 506 507 508 Configuring Python 509 ================== 510 511 To run Python conveniently from a command prompt, you might consider changing 512 some default environment variables in Windows. While the installer provides an 513 option to configure the PATH and PATHEXT variables for you, this is only 514 reliable for a single, system-wide installation. If you regularly use multiple 515 versions of Python, consider using the :ref:`launcher`. 516 517 518 .. _setting-envvars: 519 520 Excursus: Setting environment variables 521 --------------------------------------- 522 523 Windows allows environment variables to be configured permanently at both the 524 User level and the System level, or temporarily in a command prompt. 525 526 To temporarily set environment variables, open Command Prompt and use the 527 :command:`set` command: 528 529 .. code-block:: doscon 530 531 C:\>set PATH=C:\Program Files\Python 3.7;%PATH% 532 C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib 533 C:\>python 534 535 These changes will apply to any further commands executed in that console, and 536 will be inherited by any applications started from the console. 537 538 Including the variable name within percent signs will expand to the existing 539 value, allowing you to add your new value at either the start or the end. 540 Modifying :envvar:`PATH` by adding the directory containing 541 :program:`python.exe` to the start is a common way to ensure the correct version 542 of Python is launched. 543 544 To permanently modify the default environment variables, click Start and search 545 for 'edit environment variables', or open System properties, :guilabel:`Advanced 546 system settings` and click the :guilabel:`Environment Variables` button. 547 In this dialog, you can add or modify User and System variables. To change 548 System variables, you need non-restricted access to your machine 549 (i.e. Administrator rights). 550 551 .. note:: 552 553 Windows will concatenate User variables *after* System variables, which may 554 cause unexpected results when modifying :envvar:`PATH`. 555 556 The :envvar:`PYTHONPATH` variable is used by all versions of Python 2 and 557 Python 3, so you should not permanently configure this variable unless it 558 only includes code that is compatible with all of your installed Python 559 versions. 560 561 .. seealso:: 562 563 https://www.microsoft.com/en-us/wdsi/help/folder-variables 564 Environment variables in Windows NT 565 566 https://technet.microsoft.com/en-us/library/cc754250.aspx 567 The SET command, for temporarily modifying environment variables 568 569 https://technet.microsoft.com/en-us/library/cc755104.aspx 570 The SETX command, for permanently modifying environment variables 571 572 https://support.microsoft.com/en-us/help/310519/how-to-manage-environment-variables-in-windows-xp 573 How To Manage Environment Variables in Windows XP 574 575 https://www.chem.gla.ac.uk/~louis/software/faq/q1.html 576 Setting Environment variables, Louis J. Farrugia 577 578 .. _windows-path-mod: 579 580 Finding the Python executable 581 ----------------------------- 582 583 .. versionchanged:: 3.5 584 585 Besides using the automatically created start menu entry for the Python 586 interpreter, you might want to start Python in the command prompt. The 587 installer has an option to set that up for you. 588 589 On the first page of the installer, an option labelled "Add Python to PATH" 590 may be selected to have the installer add the install location into the 591 :envvar:`PATH`. The location of the :file:`Scripts\\` folder is also added. 592 This allows you to type :command:`python` to run the interpreter, and 593 :command:`pip` for the package installer. Thus, you can also execute your 594 scripts with command line options, see :ref:`using-on-cmdline` documentation. 595 596 If you don't enable this option at install time, you can always re-run the 597 installer, select Modify, and enable it. Alternatively, you can manually 598 modify the :envvar:`PATH` using the directions in :ref:`setting-envvars`. You 599 need to set your :envvar:`PATH` environment variable to include the directory 600 of your Python installation, delimited by a semicolon from other entries. An 601 example variable could look like this (assuming the first two entries already 602 existed):: 603 604 C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.7 605 606 .. _launcher: 607 608 Python Launcher for Windows 609 =========================== 610 611 .. versionadded:: 3.3 612 613 The Python launcher for Windows is a utility which aids in locating and 614 executing of different Python versions. It allows scripts (or the 615 command-line) to indicate a preference for a specific Python version, and 616 will locate and execute that version. 617 618 Unlike the :envvar:`PATH` variable, the launcher will correctly select the most 619 appropriate version of Python. It will prefer per-user installations over 620 system-wide ones, and orders by language version rather than using the most 621 recently installed version. 622 623 The launcher was originally specified in :pep:`397`. 624 625 Getting started 626 --------------- 627 628 From the command-line 629 ^^^^^^^^^^^^^^^^^^^^^ 630 631 .. versionchanged:: 3.6 632 633 System-wide installations of Python 3.3 and later will put the launcher on your 634 :envvar:`PATH`. The launcher is compatible with all available versions of 635 Python, so it does not matter which version is installed. To check that the 636 launcher is available, execute the following command in Command Prompt: 637 638 :: 639 640 py 641 642 You should find that the latest version of Python you have installed is 643 started - it can be exited as normal, and any additional command-line 644 arguments specified will be sent directly to Python. 645 646 If you have multiple versions of Python installed (e.g., 2.7 and |version|) you 647 will have noticed that Python |version| was started - to launch Python 2.7, try 648 the command: 649 650 :: 651 652 py -2.7 653 654 If you want the latest version of Python 2.x you have installed, try the 655 command: 656 657 :: 658 659 py -2 660 661 You should find the latest version of Python 2.x starts. 662 663 If you see the following error, you do not have the launcher installed: 664 665 :: 666 667 'py' is not recognized as an internal or external command, 668 operable program or batch file. 669 670 Per-user installations of Python do not add the launcher to :envvar:`PATH` 671 unless the option was selected on installation. 672 673 Virtual environments 674 ^^^^^^^^^^^^^^^^^^^^ 675 676 .. versionadded:: 3.5 677 678 If the launcher is run with no explicit Python version specification, and a 679 virtual environment (created with the standard library :mod:`venv` module or 680 the external ``virtualenv`` tool) active, the launcher will run the virtual 681 environment's interpreter rather than the global one. To run the global 682 interpreter, either deactivate the virtual environment, or explicitly specify 683 the global Python version. 684 685 From a script 686 ^^^^^^^^^^^^^ 687 688 Let's create a test Python script - create a file called ``hello.py`` with the 689 following contents 690 691 .. code-block:: python 692 693 #! python 694 import sys 695 sys.stdout.write("hello from Python %s\n" % (sys.version,)) 696 697 From the directory in which hello.py lives, execute the command: 698 699 :: 700 701 py hello.py 702 703 You should notice the version number of your latest Python 2.x installation 704 is printed. Now try changing the first line to be: 705 706 .. code-block:: python 707 708 #! python3 709 710 Re-executing the command should now print the latest Python 3.x information. 711 As with the above command-line examples, you can specify a more explicit 712 version qualifier. Assuming you have Python 2.6 installed, try changing the 713 first line to ``#! python2.6`` and you should find the 2.6 version 714 information printed. 715 716 Note that unlike interactive use, a bare "python" will use the latest 717 version of Python 2.x that you have installed. This is for backward 718 compatibility and for compatibility with Unix, where the command ``python`` 719 typically refers to Python 2. 720 721 From file associations 722 ^^^^^^^^^^^^^^^^^^^^^^ 723 724 The launcher should have been associated with Python files (i.e. ``.py``, 725 ``.pyw``, ``.pyc`` files) when it was installed. This means that 726 when you double-click on one of these files from Windows explorer the launcher 727 will be used, and therefore you can use the same facilities described above to 728 have the script specify the version which should be used. 729 730 The key benefit of this is that a single launcher can support multiple Python 731 versions at the same time depending on the contents of the first line. 732 733 Shebang Lines 734 ------------- 735 736 If the first line of a script file starts with ``#!``, it is known as a 737 "shebang" line. Linux and other Unix like operating systems have native 738 support for such lines and they are commonly used on such systems to indicate 739 how a script should be executed. This launcher allows the same facilities to 740 be used with Python scripts on Windows and the examples above demonstrate their 741 use. 742 743 To allow shebang lines in Python scripts to be portable between Unix and 744 Windows, this launcher supports a number of 'virtual' commands to specify 745 which interpreter to use. The supported virtual commands are: 746 747 * ``/usr/bin/env python`` 748 * ``/usr/bin/python`` 749 * ``/usr/local/bin/python`` 750 * ``python`` 751 752 For example, if the first line of your script starts with 753 754 .. code-block:: sh 755 756 #! /usr/bin/python 757 758 The default Python will be located and used. As many Python scripts written 759 to work on Unix will already have this line, you should find these scripts can 760 be used by the launcher without modification. If you are writing a new script 761 on Windows which you hope will be useful on Unix, you should use one of the 762 shebang lines starting with ``/usr``. 763 764 Any of the above virtual commands can be suffixed with an explicit version 765 (either just the major version, or the major and minor version) - for example 766 ``/usr/bin/python2.7`` - which will cause that specific version to be located 767 and used. 768 769 The ``/usr/bin/env`` form of shebang line has one further special property. 770 Before looking for installed Python interpreters, this form will search the 771 executable :envvar:`PATH` for a Python executable. This corresponds to the 772 behaviour of the Unix ``env`` program, which performs a :envvar:`PATH` search. 773 774 Arguments in shebang lines 775 -------------------------- 776 777 The shebang lines can also specify additional options to be passed to the 778 Python interpreter. For example, if you have a shebang line: 779 780 .. code-block:: sh 781 782 #! /usr/bin/python -v 783 784 Then Python will be started with the ``-v`` option 785 786 Customization 787 ------------- 788 789 Customization via INI files 790 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 791 792 Two .ini files will be searched by the launcher - ``py.ini`` in the current 793 user's "application data" directory (i.e. the directory returned by calling the 794 Windows function ``SHGetFolderPath`` with ``CSIDL_LOCAL_APPDATA``) and ``py.ini`` in the 795 same directory as the launcher. The same .ini files are used for both the 796 'console' version of the launcher (i.e. py.exe) and for the 'windows' version 797 (i.e. pyw.exe). 798 799 Customization specified in the "application directory" will have precedence over 800 the one next to the executable, so a user, who may not have write access to the 801 .ini file next to the launcher, can override commands in that global .ini file. 802 803 Customizing default Python versions 804 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 805 806 In some cases, a version qualifier can be included in a command to dictate 807 which version of Python will be used by the command. A version qualifier 808 starts with a major version number and can optionally be followed by a period 809 ('.') and a minor version specifier. If the minor qualifier is specified, it 810 may optionally be followed by "-32" to indicate the 32-bit implementation of 811 that version be used. 812 813 For example, a shebang line of ``#!python`` has no version qualifier, while 814 ``#!python3`` has a version qualifier which specifies only a major version. 815 816 If no version qualifiers are found in a command, the environment variable 817 ``PY_PYTHON`` can be set to specify the default version qualifier - the default 818 value is "2". Note this value could specify just a major version (e.g. "2") or 819 a major.minor qualifier (e.g. "2.6"), or even major.minor-32. 820 821 If no minor version qualifiers are found, the environment variable 822 ``PY_PYTHON{major}`` (where ``{major}`` is the current major version qualifier 823 as determined above) can be set to specify the full version. If no such option 824 is found, the launcher will enumerate the installed Python versions and use 825 the latest minor release found for the major version, which is likely, 826 although not guaranteed, to be the most recently installed version in that 827 family. 828 829 On 64-bit Windows with both 32-bit and 64-bit implementations of the same 830 (major.minor) Python version installed, the 64-bit version will always be 831 preferred. This will be true for both 32-bit and 64-bit implementations of the 832 launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation 833 of the specified version if available. This is so the behavior of the launcher 834 can be predicted knowing only what versions are installed on the PC and 835 without regard to the order in which they were installed (i.e., without knowing 836 whether a 32 or 64-bit version of Python and corresponding launcher was 837 installed last). As noted above, an optional "-32" suffix can be used on a 838 version specifier to change this behaviour. 839 840 Examples: 841 842 * If no relevant options are set, the commands ``python`` and 843 ``python2`` will use the latest Python 2.x version installed and 844 the command ``python3`` will use the latest Python 3.x installed. 845 846 * The commands ``python3.1`` and ``python2.7`` will not consult any 847 options at all as the versions are fully specified. 848 849 * If ``PY_PYTHON=3``, the commands ``python`` and ``python3`` will both use 850 the latest installed Python 3 version. 851 852 * If ``PY_PYTHON=3.1-32``, the command ``python`` will use the 32-bit 853 implementation of 3.1 whereas the command ``python3`` will use the latest 854 installed Python (PY_PYTHON was not considered at all as a major 855 version was specified.) 856 857 * If ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1``, the commands 858 ``python`` and ``python3`` will both use specifically 3.1 859 860 In addition to environment variables, the same settings can be configured 861 in the .INI file used by the launcher. The section in the INI file is 862 called ``[defaults]`` and the key name will be the same as the 863 environment variables without the leading ``PY_`` prefix (and note that 864 the key names in the INI file are case insensitive.) The contents of 865 an environment variable will override things specified in the INI file. 866 867 For example: 868 869 * Setting ``PY_PYTHON=3.1`` is equivalent to the INI file containing: 870 871 .. code-block:: ini 872 873 [defaults] 874 python=3.1 875 876 * Setting ``PY_PYTHON=3`` and ``PY_PYTHON3=3.1`` is equivalent to the INI file 877 containing: 878 879 .. code-block:: ini 880 881 [defaults] 882 python=3 883 python3=3.1 884 885 Diagnostics 886 ----------- 887 888 If an environment variable ``PYLAUNCH_DEBUG`` is set (to any value), the 889 launcher will print diagnostic information to stderr (i.e. to the console). 890 While this information manages to be simultaneously verbose *and* terse, it 891 should allow you to see what versions of Python were located, why a 892 particular version was chosen and the exact command-line used to execute the 893 target Python. 894 895 896 897 .. _finding_modules: 898 899 Finding modules 900 =============== 901 902 Python usually stores its library (and thereby your site-packages folder) in the 903 installation directory. So, if you had installed Python to 904 :file:`C:\\Python\\`, the default library would reside in 905 :file:`C:\\Python\\Lib\\` and third-party modules should be stored in 906 :file:`C:\\Python\\Lib\\site-packages\\`. 907 908 To completely override :data:`sys.path`, create a ``._pth`` file with the same 909 name as the DLL (``python37._pth``) or the executable (``python._pth``) and 910 specify one line for each path to add to :data:`sys.path`. The file based on the 911 DLL name overrides the one based on the executable, which allows paths to be 912 restricted for any program loading the runtime if desired. 913 914 When the file exists, all registry and environment variables are ignored, 915 isolated mode is enabled, and :mod:`site` is not imported unless one line in the 916 file specifies ``import site``. Blank paths and lines starting with ``#`` are 917 ignored. Each path may be absolute or relative to the location of the file. 918 Import statements other than to ``site`` are not permitted, and arbitrary code 919 cannot be specified. 920 921 Note that ``.pth`` files (without leading underscore) will be processed normally 922 by the :mod:`site` module when ``import site`` has been specified. 923 924 When no ``._pth`` file is found, this is how :data:`sys.path` is populated on 925 Windows: 926 927 * An empty entry is added at the start, which corresponds to the current 928 directory. 929 930 * If the environment variable :envvar:`PYTHONPATH` exists, as described in 931 :ref:`using-on-envvars`, its entries are added next. Note that on Windows, 932 paths in this variable must be separated by semicolons, to distinguish them 933 from the colon used in drive identifiers (``C:\`` etc.). 934 935 * Additional "application paths" can be added in the registry as subkeys of 936 :samp:`\\SOFTWARE\\Python\\PythonCore\\{version}\\PythonPath` under both the 937 ``HKEY_CURRENT_USER`` and ``HKEY_LOCAL_MACHINE`` hives. Subkeys which have 938 semicolon-delimited path strings as their default value will cause each path 939 to be added to :data:`sys.path`. (Note that all known installers only use 940 HKLM, so HKCU is typically empty.) 941 942 * If the environment variable :envvar:`PYTHONHOME` is set, it is assumed as 943 "Python Home". Otherwise, the path of the main Python executable is used to 944 locate a "landmark file" (either ``Lib\os.py`` or ``pythonXY.zip``) to deduce 945 the "Python Home". If a Python home is found, the relevant sub-directories 946 added to :data:`sys.path` (``Lib``, ``plat-win``, etc) are based on that 947 folder. Otherwise, the core Python path is constructed from the PythonPath 948 stored in the registry. 949 950 * If the Python Home cannot be located, no :envvar:`PYTHONPATH` is specified in 951 the environment, and no registry entries can be found, a default path with 952 relative entries is used (e.g. ``.\Lib;.\plat-win``, etc). 953 954 If a ``pyvenv.cfg`` file is found alongside the main executable or in the 955 directory one level above the executable, the following variations apply: 956 957 * If ``home`` is an absolute path and :envvar:`PYTHONHOME` is not set, this 958 path is used instead of the path to the main executable when deducing the 959 home location. 960 961 The end result of all this is: 962 963 * When running :file:`python.exe`, or any other .exe in the main Python 964 directory (either an installed version, or directly from the PCbuild 965 directory), the core path is deduced, and the core paths in the registry are 966 ignored. Other "application paths" in the registry are always read. 967 968 * When Python is hosted in another .exe (different directory, embedded via COM, 969 etc), the "Python Home" will not be deduced, so the core path from the 970 registry is used. Other "application paths" in the registry are always read. 971 972 * If Python can't find its home and there are no registry value (frozen .exe, 973 some very strange installation setup) you get a path with some default, but 974 relative, paths. 975 976 For those who want to bundle Python into their application or distribution, the 977 following advice will prevent conflicts with other installations: 978 979 * Include a ``._pth`` file alongside your executable containing the 980 directories to include. This will ignore paths listed in the registry and 981 environment variables, and also ignore :mod:`site` unless ``import site`` is 982 listed. 983 984 * If you are loading :file:`python3.dll` or :file:`python37.dll` in your own 985 executable, explicitly call :c:func:`Py_SetPath` or (at least) 986 :c:func:`Py_SetProgramName` before :c:func:`Py_Initialize`. 987 988 * Clear and/or overwrite :envvar:`PYTHONPATH` and set :envvar:`PYTHONHOME` 989 before launching :file:`python.exe` from your application. 990 991 * If you cannot use the previous suggestions (for example, you are a 992 distribution that allows people to run :file:`python.exe` directly), ensure 993 that the landmark file (:file:`Lib\\os.py`) exists in your install directory. 994 (Note that it will not be detected inside a ZIP file, but a correctly named 995 ZIP file will be detected instead.) 996 997 These will ensure that the files in a system-wide installation will not take 998 precedence over the copy of the standard library bundled with your application. 999 Otherwise, your users may experience problems using your application. Note that 1000 the first suggestion is the best, as the others may still be susceptible to 1001 non-standard paths in the registry and user site-packages. 1002 1003 .. versionchanged:: 1004 3.6 1005 1006 * Adds ``._pth`` file support and removes ``applocal`` option from 1007 ``pyvenv.cfg``. 1008 * Adds ``pythonXX.zip`` as a potential landmark when directly adjacent 1009 to the executable. 1010 1011 .. deprecated:: 1012 3.6 1013 1014 Modules specified in the registry under ``Modules`` (not ``PythonPath``) 1015 may be imported by :class:`importlib.machinery.WindowsRegistryFinder`. 1016 This finder is enabled on Windows in 3.6.0 and earlier, but may need to 1017 be explicitly added to :attr:`sys.meta_path` in the future. 1018 1019 Additional modules 1020 ================== 1021 1022 Even though Python aims to be portable among all platforms, there are features 1023 that are unique to Windows. A couple of modules, both in the standard library 1024 and external, and snippets exist to use these features. 1025 1026 The Windows-specific standard modules are documented in 1027 :ref:`mswin-specific-services`. 1028 1029 PyWin32 1030 ------- 1031 1032 The `PyWin32 <https://pypi.org/project/pywin32>`_ module by Mark Hammond 1033 is a collection of modules for advanced Windows-specific support. This includes 1034 utilities for: 1035 1036 * `Component Object Model <https://www.microsoft.com/com/>`_ (COM) 1037 * Win32 API calls 1038 * Registry 1039 * Event log 1040 * `Microsoft Foundation Classes <https://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx>`_ (MFC) 1041 user interfaces 1042 1043 `PythonWin <https://web.archive.org/web/20060524042422/ 1044 https://www.python.org/windows/pythonwin/>`_ is a sample MFC application 1045 shipped with PyWin32. It is an embeddable IDE with a built-in debugger. 1046 1047 .. seealso:: 1048 1049 `Win32 How Do I...? <http://timgolden.me.uk/python/win32_how_do_i.html>`_ 1050 by Tim Golden 1051 1052 `Python and COM <http://www.boddie.org.uk/python/COM.html>`_ 1053 by David and Paul Boddie 1054 1055 1056 cx_Freeze 1057 --------- 1058 1059 `cx_Freeze <https://anthony-tuininga.github.io/cx_Freeze/>`_ is a :mod:`distutils` 1060 extension (see :ref:`extending-distutils`) which wraps Python scripts into 1061 executable Windows programs (:file:`{*}.exe` files). When you have done this, 1062 you can distribute your application without requiring your users to install 1063 Python. 1064 1065 1066 WConio 1067 ------ 1068 1069 Since Python's advanced terminal handling layer, :mod:`curses`, is restricted to 1070 Unix-like systems, there is a library exclusive to Windows as well: Windows 1071 Console I/O for Python. 1072 1073 `WConio <http://newcenturycomputers.net/projects/wconio.html>`_ is a wrapper for 1074 Turbo-C's :file:`CONIO.H`, used to create text user interfaces. 1075 1076 1077 1078 Compiling Python on Windows 1079 =========================== 1080 1081 If you want to compile CPython yourself, first thing you should do is get the 1082 `source <https://www.python.org/downloads/source/>`_. You can download either the 1083 latest release's source or just grab a fresh `checkout 1084 <https://devguide.python.org/setup/#getting-the-source-code>`_. 1085 1086 The source tree contains a build solution and project files for Microsoft 1087 Visual Studio 2015, which is the compiler used to build the official Python 1088 releases. These files are in the :file:`PCbuild` directory. 1089 1090 Check :file:`PCbuild/readme.txt` for general information on the build process. 1091 1092 1093 For extension modules, consult :ref:`building-on-windows`. 1094 1095 .. seealso:: 1096 1097 `Python + Windows + distutils + SWIG + gcc MinGW <http://sebsauvage.net/python/mingw.html>`_ 1098 or "Creating Python extensions in C/C++ with SWIG and compiling them with 1099 MinGW gcc under Windows" or "Installing Python extension with distutils 1100 and without Microsoft Visual C++" by Sbastien Sauvage, 2003 1101 1102 `MingW -- Python extensions <http://oldwiki.mingw.org/index.php/Python%20extensions>`_ 1103 by Trent Apted et al, 2007 1104 1105 1106 Other Platforms 1107 =============== 1108 1109 With ongoing development of Python, some platforms that used to be supported 1110 earlier are no longer supported (due to the lack of users or developers). 1111 Check :pep:`11` for details on all unsupported platforms. 1112 1113 * `Windows CE <http://pythonce.sourceforge.net/>`_ is still supported. 1114 * The `Cygwin <https://cygwin.com/>`_ installer offers to install the Python 1115 interpreter as well (cf. `Cygwin package source 1116 <ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/ 1117 release/python>`_, `Maintainer releases 1118 <http://www.tishler.net/jason/software/python/>`_) 1119 1120 See `Python for Windows <https://www.python.org/downloads/windows/>`_ 1121 for detailed information about platforms with pre-compiled installers. 1122