Work with multiple python versions on Windows 10
Welcome everybody! I believe every beginning software developer has experienced, how frustrating it can be to use several releases of python on one computer.
In this article I will show you
- how to install several python versions
- how to access several python versions via the terminal
- best practices i.e. using virtual environments for python projects
Install multiple python versions
To get started, head over to the offcial python website.
Lets suppose you want to install the releases 2.5, 3.5 and 3.9.
For Windows users, I recommend using the Windows x86-64 executable installer
option if you work on a 64bit system. Otherwise, just use the Windows x86 executable installer
.
After locating the install option for the specific version, just press the download link. After the executable file has been downloaded, you should be able to launch the setup tool and start the installation by clicking on Install Now
.
Make sure you tick the checkbox Add Python x.x to PATH in order to prevent you from future trouble!
That wasn’t so bad, was it?! 😄
Now comes the second part of this article, which is the actual usage of different versions from the command prompt (CMD).
Overall, there are two ways to do so
1. you can use the python launcher
py
2. the easier option is the
python
command
Let’s take a look at both methods :)
Using the python launcher (py)
First off, you can check which versions you have installed by using the following command:
C:\Users> py --list
Installed Pythons found by py Launcher for Windows
-3.9-64 *
-3.5-64
-2.7-64
As expected, we properly installed releases 2.5, 3.5 and 3.9.
Lets suppose you want to use version 3.9. To start the interpreter you can write py -3.9
(and exit it via the exit()
command):
C:\Users> py -3.9
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
However, most of the time, you probably like to use some command like pip
. To do this, you can use the following sheme py -<version> -m <command>
, e.g. py -3.9 -m pip freeze
. Simple as that!
Python and python3 command
The py-launcher command sheme often is too ugly, so there is the python2
and python3
command.
The command structure is very straightforward: python <command>
On my computer the following default versions are set
C:\Users> python --version
Python 2.7.18C:\Users>python3 --version
Python 3.9.1
In order to change these default aliases on Windows, you have two options.
- If you are using the Windows PowerShell
Set-Alias -Name <new_command_name> -Value python3
2. If you are using the Command Prompt (CMD)
doskey <new_command_name> = <old_command_name>
Working with virtual environments
When working on several projects simultaneously with different releases of python or python libraries, it is super useful and good practice, not to install all versions on your “real” system, but rather on a virtual system. The so called virtual environment.
A virtual environment simulates a real computer environment, but does not affect your real environment directly.
Setup a virtual environment with a specific python version
Most of the times you want to work with one python version in one virtual environment. Firstly, you have to install the virtual environment support for this specific python release using the following commands, e.g. for version 3.5
C:\Users> py -3.5 -m pip install virtualenv
In order to create a new environment (in this case named venv
) which uses a specific python version, e.g. 3.5 do
C:\Users> py -3.5 -m virtualenv venv
The next step in configuring a virtual environment is to actually activate it. On Windows this is done by executing the following script (notice, that the name of your folder can differ, depending on the name you gave the virtualenv on creation):
C:\Users> venv\Scripts\activate
(venv) C:\Users> # youre virtualenv is now ready to use
You should now see (venv)
in the command line which indicates, that the environment is activated. If this does not work from the command line, you can use windows explorer to find the venv\Scripts\activate
location.
The nice thing about this virtualenv is, that you can now use the python
command and do not have to specify a version. E.g. in the above virtualenv you can see this by using the version
command in the activated virtualenv:
(venv) C:\Users> python --version
Python 3.5.2
Conclusion
For a beginner programmer who is just learning python, this can be an intimidating journey but once you get the gist of using multiple releases of python on one system, youre world will get much more exciting!
So have fun coding 💻 ☕️ 😃
In case you liked it or are feeling particularly awesome today, I’d appreciate if you would …
However you decide, have a great day :)