How To Compile Python In Windows
4. Using Python on Windows¶
This document aims to give an overview of Windows-specific behaviour yous should know nearly when using Python on Microsoft Windows.
Unlike about Unix systems and services, Windows does not include a organisation supported installation of Python. To make Python bachelor, the CPython team has compiled Windows installers (MSI packages) with every release for many years. These installers are primarily intended to add a per-user installation of Python, with the core interpreter and library being used by a single user. The installer is also able to install for all users of a single machine, and a separate ZIP file is available for application-local distributions.
Every bit specified in PEP 11, a Python release only supports a Windows platform while Microsoft considers the platform under extended support. This means that Python 3.10 supports Windows 8.i and newer. If yous require Windows 7 support, delight install Python 3.8.
There are a number of different installers bachelor for Windows, each with sure benefits and downsides.
The full installer contains all components and is the best pick for developers using Python for any kind of project.
The Microsoft Store package is a elementary installation of Python that is suitable for running scripts and packages, and using IDLE or other development environments. It requires Windows x, just can exist safely installed without corrupting other programs. Information technology besides provides many convenient commands for launching Python and its tools.
The nuget.org packages are lightweight installations intended for continuous integration systems. It can exist used to build Python packages or run scripts, but is not updateable and has no user interface tools.
The embeddable package is a minimal package of Python suitable for embedding into a larger application.
iv.one. The full installer¶
four.ane.1. Installation steps¶
Iv Python 3.ten installers are available for download - two each for the 32-chip and 64-bit versions of the interpreter. The web installer is a small initial download, and it will automatically download the required components every bit necessary. The offline installer includes the components necessary for a default installation and only requires an internet connectedness for optional features. Encounter Installing Without Downloading for other ways to avoid downloading during installation.
Later on starting the installer, one of two options may be selected:
If y'all select "Install Now":
-
You will not demand to be an ambassador (unless a system update for the C Runtime Library is required or you install the Python Launcher for Windows for all users)
-
Python will be installed into your user directory
-
The Python Launcher for Windows volition be installed co-ordinate to the selection at the lesser of the first page
-
The standard library, test suite, launcher and pip will be installed
-
If selected, the install directory will be added to your
PATH -
Shortcuts will only exist visible for the current user
Selecting "Customize installation" will permit you to select the features to install, the installation location and other options or postal service-install actions. To install debugging symbols or binaries, you will need to apply this option.
To perform an all-users installation, you should select "Customize installation". In this instance:
-
You may exist required to provide administrative credentials or approval
-
Python will be installed into the Program Files directory
-
The Python Launcher for Windows volition be installed into the Windows directory
-
Optional features may be selected during installation
-
The standard library tin can exist pre-compiled to bytecode
-
If selected, the install directory will be added to the system
PATH -
Shortcuts are available for all users
4.1.2. Removing the MAX_PATH Limitation¶
Windows historically has limited path lengths to 260 characters. This meant that paths longer than this would non resolve and errors would result.
In the latest versions of Windows, this limitation can be expanded to approximately 32,000 characters. Your administrator volition need to activate the "Enable Win32 long paths" group policy, or set LongPathsEnabled to 1 in the registry cardinal HKEY_LOCAL_MACHINE\Organisation\CurrentControlSet\Control\FileSystem .
This allows the open() function, the os module and most other path functionality to take and return paths longer than 260 characters.
After changing the above choice, no further configuration is required.
Changed in version 3.6: Support for long paths was enabled in Python.
4.1.3. Installing Without UI¶
All of the options bachelor in the installer UI can also be specified from the control line, allowing scripted installers to replicate an installation on many machines without user interaction. These options may also exist set without suppressing the UI in order to change some of the defaults.
To completely hide the installer UI and install Python silently, pass the /quiet choice. To skip past the user interaction only still display progress and errors, pass the /passive selection. The /uninstall option may be passed to immediately begin removing Python - no confirmation prompt will exist displayed.
All other options are passed equally name=value , where the value is usually 0 to disable a feature, 1 to enable a feature, or a path. The total list of available options is shown below.
| Proper name | Description | Default |
|---|---|---|
| InstallAllUsers | Perform a arrangement-wide installation. | 0 |
| TargetDir | The installation directory | Selected based on InstallAllUsers |
| DefaultAllUsersTargetDir | The default installation directory for all-user installs | |
| DefaultJustForMeTargetDir | The default install directory for just-for-me installs | |
| DefaultCustomTargetDir | The default custom install directory displayed in the UI | (empty) |
| AssociateFiles | Create file associations if the launcher is also installed. | ane |
| CompileAll | Compile all | 0 |
| PrependPath | Add install and Scripts directories to | 0 |
| Shortcuts | Create shortcuts for the interpreter, documentation and IDLE if installed. | ane |
| Include_doc | Install Python manual | 1 |
| Include_debug | Install debug binaries | 0 |
| Include_dev | Install developer headers and libraries | ane |
| Include_exe | Install | ane |
| Include_launcher | Install Python Launcher for Windows. | 1 |
| InstallLauncherAllUsers | Installs Python Launcher for Windows for all users. | ane |
| Include_lib | Install standard library and extension modules | 1 |
| Include_pip | Install bundled pip and setuptools | 1 |
| Include_symbols | Install debugging symbols (*.pdb) | 0 |
| Include_tcltk | Install Tcl/Tk support and IDLE | i |
| Include_test | Install standard library test suite | 1 |
| Include_tools | Install utility scripts | ane |
| LauncherOnly | Only installs the launcher. This will override most other options. | 0 |
| SimpleInstall | Disable most install UI | 0 |
| SimpleInstallDescription | A custom bulletin to display when the simplified install UI is used. | (empty) |
For example, to silently install a default, system-wide Python installation, you could apply the following command (from an elevated command prompt):
python-3.9.0.exe /placidity InstallAllUsers=ane PrependPath=i Include_test=0 To allow users to easily install a personal copy of Python without the test suite, you could provide a shortcut with the post-obit control. This will display a simplified initial page and disallow customization:
python-3.ix.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0 SimpleInstall=1 SimpleInstallDescription="Simply for me, no examination suite." (Note that omitting the launcher also omits file associations, and is only recommended for per-user installs when there is likewise a system-wide installation that included the launcher.)
The options listed above can likewise be provided in a file named unattend.xml alongside the executable. This file specifies a list of options and values. When a value is provided as an attribute, information technology will be converted to a number if possible. Values provided equally chemical element text are always left as strings. This instance file sets the aforementioned options as the previous instance:
<Options> <Choice Proper name= "InstallAllUsers" Value= "no" /> <Choice Name= "Include_launcher" Value= "0" /> <Pick Proper name= "Include_test" Value= "no" /> <Choice Name= "SimpleInstall" Value= "yes" /> <Option Name= "SimpleInstallDescription" >Just for me, no examination suite</Choice> </Options> four.1.four. Installing Without Downloading¶
As some features of Python are not included in the initial installer download, selecting those features may require an internet connection. To avoid this need, all possible components may be downloaded on-need to create a consummate layout that volition no longer require an internet connection regardless of the selected features. Note that this download may be bigger than required, but where a big number of installations are going to be performed information technology is very useful to accept a locally cached re-create.
Execute the following command from Control Prompt to download all possible required files. Recollect to substitute python-3.nine.0.exe for the actual name of your installer, and to create layouts in their own directories to avoid collisions between files with the aforementioned proper name.
python-3.9.0.exe /layout [optional target directory] You may too specify the /placidity choice to hibernate the progress display.
4.ane.v. Modifying an install¶
One time Python has been installed, you lot can add together or remove features through the Programs and Features tool that is part of Windows. Select the Python entry and choose "Uninstall/Change" to open the installer in maintenance mode.
"Modify" allows you to add together or remove features by modifying the checkboxes - unchanged checkboxes will not install or remove anything. Some options cannot be changed in this style, such equally the install directory; to modify these, you will need to remove and then reinstall Python completely.
"Repair" will verify all the files that should be installed using the current settings and supervene upon any that accept been removed or modified.
"Uninstall" will remove Python entirely, with the exception of the Python Launcher for Windows, which has its own entry in Programs and Features.
iv.2. The Microsoft Store parcel¶
New in version three.vii.2.
The Microsoft Store packet is an hands installable Python interpreter that is intended mainly for interactive employ, for case, by students.
To install the bundle, ensure yous have the latest Windows 10 updates and search the Microsoft Store app for "Python 3.x". Ensure that the app you select is published by the Python Software Foundation, and install it.
Alert
Python will always be available for gratis on the Microsoft Store. If you lot are asked to pay for it, you have not selected the correct packet.
After installation, Python may be launched by finding information technology in Start. Alternatively, information technology will be available from any Command Prompt or PowerShell session by typing python . Further, pip and IDLE may be used past typing pip or idle . IDLE can likewise exist found in Get-go.
All three commands are as well bachelor with version number suffixes, for example, every bit python3.exe and python3.x.exe as well as python.exe (where 3.x is the specific version you want to launch, such as 3.10). Open up "Manage App Execution Aliases" through Start to select which version of Python is associated with each control. It is recommended to make sure that pip and idle are consistent with whichever version of python is selected.
Virtual environments tin be created with python -thou venv and activated and used as normal.
If you have installed another version of Python and added it to your PATH variable, information technology will be available as python.exe rather than the one from the Microsoft Store. To access the new installation, use python3.exe or python3.x.exe .
The py.exe launcher will observe this Python installation, but volition prefer installations from the traditional installer.
To remove Python, open up Settings and use Apps and Features, or else find Python in Start and right-click to select Uninstall. Uninstalling will remove all packages you installed directly into this Python installation, merely will non remove whatsoever virtual environments
4.two.1. Known Problems¶
Because of restrictions on Microsoft Shop apps, Python scripts may not have full write admission to shared locations such as TEMP and the registry. Instead, it will write to a individual re-create. If your scripts must modify the shared locations, you volition demand to install the full installer.
For more item on the technical basis for these limitations, please consult Microsoft's documentation on packaged full-trust apps, currently bachelor at docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-backside-the-scenes
4.iii. The nuget.org packages¶
New in version 3.five.ii.
The nuget.org bundle is a reduced size Python environment intended for use on continuous integration and build systems that do non have a organisation-wide install of Python. While nuget is "the bundle manager for .Cyberspace", information technology also works perfectly fine for packages containing build-fourth dimension tools.
Visit nuget.org for the most up-to-date information on using nuget. What follows is a summary that is sufficient for Python developers.
The nuget.exe command line tool may be downloaded direct from https://aka.ms/nugetclidl , for instance, using curl or PowerShell. With the tool, the latest version of Python for 64-chip or 32-bit machines is installed using:
nuget.exe install python -ExcludeVersion -OutputDirectory . nuget.exe install pythonx86 -ExcludeVersion -OutputDirectory . To select a detail version, add a -Version 3.ten.y . The output directory may be changed from . , and the packet will exist installed into a subdirectory. By default, the subdirectory is named the same every bit the package, and without the -ExcludeVersion option this proper name will include the specific version installed. Inside the subdirectory is a tools directory that contains the Python installation:
# Without -ExcludeVersion > .\python.3.5.ii\tools\python.exe -5 Python 3.5.two # With -ExcludeVersion > .\python\tools\python.exe -5 Python 3.five.2 In general, nuget packages are non upgradeable, and newer versions should be installed side-past-side and referenced using the total path. Alternatively, delete the package directory manually and install it again. Many CI systems will do this automatically if they do not preserve files between builds.
Alongside the tools directory is a build\native directory. This contains a MSBuild backdrop file python.props that can be used in a C++ project to reference the Python install. Including the settings volition automatically apply the headers and import libraries in your build.
The parcel information pages on nuget.org are www.nuget.org/packages/python for the 64-chip version and www.nuget.org/packages/pythonx86 for the 32-bit version.
four.4. The embeddable package¶
New in version 3.5.
The embedded distribution is a ZIP file containing a minimal Python environment. It is intended for interim as part of another awarding, rather than being directly accessed past end-users.
When extracted, the embedded distribution is (nearly) fully isolated from the user's organization, including environs variables, system registry settings, and installed packages. The standard library is included as pre-compiled and optimized .pyc files in a Zip, and python3.dll , python37.dll , python.exe and pythonw.exe are all provided. Tcl/tk (including all dependants, such as Idle), pip and the Python documentation are not included.
Note
The embedded distribution does non include the Microsoft C Runtime and information technology is the responsibleness of the application installer to provide this. The runtime may have already been installed on a user's system previously or automatically via Windows Update, and can be detected by finding ucrtbase.dll in the system directory.
Tertiary-party packages should be installed by the awarding installer alongside the embedded distribution. Using pip to manage dependencies equally for a regular Python installation is not supported with this distribution, though with some care it may exist possible to include and utilize pip for automatic updates. In full general, third-party packages should be treated as office of the awarding ("vendoring") so that the developer tin ensure compatibility with newer versions before providing updates to users.
The two recommended use cases for this distribution are described beneath.
4.four.1. Python Application¶
An application written in Python does not necessarily crave users to be aware of that fact. The embedded distribution may be used in this case to include a individual version of Python in an install package. Depending on how transparent information technology should be (or conversely, how professional information technology should appear), there are 2 options.
Using a specialized executable as a launcher requires some coding, but provides the about transparent feel for users. With a customized launcher, there are no obvious indications that the plan is running on Python: icons can be customized, company and version data can be specified, and file associations carry properly. In most cases, a custom launcher should simply exist able to call Py_Main with a difficult-coded command line.
The simpler approach is to provide a batch file or generated shortcut that directly calls the python.exe or pythonw.exe with the required command-line arguments. In this case, the application will appear to exist Python and not its actual proper noun, and users may have trouble distinguishing it from other running Python processes or file associations.
With the latter approach, packages should be installed equally directories alongside the Python executable to ensure they are bachelor on the path. With the specialized launcher, packages tin can be located in other locations equally there is an opportunity to specify the search path earlier launching the application.
4.4.2. Embedding Python¶
Applications written in native code frequently crave some form of scripting language, and the embedded Python distribution can be used for this purpose. In general, the majority of the awarding is in native lawmaking, and some part volition either invoke python.exe or direct apply python3.dll . For either example, extracting the embedded distribution to a subdirectory of the application installation is sufficient to provide a loadable Python interpreter.
As with the application apply, packages can be installed to whatsoever location equally there is an opportunity to specify search paths before initializing the interpreter. Otherwise, at that place is no fundamental differences betwixt using the embedded distribution and a regular installation.
4.5. Alternative bundles¶
Besides the standard CPython distribution, there are modified packages including boosted functionality. The post-obit is a list of popular versions and their key features:
- ActivePython
-
Installer with multi-platform compatibility, documentation, PyWin32
- Anaconda
-
Popular scientific modules (such as numpy, scipy and pandas) and the
condapackage director. - Canopy
-
A "comprehensive Python analysis environment" with editors and other development tools.
- WinPython
-
Windows-specific distribution with prebuilt scientific packages and tools for building packages.
Note that these packages may not include the latest versions of Python or other libraries, and are not maintained or supported past the core Python squad.
four.vi. Configuring Python¶
To run Python conveniently from a command prompt, you lot might consider changing some default environment variables in Windows. While the installer provides an option to configure the PATH and PATHEXT variables for you, this is simply reliable for a single, system-wide installation. If you regularly apply multiple versions of Python, consider using the Python Launcher for Windows.
4.half-dozen.ane. Excursus: Setting environment variables¶
Windows allows environment variables to be configured permanently at both the User level and the System level, or temporarily in a command prompt.
To temporarily set environs variables, open Command Prompt and use the set command:
C:\> fix PATH =C:\Program Files\Python iii.ix;%PATH% C:\> set PYTHONPATH = %PYTHONPATH%;C:\My_python_lib C:\>python These changes volition utilize to any further commands executed in that console, and will be inherited by any applications started from the console.
Including the variable name within percent signs volition expand to the existing value, allowing you to add your new value at either the start or the end. Modifying PATH by adding the directory containing python.exe to the offset is a common way to ensure the correct version of Python is launched.
To permanently modify the default surround variables, click First and search for 'edit environment variables', or open System properties, Avant-garde system settings and click the Surround Variables push. In this dialog, you lot can add together or modify User and Organisation variables. To change System variables, you need non-restricted access to your machine (i.eastward. Administrator rights).
Annotation
Windows volition concatenate User variables after System variables, which may cause unexpected results when modifying PATH .
The PYTHONPATH variable is used past all versions of Python 2 and Python iii, so you should non permanently configure this variable unless it only includes code that is uniform with all of your installed Python versions.
4.6.2. Finding the Python executable¶
Inverse in version 3.5.
Besides using the automatically created start menu entry for the Python interpreter, y'all might desire to get-go Python in the command prompt. The installer has an choice to set that up for you.
On the beginning page of the installer, an choice labelled "Add together Python to PATH" may be selected to have the installer add the install location into the PATH . The location of the Scripts\ binder is likewise added. This allows you to type python to run the interpreter, and pip for the packet installer. Thus, you can besides execute your scripts with control line options, run across Control line documentation.
If yous don't enable this option at install time, you tin can always re-run the installer, select Change, and enable it. Alternatively, you can manually modify the PATH using the directions in Excursus: Setting environment variables. Yous need to fix your PATH environs variable to include the directory of your Python installation, delimited by a semicolon from other entries. An example variable could expect similar this (bold the first two entries already existed):
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.ix 4.seven. UTF-viii mode¶
New in version 3.seven.
Windows nevertheless uses legacy encodings for the system encoding (the ANSI Code Page). Python uses it for the default encoding of text files (e.thousand. locale.getpreferredencoding() ).
This may crusade problems considering UTF-8 is widely used on the cyberspace and nigh Unix systems, including WSL (Windows Subsystem for Linux).
You can use the Python UTF-8 Style to change the default text encoding to UTF-eight. Yous tin can enable the Python UTF-8 Mode via the -X utf8 command line option, or the PYTHONUTF8=1 environment variable. See PYTHONUTF8 for enabling UTF-8 style, and Excursus: Setting environment variables for how to modify surround variables.
When the Python UTF-8 Way is enabled, yous can yet use the system encoding (the ANSI Lawmaking Folio) via the "mbcs" codec.
Note that adding PYTHONUTF8=1 to the default environment variables will affect all Python three.7+ applications on your system. If you have any Python 3.7+ applications which rely on the legacy organization encoding, information technology is recommended to set the environment variable temporarily or use the -X utf8 command line option.
Note
Even when UTF-8 mode is disabled, Python uses UTF-viii past default on Windows for:
-
Console I/O including standard I/O (come across PEP 528 for details).
-
The filesystem encoding (see PEP 529 for details).
4.viii. Python Launcher for Windows¶
New in version 3.3.
The Python launcher for Windows is a utility which aids in locating and executing of different Python versions. It allows scripts (or the command-line) to indicate a preference for a specific Python version, and volition locate and execute that version.
Different the PATH variable, the launcher will correctly select the most advisable version of Python. It will prefer per-user installations over organization-wide ones, and orders by language version rather than using the most recently installed version.
The launcher was originally specified in PEP 397.
4.viii.1. Getting started¶
4.8.1.ane. From the command-line¶
Changed in version 3.6.
System-wide installations of Python three.iii and afterward will put the launcher on your PATH . The launcher is compatible with all available versions of Python, so it does not thing which version is installed. To check that the launcher is available, execute the following command in Control Prompt:
You should find that the latest version of Python you lot have installed is started - it tin can be exited as normal, and any additional command-line arguments specified will be sent directly to Python.
If you have multiple versions of Python installed (e.g., two.seven and 3.x) y'all will have noticed that Python 3.10 was started - to launch Python ii.7, try the control:
If yous want the latest version of Python 2.x you have installed, try the command:
You should find the latest version of Python 2.x starts.
If y'all see the following mistake, you do non accept the launcher installed:
'py' is non recognized equally an internal or external control, operable programme or batch file. Per-user installations of Python do not add the launcher to PATH unless the option was selected on installation.
You should see the currently installed versions of Python.
4.8.1.ii. Virtual environments¶
New in version 3.5.
If the launcher is run with no explicit Python version specification, and a virtual surroundings (created with the standard library venv module or the external virtualenv tool) active, the launcher will run the virtual environs's interpreter rather than the global 1. To run the global interpreter, either deactivate the virtual environs, or explicitly specify the global Python version.
4.eight.1.iii. From a script¶
Let's create a test Python script - create a file called hello.py with the following contents
#! python import sys sys . stdout . write ( "hi from Python %southward \n " % ( sys . version ,)) From the directory in which hello.py lives, execute the control:
You should detect the version number of your latest Python 2.ten installation is printed. Now try changing the showtime line to be:
Re-executing the command should at present print the latest Python 3.x information. As with the above control-line examples, y'all tin specify a more than explicit version qualifier. Bold y'all have Python ii.vi installed, try irresolute the first line to #! python2.half dozen and you should observe the ii.6 version information printed.
Note that unlike interactive use, a bare "python" will use the latest version of Python two.x that you lot have installed. This is for backward compatibility and for compatibility with Unix, where the control python typically refers to Python two.
4.8.1.4. From file associations¶
The launcher should have been associated with Python files (i.e. .py , .pyw , .pyc files) when it was installed. This ways that when you double-click on one of these files from Windows explorer the launcher will be used, and therefore you can apply the same facilities described above to have the script specify the version which should exist used.
The key do good of this is that a single launcher can support multiple Python versions at the same time depending on the contents of the showtime line.
iv.8.2. Shebang Lines¶
If the first line of a script file starts with #! , it is known as a "shebang" line. Linux and other Unix like operating systems have native support for such lines and they are commonly used on such systems to point how a script should be executed. This launcher allows the aforementioned facilities to be used with Python scripts on Windows and the examples in a higher place demonstrate their use.
To let shebang lines in Python scripts to be portable between Unix and Windows, this launcher supports a number of 'virtual' commands to specify which interpreter to use. The supported virtual commands are:
-
/usr/bin/env python -
/usr/bin/python -
/usr/local/bin/python -
python
For example, if the first line of your script starts with
The default Python will be located and used. As many Python scripts written to work on Unix volition already have this line, you should find these scripts can be used by the launcher without modification. If you lot are writing a new script on Windows which yous hope will exist useful on Unix, you lot should utilize one of the shebang lines starting with /usr .
Any of the above virtual commands tin can exist suffixed with an explicit version (either just the major version, or the major and minor version). Furthermore the 32-bit version can be requested by calculation "-32" after the minor version. I.e. /usr/bin/python2.7-32 will asking usage of the 32-bit python 2.7.
New in version 3.7: Beginning with python launcher 3.7 it is possible to request 64-bit version by the "-64" suffix. Furthermore information technology is possible to specify a major and architecture without minor (i.e. /usr/bin/python3-64 ).
The /usr/bin/env form of shebang line has 1 farther special property. Before looking for installed Python interpreters, this grade volition search the executable PATH for a Python executable. This corresponds to the behaviour of the Unix env program, which performs a PATH search.
4.8.3. Arguments in shebang lines¶
The shebang lines can also specify additional options to exist passed to the Python interpreter. For example, if yous have a shebang line:
And then Python will be started with the -v pick
4.8.4. Customization¶
4.viii.4.1. Customization via INI files¶
Two .ini files will be searched by the launcher - py.ini in the current user'due south "awarding information" directory (i.due east. the directory returned by calling the Windows function SHGetFolderPath with CSIDL_LOCAL_APPDATA ) and py.ini in the same directory equally the launcher. The same .ini files are used for both the 'console' version of the launcher (i.e. py.exe) and for the 'windows' version (i.e. pyw.exe).
Customization specified in the "application directory" will have precedence over the ane next to the executable, so a user, who may non have write access to the .ini file side by side to the launcher, can override commands in that global .ini file.
four.8.four.2. Customizing default Python versions¶
In some cases, a version qualifier can be included in a command to dictate which version of Python will be used by the control. A version qualifier starts with a major version number and can optionally be followed by a period ('.') and a minor version specifier. Furthermore it is possible to specify if a 32 or 64 bit implementation shall exist requested past adding "-32" or "-64".
For example, a shebang line of #!python has no version qualifier, while #!python3 has a version qualifier which specifies only a major version.
If no version qualifiers are found in a command, the environment variable PY_PYTHON can be set up to specify the default version qualifier. If it is not set, the default is "3". The variable tin can specify any value that may be passed on the command line, such every bit "3", "3.7", "3.7-32" or "iii.seven-64". (Note that the "-64" choice is merely available with the launcher included with Python iii.7 or newer.)
If no pocket-sized version qualifiers are constitute, the environment variable PY_PYTHON{major} (where {major} is the current major version qualifier equally adamant above) can be set to specify the full version. If no such option is institute, the launcher volition enumerate the installed Python versions and use the latest minor release found for the major version, which is likely, although not guaranteed, to be the virtually recently installed version in that family.
On 64-bit Windows with both 32-bit and 64-bit implementations of the same (major.minor) Python version installed, the 64-bit version will e'er be preferred. This will exist true for both 32-chip and 64-flake implementations of the launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation of the specified version if available. This is so the behavior of the launcher tin be predicted knowing only what versions are installed on the PC and without regard to the order in which they were installed (i.due east., without knowing whether a 32 or 64-bit version of Python and corresponding launcher was installed last). As noted above, an optional "-32" or "-64" suffix can be used on a version specifier to change this behaviour.
Examples:
-
If no relevant options are fix, the commands
pythonandpython2volition employ the latest Python 2.ten version installed and the commandpython3will use the latest Python 3.x installed. -
The commands
python3.1andpython2.7will not consult any options at all as the versions are fully specified. -
If
PY_PYTHON=3, the commandspythonandpython3will both use the latest installed Python three version. -
If
PY_PYTHON=iii.1-32, the controlpythonwill use the 32-bit implementation of 3.1 whereas the commandpython3will use the latest installed Python (PY_PYTHON was not considered at all as a major version was specified.) -
If
PY_PYTHON=iiiandPY_PYTHON3=3.1, the commandspythonandpython3will both utilize specifically 3.i
In add-on to environment variables, the aforementioned settings can exist configured in the .INI file used by the launcher. The section in the INI file is chosen [defaults] and the cardinal name will exist the aforementioned as the environment variables without the leading PY_ prefix (and annotation that the key names in the INI file are case insensitive.) The contents of an surroundings variable will override things specified in the INI file.
For example:
-
Setting
PY_PYTHON=3.aneis equivalent to the INI file containing:
-
Setting
PY_PYTHON=3andPY_PYTHON3=3.1is equivalent to the INI file containing:
[defaults] python = 3 python3 = iii.i four.eight.v. Diagnostics¶
If an environment variable PYLAUNCH_DEBUG is prepare (to any value), the launcher volition impress diagnostic information to stderr (i.eastward. to the console). While this information manages to exist simultaneously verbose and terse, it should allow you to encounter what versions of Python were located, why a particular version was chosen and the exact command-line used to execute the target Python.
4.9. Finding modules¶
Python ordinarily stores its library (and thereby your site-packages binder) in the installation directory. So, if you had installed Python to C:\Python\ , the default library would reside in C:\Python\Lib\ and 3rd-party modules should be stored in C:\Python\Lib\site-packages\ .
To completely override sys.path , create a ._pth file with the same name as the DLL ( python37._pth ) or the executable ( python._pth ) and specify one line for each path to add to sys.path . The file based on the DLL proper noun overrides the one based on the executable, which allows paths to exist restricted for any program loading the runtime if desired.
When the file exists, all registry and environment variables are ignored, isolated fashion is enabled, and site is not imported unless i line in the file specifies import site . Blank paths and lines starting with # are ignored. Each path may be absolute or relative to the location of the file. Import statements other than to site are not permitted, and arbitrary lawmaking cannot be specified.
Notation that .pth files (without leading underscore) will be processed normally past the site module when import site has been specified.
When no ._pth file is found, this is how sys.path is populated on Windows:
-
An empty entry is added at the start, which corresponds to the current directory.
-
If the surroundings variable
PYTHONPATHexists, equally described in Surround variables, its entries are added next. Note that on Windows, paths in this variable must be separated by semicolons, to distinguish them from the colon used in drive identifiers (C:\etc.). -
Additional "application paths" tin can be added in the registry as subkeys of
\SOFTWARE\Python\PythonCore{version}\PythonPathnether both theHKEY_CURRENT_USERandHKEY_LOCAL_MACHINEhives. Subkeys which have semicolon-delimited path strings equally their default value will crusade each path to be added tosys.path. (Notation that all known installers only employ HKLM, so HKCU is typically empty.) -
If the environment variable
PYTHONHOMEis prepare, it is causeless as "Python Home". Otherwise, the path of the chief Python executable is used to locate a "landmark file" (eitherLib\os.pyorpythonXY.zip) to deduce the "Python Dwelling". If a Python dwelling house is constitute, the relevant sub-directories added tosys.path(Lib,plat-win, etc) are based on that folder. Otherwise, the core Python path is constructed from the PythonPath stored in the registry. -
If the Python Habitation cannot exist located, no
PYTHONPATHis specified in the surround, and no registry entries tin can exist found, a default path with relative entries is used (e.g..\Lib;.\plat-win, etc).
If a pyvenv.cfg file is constitute alongside the principal executable or in the directory one level to a higher place the executable, the following variations apply:
-
If
homeis an absolute path andPYTHONHOMEis not set, this path is used instead of the path to the main executable when deducing the habitation location.
The stop result of all this is:
-
When running
python.exe, or any other .exe in the main Python directory (either an installed version, or directly from the PCbuild directory), the core path is deduced, and the core paths in the registry are ignored. Other "application paths" in the registry are always read. -
When Python is hosted in another .exe (different directory, embedded via COM, etc), the "Python Home" will not be deduced, and so the core path from the registry is used. Other "application paths" in the registry are always read.
-
If Python can't detect its home and in that location are no registry value (frozen .exe, some very strange installation setup) yous get a path with some default, merely relative, paths.
For those who want to package Python into their application or distribution, the post-obit communication will prevent conflicts with other installations:
-
Include a
._pthfile alongside your executable containing the directories to include. This will ignore paths listed in the registry and environment variables, and also ignoresiteunlessimport siteis listed. -
If y'all are loading
python3.dllorpython37.dllin your own executable, explicitly callPy_SetPath()or (at least)Py_SetProgramName()beforePy_Initialize(). -
Clear and/or overwrite
PYTHONPATHand setPYTHONHOMEbefore launchingpython.exefrom your application. -
If you cannot apply the previous suggestions (for case, y'all are a distribution that allows people to run
python.exedirectly), ensure that the landmark file (Lib\os.py) exists in your install directory. (Notation that it will non exist detected inside a ZIP file, but a correctly named Cypher file will exist detected instead.)
These will ensure that the files in a organisation-wide installation will non take precedence over the re-create of the standard library bundled with your application. Otherwise, your users may experience bug using your awarding. Note that the beginning suggestion is the best, as the others may nonetheless exist susceptible to not-standard paths in the registry and user site-packages.
Inverse in version iii.6:
Adds
._pthfile support and removesapplocaloption frompyvenv.cfg.Adds
pythonXX.nilas a potential landmark when directly next to the executable.
Deprecated since version three.6:
Modules specified in the registry under
Modules(notPythonPath) may exist imported byimportlib.machinery.WindowsRegistryFinder. This finder is enabled on Windows in 3.half dozen.0 and earlier, but may need to exist explicitly added tosys.meta_pathin the time to come.
iv.x. Additional modules¶
Even though Python aims to exist portable amid all platforms, there are features that are unique to Windows. A couple of modules, both in the standard library and external, and snippets exist to use these features.
The Windows-specific standard modules are documented in MS Windows Specific Services.
4.10.ane. PyWin32¶
The PyWin32 module by Mark Hammond is a collection of modules for advanced Windows-specific support. This includes utilities for:
-
Component Object Model (COM)
-
Win32 API calls
-
Registry
-
Issue log
-
Microsoft Foundation Classes (MFC) user interfaces
PythonWin is a sample MFC application shipped with PyWin32. It is an embeddable IDE with a built-in debugger.
four.10.2. cx_Freeze¶
cx_Freeze is a distutils extension (see Extending Distutils) which wraps Python scripts into executable Windows programs ( * .exe files). When you have washed this, you lot can distribute your application without requiring your users to install Python.
4.10.iii. WConio¶
Since Python's advanced terminal treatment layer, curses , is restricted to Unix-like systems, there is a library exclusive to Windows also: Windows Console I/O for Python.
WConio is a wrapper for Turbo-C's CONIO.H , used to create text user interfaces.
iv.11. Compiling Python on Windows¶
If you want to compile CPython yourself, first thing yous should practise is get the source. You tin download either the latest release's source or just grab a fresh checkout.
The source tree contains a build solution and project files for Microsoft Visual Studio 2015, which is the compiler used to build the official Python releases. These files are in the PCbuild directory.
Check PCbuild/readme.txt for general information on the build process.
For extension modules, consult Building C and C++ Extensions on Windows.
See likewise
- Python + Windows + distutils + SWIG + gcc MinGW
-
or "Creating Python extensions in C/C++ with SWIG and compiling them with MinGW gcc under Windows" or "Installing Python extension with distutils and without Microsoft Visual C++" by Sébastien Sauvage, 2003
4.12. Other Platforms¶
With ongoing evolution of Python, some platforms that used to be supported earlier are no longer supported (due to the lack of users or developers). Check PEP xi for details on all unsupported platforms.
-
Windows CE is yet supported.
-
The Cygwin installer offers to install the Python interpreter as well (cf. Cygwin package source, Maintainer releases)
See Python for Windows for detailed information about platforms with pre-compiled installers.
Source: https://docs.python.org/3/using/windows.html
Posted by: hobbsfamenig.blogspot.com

0 Response to "How To Compile Python In Windows"
Post a Comment