How Python Launcher Simplifies Multi‑Version Python Execution on Windows and Linux
This article explains the problem of selecting the correct Python interpreter for scripts, introduces the Python Launcher introduced in PEP 397, and provides step‑by‑step instructions for installing and using it on both Linux and Windows to run scripts with the desired Python version.
1. Problem Statement
You may have encountered the issue where the python command runs an unexpected interpreter version, especially when a script requires a specific Python 2.x interpreter while the default python points to Python 3.x. python test.py Because the system resolves python via the PATH, the interpreter that appears first in PATH is used, which can lead to running the wrong version.
In Linux
On Unix‑like systems, a symbolic link can bind a generic python command to a concrete interpreter path. The shebang line (e.g., #!/usr/bin/python2) then causes the script to invoke the linked interpreter.
#!/usr/bin/python2In Windows
Windows faces the same issue, and the solution is provided by the Python Launcher introduced in PEP 397 and shipped with Python 3.3 and later.
2. Solution
The Python Launcher ( py.exe for console, pyw.exe for GUI) can locate and execute the appropriate Python version based on the script’s shebang or command‑line options.
How to Install Python Launcher
Starting with Python 3.3, the launcher is an optional component during installation. Selecting it adds py.exe to the system PATH. It needs to be installed only once and works with all later Python versions.
How to Use Python Launcher
After installation, you can run py from any directory. By default, it launches the newest installed interpreter. If multiple versions exist, you can specify the desired one:
py -2 test.py # use the latest Python 2.x py -3.7 test.py # use Python 3.7Shebang lines also guide the launcher. For example, a script starting with #! python2.7 will be executed with Python 2.7, while #! python3 selects the latest Python 3.x.
#! python2.7
import sys
print(sys.version)Running py test.py will automatically invoke the interpreter indicated by the shebang. If no shebang is present, the launcher defaults to the newest version, but you can override it with -2, -3, or a specific version number.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
