Hi 👋
In this short article I will show you two ways of changing Python versions on Windows. It is useful when you have installed multiple Python versions on your system and want to run a specific version from the terminal.
For example, if we have the following versions installed:
We can use either the Python Launcher py
to run Python or the python
command.
Python Launcher
To list installed Python versions with Python launcher we can use the py -0
command.
|
|
The default version has a star next to it. If we run a simple py command, we’ll get a prompt to Python 3.10. To change the default version all we need to do is to set the environment variable PY_PYTHON to the desired version.
|
|
Using the Python command
If you prefer running Python using the full command then you’ll get the Python version which has higher precedence in your path, for example if I run python
on my machine I will get:
|
|
We can change the order by going to: My PC -> Advanced System Settings -> Environment Variables
Select path from User variables and click Edit…
Python 3.10 has higher precedence in path because it is above Python 3.7. If we want to change the order, we need to select the folders referencing Python37 and click Move Up until they are above Python 3.10
Restarting your terminal and running python
again should run your desired Python version.
Thanks for reading! 🍻