Created by: gwideman, Oct 3, 2013 2:10 am
Revised by: gwideman, Oct 7, 2014 6:29 pm (6 revisions)

Very incomplete


This page is concerned with how a particular Python environment comes to be the one invoked, under the numerous scenarios in which either python is commanded to run, or a python script is invoked. For now, I'm just accumulating a list of all the facets to this problem. And focused on Windows primarily.
By "Python environment" I am including:
  • Which version of Python interpreter gets invoked
  • Which global variables are made available to that interpreter
    • Especially including sys.path, and its particular contents
  • Which modules are accessible
    • Which concerns sys.path, but also other factors
There are essentially two major pieces to the apparatus that gets us to a running and initialized python environment:
  • The apparatus that selects which Python interpreter gets invoked (with its particular python installation)
    • The Windows apparatus for finding an executable upon being commanded in some way to invoke python
    • Any additional Python-supplied apparatus for intervening in this process. (Eg: Python launcher).
  • The apparatus that Python uses to establish which libraries and behaviors will be available to the launched Python session.

Enumeration of things that matter (or might do)

Launching Python

Mode of launch

In environment
Invocation method
No Launcher
With Launcher
Command-line shell
python []

py []


pipe to or from (affects byte/unicode setting of stdio)

Windows Explorer, double-click


shortcut (could include args)

Another executable
Invoke process involving either a *.py/*.pyw file,
or invoking py.exe or python explicitly

Within an IDE
Run with or without debugger

Environment variables


The list of directories in which Windows looks for executables

Fromt he command line, allows '' to be treated as a command 'something'. The list of file name extensions that Windows will treat as 'executables'. When '.py' is listed in PATHEXT, and a user issues a command 'something', Windows will look to see if there's a file '' in the PATH, and if so, look in the file associations list to find an executable to process files having a '.py' extension (in this case python.exe or py.exe, depending on version of Python installed.)

Registry key

(Environment variables are stored in registry)
(OK, but I covered those above)

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths

Not present in my installation, presumably superseded by py.exe, also not present. Switching python versions on windows

File associations (.py --> application etc)
HKCR\.py --> Python.File
HKCR\Python.File\shell\open\command -->
"C:\Program Files (x86)\Python Launcher\py.exe" "%1" %*
Similar for .pyc, .pyw

But no such key on my machine.

Windows command search algorithm

How Windows console treats a user-typed command: MSDN: Application Registration
  • If the command includes an explicit path, use that.
Otherwise, look for an 'executable' file matching the command name. Try with each file name extension listed in PATHEXT. Look in the following places, in the order shown:
  • The current working directory.
  • The Windows directory only (no subdirectories are searched yet).
  • The Windows\System32 directory.
  • Directories listed in the PATH environment variable.
  • Recommended: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
    • Really? This is lowest priority?
Doesn't mention how this interacts with HKCU\Applications. Maybe this search algorithm only applies when no explicit path is available from the launch method.

Python Launcher

Python Launcher comes into play under some scenarios, which are:
  • To do
Other notes:
  • Python Launcher (py.exe) is itself found by Windows command search algorithm
  • Python Launcher uses configuration data from
    • Environment variables
    • Registry entries?
    • py.ini files

Python Virtual Environments

  • Any special wrinkles?
  • "Activating" a virtual environment prepends it to PATH (only for the current session)
  • If you manually prepend the virtual environment to PATH, this affects only some launch scenarios
    • Interaction with Python Launcher:
      • To do

Establishing the Python environment

Once launched, what factors influence what is visible within the python environment?
  • Command line params to python.exe
  • The particular Python directory tree surrounding the launched python.exe

Accessibility of modules

  • sys.path.
    • Module loading is a topic in its own right: Organization for common modules
    • What contributes to sys.path (or items reachable via sys.path)?
      • "site-package" directories established in
        • *.pth files
      • Environment variables: PYTHONPATH [sp?]

  • Accessibility of non-python functionality
    • Stand-alone Executables: All the usual for Windows executable search algorithm
    • DLLs: ???