Site Contents
Wikispaces topics
Wikispaces topics
- Principles for Addons
- Documentation for Addons
- Addon components on this site
- Utility functions
- SiteTOC
- Remove Addon BRs
- Customizations & Fixes
- Notes about Wikispaces
- Tests
- WS features Test
- CSS
- Javascript
- Misc
- Collaboration/CMSs
- Notes on features
- Drupal investigations
- Drupal docs study
- Software development
- Fonts
- Java
- JavaScript/CSS
- Trees and TreeGrids
- JavaScript frameworks
- PHP
- Python
- Organization for common modules
- Packaging and deployment examined
- Packaging and deployment notes
- Distutils, setuptools, pip sleuthing
- Environment and Launcher
- Python features and libraries
- Miscellaneous
- Arduino and AVR
- Delphi
- Matlab
- ANTLR
- Process management
- Microsoft Access
- Linux
- Visio
- Economics
- "Making"
- Laser cutter notes
- Motor notes
- DIY Bio
- Includes
- For pages
- zhtml_pageinfo
- util_authordate (deprec)
- Test
- For pages
- Admin
- Addon Components, V.old
- Left-overs
Table of Contents
Revised by: gwideman, Oct 17, 2013 4:34 pm (30 revisions)
2013-10-17: Note: Work in progress -- or more like in a shambles
Overview
This page collects my notes covering understanding what happens when a user launches a Python script, particularly on a machine where multiple versions of Python are installed or available.Multiple Python installations are a frequent occurrence, primarily because the Python language of the 3.x series of interpreters is not fully compatible with the language of the 2.x series. Despite the improvements of the 3.x series, scripts and libraries based on Python 2.x are still in wide use, so a Python user often needs their machine equipped for both. Additionally, features changed significantly along the progression of 2.x versions, and 3.x versions, again suggesting the need for more installations. Yet more installations may be occasioned in order to install different sets of libraries which are either incompatible, or to reduce complexity of any one installation.
All of this precipitates the need to manage which of multiple installations of Python gets invoked for a particular script.
Topics include handling the different user actions which can invoke a Python script and determining the environment the script will see once running. To do this we need to understand the Windows and Python mechanisms which affect these. In outline:
User actions which invoke a script
The environment within which the script operates
The mechanisms which affect the above issues
Related issues
Background
The central task: launching a Python environment that suits a particular script...
The central launch task is to ensure that the invoked Python environment does two things:- understands the version of the Python language (version X.Y) that the script makes use of. A script typically needs a Python interpreter that is:
- definitely within the same X family, either the 2 family, or the 3 family.
- often a version number greater or equal to particular X.Y. For example, '2.6 or above', or '3.2 or above'. an exact version X.Y: "must be 3.3".
- occasionally a version number greater than X.Y.Z: 'Must have features of 3.2, with the bug fix that was applied at 3.2.2'.
- occasionally an exact X.Y version, like 'exactly 3.2', perhaps because that's what the developers tested the script with.
- almost never an exact X.Y.Z, except perhaps as a script developer testing the script with a very specific version of Python.
- includes or provides access to the libraries (packages or other modules) that the script makes use of.
... and to do these two things in the face of Python updates, and possibly replacement, as time goes by. This is the classic problem of "program vs library versions" familiar in many operating system and language environments -- famously "dependency hell" or "DLL hell"....is not entirely solved
The short story is that Python does not have a mechanism that completely solves this set of problems. Instead, there are a number of different mechanisms in play, each of which addresses part of the problem, sometimes in conflicting ways, and they do not add up to a complete solution. Instead they add up to a requirement that developers, and to an extent users, have to encounter a considerable amount of superfluous complexity, just to get scripts launched reliably.Key Problems
The key constraining features are:The Python Launcher for Windows
The Python Launcher for Windows is a feature which gets installed with Python starting with version 3.3. It implements several previously unavailable ways to control the launch of Python, most notably, the ability to understand "shebang" lines. Once installed, Launcher intervenes in the launch for all versions of Python, including pre-3.3 and 2.x series (but not for absolutely all methods of launch). So in what follows, a distinction must be made between what happens on a system in which Launcher has been installed, versus a system on which it is absent.- - - - - - - - - -
Entanglement of Python version with Python environment
A Python installation includes a directory tree containing the Python components. This tree also contains a directory ([python_installation]\lib\site-packages\) , which is the most convenient location to place additional packages and modules which can be used by scripts running with this installation of Python.The mechanism which makes packages and modules available to your scripts is the Python variable sys.path. Here are examples of sys.path, as it appears for a couple of different Python installations.
C:\Windows\system32\python27.zip
C:\Python\27\DLLs
C:\Python\27\lib
C:\Python\27\lib\plat-win
C:\Python\27\lib\lib-tk
C:\Python\27
C:\Python\27\lib\site-packages
C:\Windows\system32\python33.zip
C:\Python\33\DLLs
C:\Python\33\lib
C:\Python\33
C:\Python\33\lib\site-packages
The inclusion of the site-packages directory within the Python directory structure is what organizes additional packages per installation. The inclusion of that path in the default sys.path list is what makes additional installed packages available to your scripts when running that particular Python.
Additional package directories (eg., outside of the Python installation) can be added to sys.path, either by setting PYTHONPATH environment variable before launching Python, or by the script itself adding items to sys.path (obviously before calling those libraries). However, the sys.path shown above is the default and thus most convenient to work with, absent any strong reason not to.
The entanglement has awkward implications
The main implication of this entanglement is that choosing to invoke the python.exe in a particular Python installation also selects the particular set of packages and modules that the script will be able to access conveniently. So, when multiple Python X.Y versions are installed, the following concerns arise:In context
I will shortly address all the means available to deal with these challenges. However, it is worth first noting the baseline: at worst, it is always possible to issue a command line command which explicitly selects a particular python.exe to run (having a particular X.Y language version, and its surrounding directory tree and installed packages), like this:If your script needs access to additional packages or modules, then you can set PYTHONPATH and any other Python environment variables you need, like:
And of course these could be placed in a batch file for convenience. (Though this in turn raises a number of questions).
So for the most part, the remaining discussion is about how to get that effect automatically, and for a variety of ways of invoking a script, without you (or your users) having to know these details (or having to type them in or set them up).
=============================
Notes
Plain vs "w"
For each python install, there is a python.exe variant named pythonw.exe. The "w" version suppresses launching a shell window when it starts up. This is useful when a program wants to launch python as a subsidiary process and not have a shell window visibly flash onto the screen. However, it is only suitable or scripts which don't want to accept input from, or direct output to, a command shell. Scripts designed to operate without a shell window can be given the filename extension ".pyw". On a computer with a normal python installation, such a script will cause Windows to invoke Launcher pyw.exe instead of py.exe, or pythonw.exe instead of python.exe. The descriptions on this page focus primarily on the plain py.exe and python.exe variant, with the "w" variant working the same way except for the shell window suppression.Mode of launch
Linux: /full/path/to/python myscript.py
Invokes the python at that location
Shell finds python interpreter using PATH (or possibly ...\App Paths registry key)
.
py -3 myscript.py
OR -- does py look at the PYTHON2 or PYTHON3 variable as part of the process?
or invoking py.exe or python explicitly
Shebang line processing
In a Python script, the first line may be a "shebang line". After the initial "#!", the shebang line may contain:(!# omitted for brevity)
Note that shell never searches PATH for a target for the command, so the command must be an explicit path to an executable or link.
/usr/local/bin/python
(Does Launcher skip even checking if these are valid paths?)
Launcher interprets these as request for a version of python to be selected according to Launcher's configuration and algorithm
/usr/bin/python3.2
/usr/bin/python3.2.1
Launcher configuration settings
The launcher gets configuration from the following files and environment variables, in order of decreasing precedence: This allows users to override global settings, and shell scripts which launch other python scripts to set environment variables for ad hoc selection of a particular python version or installation.To be determined from source code:
Launcher INI file format and environment variables
Launcher inspect two py.ini files:- Global py.ini in the same directory as py.exe; typically C:\Windows\py.ini
- User py.ini in user's %LOCALAPPDATA% directory; typically C:\Users\AppData\Local\py.ini.
The user py.ini file overrides the global py.ini file. (Is this override on a per-file, or per-variable basis?).In addition, Launcher recognizes environment variables whose names parallel some of the possible keys in the [defaults] section of the INI file. (And no environment variables corresponding to the [commands] section?) The possible lines in the ini files, and corresponding environment variables, are detailed below.
py.ini format
(Can the [commands] section define the actual path for non-custom virtual commands?)The launcher executables py.exe and pyw.exe both read the same set of optional ini files and optional environment variables.
[defaults] section of py.ini
NOTE: With no ini or environment variables, py.exe behaves as though PY_PYTHON=2, even though python 3.something is installed.
python=X.Y.Z
python=3.2.1
set PY_PYTHON=3.2.1
pythonX=X.Y.Z
python3=3.2.1
set PY_PYTHON3=3.2.1
[...]
pythonX.Y=X.Y.Z-bb
(bb = 32 or 64)
[commands] section of ini
This section defines shebang-line virtual commands, commands which Launcher will recognize in the shebang line of a script. Each definition's key part defines the command name. Each definition's value part defines the OS command which Launcher should execute, so this is typically a path to a python.exe, along with associated flags, if any.py.exe python version search mechanism
Interaction with Python virtual environments
What Installers do (what makes a script "installed")
Conclusions
References