How to run multiple Python versions on Windows
Install each Python version from the official site. Avoid adding to PATH to prevent hiccups. Use the py launcher to specifiy a version:
In your Python files, use a shebang for the preferred version:
Construct virtual environments for specific projects and Python versions:
Switch versions using pyenv-win
or manually update PATH in your command line.
The PATHway to Success
The PATH environment variable is your torchlight in the dark world of multiple Python versions. Ensure there's no global interference. You should be setting the PATH locally within each command console using the Python version of your choice:
Salute batch files for automating environment preparation, acting like your loyal minions. A sample minion (aka batch file setpython38.bat
):
For Encapsulation: Virtual Environment
Virtualenv is your personal assistant for managing project dependencies. It boots and shuts down as per your orders:
In command of the correct Python version for your project? Simply activate your environment:
When it's time to switch jobs or Python versions, use the deactivate
command.
Get a Personalized Python Experience
Personalize your command for Python. Configuring the PATHEXT environment variable allows for simple Python file execution. You no longer need to type '.exe'. Just use the script name:
The 'py' launcher has got your back! It adheres to the Python version declared in a script’s shebang line:
And yes, you can still use symbolic links for quick access to different versions. Isn't that awesome!
In the 'py' Launcher We Trust
The 'py' launcher for Windows, as bestowed by PEP 397, is your trusted general for version warfare. Use the command-line switches to specify the version:
Brushing up on PEP 397 guidelines will polish your axes for the Python version battles on Windows.
Project-Specific Configurations to the Rescue
Leverage .bat or .cmd files to encapsulate your environment settings. Your Python version and script execution are no longer global matters, but project-specific protocols:
Double-click the batch file to run your Python delights with all the right environment condiments ("Double, double toil and trouble; Batch file run, and codebase bubble" 😉).
Was this article helpful?