The Python Launcher for UNIX
An implementation of the py command for UNIX-based platforms.
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 is
installed). 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 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)
- Search
PATHforpython3.6
py -3 (loose/major version)
- Use the version found in the
PY_PYTHON3environment variable if defined and not the empty string (e.g.PY_PYTHON3=3.6) - Search
PATHfor all instances ofpython3.Y - Find the executable with largest
Ythat earliest onPATH
py (any/unknown version)
- Use
${VIRTUAL_ENV}/bin/pythonimmediately if available - If the first argument is a file path ...
- Check for a shebang
- If executable starts with
/usr/bin/python,/usr/local/bin/python,/usr/bin/env pythonorpython, proceed based on the version found (barepythonis considered the equivalent of not specifying a Python version)
- Use the version found in the
PY_PYTHONenvironment variable if defined (e.g.PY_PYTHON=3orPY_PYTHON=3.6) - Search
PATHfor all instances ofpythonX.Y - Find the executable with the largest
X.Yearliest onPATH
TODO
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)
Everything in bold is required to hit MVP.
Functionality
- Provide a
python_launcherextension module- It will make the pipenv developers happy
- Might need a rename to
pylauncherorpyfinderto follow Python practices if it isn't too much trouble)
- 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)
- Customized commands?
- Want a better format like TOML?
- Want a way to override/specify things, e.g. wanting a framework build on macOS?
- Aliasing? E.g.
2.7-frameworkfor/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python? - Just provide a way to specify a specific interpreter for a specific version?
E.g.
2.7for/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python - What about implementations that don't install to e.g.
python3.7likepypy3?- Need more than just being able to alias
pypy3to its Python version?
- Need more than just being able to alias
- Aliasing? E.g.
- How should the config file search work?
- Pre-defined locations?
- Walk up from current directory?
- XDG base directory specification?
- Windows support
PATH- Windows Store (should be covered by
PATHsearch, but need to make sure) - Registry
- Replacement for
.venv/bin/python(while keeping thepythonname)- Might need switch off CLI additions -- i.e.
-h,--list, and version specifier support -- in this situation to make this work - Read
../pyvenv.cfgand itshomekey to determine where to look for the Python executable- What if
homehas multiple Python executables installed? Might need to add anexecutablekey to give full path to the creating interpreter.
- What if
- Acts as a heavyweight "symlink" to the Python executable for the virtual environment
- Speeds up environment creation by not having to copy over entire Python installation on Windows (e.g.
.pydfiles) - See/edit the
sitemodule to gain ability to specify virtual environment location (while maintaining the invariant on how to detect virtual environments as outlined in thevenvmodule docs)
- Might need switch off CLI additions -- i.e.
- Automatically detect
.venv/pyvenv.cfgand use that (basically an implicit setting of$VIRTUAL_ENV)?- Also detect
venvdirectories as well? - Is it worth the overhead/perf hit to search all subdirectories for a
pyvenv.cfgand only activate if a single one is found? - Is it worth making the directory that is searched for configurable?
- Have
--venvdo the search for apyvenv.cfgin subdirectories and use that (or maybe search all subdirectories and perform a normal version resolution to use the newest one; not sure if it's worth the hassle of supporting version specification as well)?
- Also detect
- Use
OsString/OsStreverywhere (versus now which is wherever it's easy w/path::Path)?- Widest compatibility for people where they have undecodable paths (which is hopefully a very small minority)
- Massive pain to make work (e.g. cannot easily convert to a
CString)
Polish
- Provide a helpful error message based on requested version when no interpreter found
- Make sure all potential
panic!points are rare enough to be acceptable - Make sure to only be printing to
stderrusingeprintln! - Make sure all error cases have appropriate exit codes and human-readable results
- Have
--listsomehow denote an activated virtual environment?- What does the Windows launcher do in this case?
- Man page?
PYLAUNCH_DEBUG? (Rust logging info)- Distribute binaries