Introduction
Managing multiple versions of Python on a single machine can be crucial, especially when working on different projects requiring specific Python versions. Understanding how to set and manage default Python versions on Windows is essential for developers looking to streamline their workflow. This tutorial will guide you through setting up your environment to use the desired Python version as the default.
Understanding the Environment
When multiple versions of Python are installed, Windows uses an executable called py.exe
(Python Launcher for Windows) to determine which version should be used when executing scripts. This launcher allows different script files to specify which Python version they require via a "shebang" line at the top of the file or by using command-line switches.
Setting Up Your Environment
-
Install Python Versions: Ensure that you have installed all necessary versions of Python on your system. You can download them from the official Python website.
-
Configure PATH Variable:
- Open System Properties by right-clicking on ‘This PC’ and selecting Properties, then click on Advanced System Settings.
- Navigate to Environment Variables. Under System variables, find the
Path
variable and edit it. - Move the directory of your preferred Python version to the top of this list to ensure it is found first when executing Python commands.
-
Use Python Launcher (py.exe):
- The Python Launcher (
py.exe
) handles script execution based on specified or default settings. - To check if
py.exe
is associated correctly, open a command prompt and execute:assoc .py ftype Python.File
- Ensure that
.py
files are linked toPython.File
, which should map to the path ofpy.exe
.
- The Python Launcher (
-
Setting Default Python Version:
- You can set a default Python version by creating an environment variable named
PY_PYTHON
. For example, setting it to3
will make Python 3.x the default.setx PY_PYTHON 3
- Use command-line switches with
py.exe
for specific needs:py -3 script.py # Uses the latest installed version of Python 3.x py -3.6 script.py # Specifically uses Python version 3.6 py -2 script.py # Forces using a version of Python 2.x
- You can set a default Python version by creating an environment variable named
-
Adjust File Associations:
- To simplify running scripts, set file associations so that you don’t need to prefix commands with
py
. Use an admin command prompt and execute:ftype Python.File="C:\Windows\py.exe" "%L" %* ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
- Add
.py
to the PATHEXT environment variable so you can run scripts without specifying their extension.
- To simplify running scripts, set file associations so that you don’t need to prefix commands with
-
Shebang Line:
- In your Python scripts, include a shebang line at the top to specify which version should execute them:
#!/usr/bin/env python3
- In your Python scripts, include a shebang line at the top to specify which version should execute them:
Best Practices
- Always use
py
instead ofpython
when running scripts from the command line. - Regularly check installed versions with
py -0
. - Consider using virtual environments for project-specific dependencies and Python version requirements. The launcher is aware of these, allowing further customization.
Conclusion
By configuring your system as described, you can manage multiple Python versions effectively on Windows. This setup enhances flexibility, ensuring that the right Python version runs smoothly across different projects.