The Python Launcher for UNIX

An implementation of the py command for UNIX-based platforms (with some potential experimentation for good measure 😉)

The goal is to have py become the cross-platform command that all Python users use when executing a Python interpreter. By having a version-agnostic command it side-steps the "what should the python command point to?" debate by clearly specifying that upfront (i.e. the newest version of Python that can be found). This also unifies the suggested command to document for launching Python on both Windows as UNIX as py which has existed as the preferred command on Windows for some time.

See the top section of py --help for instructions.

Search order

Please note that while searching, the search for a Python version can become more specific. This leads to a switch in the search algorithm to the one most appropriate to the specificity of the version.

py -3.6 (specific version)

  1. Search PATH for python3.6

py -3 (loose/major version)

  1. Use the version found in the PY_PYTHON3 environment variable if defined and not the empty string (e.g. PY_PYTHON3=3.6)
  2. Search PATH for all instances of python3.Y
  3. Find the executable with largest Y that earliest on PATH

py (any/unknown version)

  1. Use ${VIRTUAL_ENV}/bin/python immediately if available
  2. If the first argument is a file path ...
    1. Check for a shebang
    2. If executable starts with /usr/bin/python, /usr/local/bin/python, /usr/bin/env python or python, proceed based on the version found (bare python is considered the equivalent of not specifying a Python version)
  3. Use the version found in the PY_PYTHON environment variable if defined (e.g. PY_PYTHON=3 or PY_PYTHON=3.6)
  4. Search PATH for all instances of pythonX.Y
  5. Find the executable with the largest X.Y earliest on PATH

TODO

CI

NOTE: I am using this project to learn Rust, so please don't be offended if I choose to implement something myself instead of accepting a pull request that you submit. (Pull requests to do something I have already implemented in a more idiomatic fashion are very much appreciated, though.)

PEP 397: Python launcher for Windows (documentation; source)

Everything in bold is required to hit MVP.

Functionality

  1. Provide a python_launcher extension module
  2. Windows support
  3. Configuration files (key thing to remember is should not get to the point that you're using this to alias specific interpreters, just making it easier to specify constraints on what kind of interpreter you need and then letting the launcher pick for you)
  4. Replacement for .venv/bin/python (while keeping the python name)?
  5. Automatically detect .venv/pyvenv.cfg and use that (basically an implicit setting of $VIRTUAL_ENV)?
  6. Use OsString/OsStr everywhere (versus now which is wherever it's easy w/ path::Path)?

Polish

  1. Make sure all potential panic! points are rare enough to be acceptable
  2. Have --list somehow denote an activated virtual environment?
  3. Man page?
  4. PYLAUNCH_DEBUG? (Rust logging info)
  5. Distribute binaries

Maintainability

  1. Flesh out documentation
    1. CLI documentation
    2. Flowchart of how the interpreter is selected?
    3. API documentation
  2. Get ~100% unit test coverage
  3. Consider dropping nix for a straight libc dependency (to potentially make Debian packaging easier)

Output from py --list on Windows: Installed Pythons found by C:\WINDOWS\py.exe Launcher for Windows -3.8-64 * -3.7-64 -3.6-64 -2.7-64 -2.7-64