This document aims to give an overview of Windows-specific behaviour you shouldknow about when using Python on Microsoft Windows.
On my mac I am trying to make a Python script open by default in the Python Launcher. I left click on the script and select 'open with' and select the Launcher. Every-time I do this Terminal pops up, the Launcher window appears briefly. Then a messages pops up in a window: 'Python Launcher can only run certain scripts try another.' Something along those line (the message has stop popping up. Py2app is a Python setuptools command which will allow you to make standalone Mac OS X application bundles and plugins from Python scripts. Py2app is similar in purpose and design to py2exe for Windows. NOTE: py2app must be used on OSX to build applications, it cannot create Mac applications on other platforms.
Unlike most Unix systems and services, Windows does not include a systemsupported installation of Python. To make Python available, the CPython teamhas compiled Windows installers (MSI packages) with every release for many years. These installersare primarily intended to add a per-user installation of Python, with thecore interpreter and library being used by a single user. The installer is alsoable to install for all users of a single machine, and a separate ZIP file isavailable for application-local distributions.
As specified in PEP 11, a Python release only supports a Windows platformwhile Microsoft considers the platform under extended support. This means thatPython 3.9 supports Windows Vista and newer. If you require Windows XPsupport then please install Python 3.4.
There are a number of different installers available for Windows, each withcertain benefits and downsides.
The full installer contains all components and is the best option fordevelopers using Python for any kind of project.
The Microsoft Store package is a simple installation of Python that is suitable forrunning scripts and packages, and using IDLE or other development environments.It requires Windows 10, but can be safely installed without corrupting otherprograms. It also provides many convenient commands for launching Python andits tools.
The nuget.org packages are lightweight installations intended for continuousintegration systems. It can be 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 forembedding into a larger application.
3.1. The full installer¶3.1.1. Installation steps¶
Four Python 3.9 installers are available for download - two each for the32-bit and 64-bit versions of the interpreter. The web installer is a smallinitial download, and it will automatically download the required components asnecessary. The offline installer includes the components necessary for adefault installation and only requires an internet connection for optionalfeatures. See Installing Without Downloading for other ways to avoid downloadingduring installation.
After starting the installer, one of two options may be selected:
If you select “Install Now”:
Selecting “Customize installation” will allow you to select the features toinstall, the installation location and other options or post-install actions.To install debugging symbols or binaries, you will need to use this option.
To perform an all-users installation, you should select “Customizeinstallation”. In this case:
3.1.2. Removing the MAX_PATH Limitation¶
Windows historically has limited path lengths to 260 characters. This meant thatpaths longer than this would not resolve and errors would result.
In the latest versions of Windows, this limitation can be expanded toapproximately 32,000 characters. Your administrator will need to activate the“Enable Win32 long paths” group policy, or set
LongPathsEnabled to 1 in the registry keyHKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlFileSystem .
This allows the
open() function, the os module and most otherpath functionality to accept and return paths longer than 260 characters.
After changing the above option, no further configuration is required.
Changed in version 3.6: Support for long paths was enabled in Python.
3.1.3. Installing Without UI¶
All of the options available in the installer UI can also be specified from thecommand line, allowing scripted installers to replicate an installation on manymachines without user interaction. These options may also be set withoutsuppressing the UI in order to change some of the defaults.
To completely hide the installer UI and install Python silently, pass the
/quiet option. To skip past the user interaction but still displayprogress and errors, pass the /passive option. The /uninstall option may be passed to immediately begin removing Python - no prompt will bedisplayed.
All other options are passed as
name=value , where the value is usually0 to disable a feature, 1 to enable a feature, or a path. The full listof available options is shown below.
For example, to silently install a default, system-wide Python installation,you could use the following command (from an elevated command prompt):
To allow users to easily install a personal copy of Python without the testsuite, you could provide a shortcut with the following command. This willdisplay a simplified initial page and disallow customization:
(Note that omitting the launcher also omits file associations, and is onlyrecommended for per-user installs when there is also a system-wide installationthat included the launcher.)
The options listed above can also 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, it will be converted to a number ifpossible. Values provided as element text are always left as strings. Thisexample file sets the same options as the previous example:
3.1.4. 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 thisneed, all possible components may be downloaded on-demand to create a completelayout that will no longer require an internet connection regardless of theselected features. Note that this download may be bigger than required, butwhere a large number of installations are going to be performed it is veryuseful to have a locally cached copy.
Execute the following command from Command Prompt to download all possiblerequired files. Remember to substitute
python-3.9.0.exe for the actualname of your installer, and to create layouts in their own directories toavoid collisions between files with the same name.
Mac Python Launcher App Download
You may also specify the
/quiet option to hide the progress display.
3.1.5. Modifying an install¶
Once Python has been installed, you can add or remove features through thePrograms and Features tool that is part of Windows. Select the Python entry andchoose “Uninstall/Change” to open the installer in maintenance mode.
“Modify” allows you to add or remove features by modifying the checkboxes -unchanged checkboxes will not install or remove anything. Some options cannot bechanged in this mode, such as the install directory; to modify these, you willneed to remove and then reinstall Python completely.
“Repair” will verify all the files that should be installed using the currentsettings and replace any that have been removed or modified.
“Uninstall” will remove Python entirely, with the exception of thePython Launcher for Windows, which has its own entry in Programs and Features.
3.2. The Microsoft Store package¶
The Microsoft Store package is an easily installable Python interpreter thatis intended mainly for interactive use, for example, by students.
To install the package, ensure you have the latest Windows 10 updates andsearch the Microsoft Store app for “Python 3.9”. Ensure that the appyou select is published by the Python Software Foundation, and install it.
Warning
Python will always be available for free on the Microsoft Store. If youare asked to pay for it, you have not selected the correct package.
After installation, Python may be launched by finding it in Start.Alternatively, it will be available from any Command Prompt or PowerShellsession by typing
python . Further, pip and IDLE may be used by typingpip or idle . IDLE can also be found in Start.
All three commands are also available with version number suffixes, forexample, as
python3.exe and python3.x.exe as well aspython.exe (where 3.x is the specific version you want to launch,such as 3.9). Open “Manage App Execution Aliases” through Start toselect which version of Python is associated with each command. It isrecommended to make sure that pip and idle are consistent withwhichever version of python is selected.
Virtual environments can be created with
python-mvenv and activatedand used as normal.
If you have installed another version of Python and added it to your
PATH variable, it will be available as python.exe rather than theone from the Microsoft Store. To access the new installation, usepython3.exe or python3.x.exe .
The
py.exe launcher will detect this Python installation, but will preferinstallations from the traditional installer.
To remove Python, open Settings and use Apps and Features, or else findPython in Start and right-click to select Uninstall. Uninstalling willremove all packages you installed directly into this Python installation, butwill not remove any virtual environments
3.2.1. Known Issues¶
Because of restrictions on Microsoft Store apps, Python scripts may not havefull write access to shared locations such as
TEMP and the registry.Instead, it will write to a private copy. If your scripts must modify theshared locations, you will need to install the full installer.
3.3. The nuget.org packages¶
The nuget.org package is a reduced size Python environment intended for use oncontinuous integration and build systems that do not have a system-wideinstall of Python. While nuget is “the package manager for .NET”, it also worksperfectly fine for packages containing build-time tools.
Visit nuget.org for the most up-to-date informationon using nuget. What follows is a summary that is sufficient for Pythondevelopers.
The
nuget.exe command line tool may be downloaded directly fromhttps://aka.ms/nugetclidl , for example, using curl or PowerShell. With thetool, the latest version of Python for 64-bit or 32-bit machines is installedusing:
To select a particular version, add a
-Version3.x.y . The output directorymay be changed from . , and the package will be installed into asubdirectory. By default, the subdirectory is named the same as the package,and without the -ExcludeVersion option this name will include the specificversion installed. Inside the subdirectory is a tools directory thatcontains the Python installation:
In general, nuget packages are not upgradeable, and newer versions should beinstalled side-by-side and referenced using the full path. Alternatively,delete the package directory manually and install it again. Many CI systemswill do this automatically if they do not preserve files between builds.
Alongside the
tools directory is a buildnative directory. Thiscontains a MSBuild properties file python.props that can be used in aC++ project to reference the Python install. Including the settings willautomatically use the headers and import libraries in your build.
The package information pages on nuget.org arewww.nuget.org/packages/pythonfor the 64-bit version and www.nuget.org/packages/pythonx86 for the 32-bit version.
3.4. The embeddable package¶
The embedded distribution is a ZIP file containing a minimal Python environment.It is intended for acting as part of another application, rather than beingdirectly accessed by end-users.
When extracted, the embedded distribution is (almost) fully isolated from theuser’s system, including environment variables, system registry settings, andinstalled packages. The standard library is included as pre-compiled andoptimized
.pyc files in a ZIP, and python3.dll , python37.dll ,python.exe and pythonw.exe are all provided. Tcl/tk (including alldependants, such as Idle), pip and the Python documentation are not included.
Note
The embedded distribution does not include the Microsoft C Runtime and it isthe responsibility of the application installer to provide this. Theruntime may have already been installed on a user’s system previously orautomatically via Windows Update, and can be detected by finding
ucrtbase.dll in the system directory.
Third-party packages should be installed by the application installer alongsidethe embedded distribution. Using pip to manage dependencies as for a regularPython installation is not supported with this distribution, though with somecare it may be possible to include and use pip for automatic updates. Ingeneral, third-party packages should be treated as part of the application(“vendoring”) so that the developer can ensure compatibility with newerversions before providing updates to users.
The two recommended use cases for this distribution are described below.
3.4.1. Python Application¶
An application written in Python does not necessarily require users to be awareof that fact. The embedded distribution may be used in this case to include aprivate version of Python in an install package. Depending on how transparent itshould be (or conversely, how professional it should appear), there are twooptions.
Using a specialized executable as a launcher requires some coding, but providesthe most transparent experience for users. With a customized launcher, there areno obvious indications that the program is running on Python: icons can becustomized, company and version information can be specified, and fileassociations behave properly. In most cases, a custom launcher should simply beable to call
Py_Main with a hard-coded command line.
The simpler approach is to provide a batch file or generated shortcut thatdirectly calls the
python.exe or pythonw.exe with the requiredcommand-line arguments. In this case, the application will appear to be Pythonand not its actual name, and users may have trouble distinguishing it from otherrunning Python processes or file associations.
With the latter approach, packages should be installed as directories alongsidethe Python executable to ensure they are available on the path. With thespecialized launcher, packages can be located in other locations as there is anopportunity to specify the search path before launching the application.
3.4.2. Embedding Python¶
Applications written in native code often require some form of scriptinglanguage, and the embedded Python distribution can be used for this purpose. Ingeneral, the majority of the application is in native code, and some part willeither invoke
python.exe or directly use python3.dll . For either case,extracting the embedded distribution to a subdirectory of the applicationinstallation is sufficient to provide a loadable Python interpreter.
As with the application use, packages can be installed to any location as thereis an opportunity to specify search paths before initializing the interpreter.Otherwise, there is no fundamental differences between using the embeddeddistribution and a regular installation.
3.5. Alternative bundles¶
Besides the standard CPython distribution, there are modified packages includingadditional functionality. The following is a list of popular versions and theirkey features:
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |