How To Manage Multiple Versions Of Python On Windows 11

Introduction

In this article, I will discuss how we can install and manage multiple Python versions on Windows 11.

Python

Python is an interpreted, high-level, general-purpose programming language created by Guido van Rossum and first released in 1991. He started Python as a hobby project to keep him occupied in the week around Christmas. It got its name from the name of the British comedy troupe Monty Python. It is used in:

  1. Software Development
  2. Web Development
  3. System Scripting
  4. Mathematics

To learn how to program in Python, visit Python Basics.

You can visit C# Corner Python Learn Series.

Installing multiple versions of Python

I will be using Python 3.10 and Python 2.7.

You can download the installer for Python 2.7 here. You can learn to install python2 on Windows here.

You can download the installer for Python 3.10 here. You can learn to install python3 on Windows here.

Setting up Python Versions

We will now create copies of the python executable and rename them to python27 and python310. This is necessary so that system knows the python version to run.

Open Windows PowerShell as administrator and execute the following command to generate the copies

# Python 2.7:
copy C:\Python27\python.exe C:\Python27\python27.exe

# Python 3.10:
copy C:\Python310\python.exe C:\Python27\python310.exe

Set up Environment Variables

The Environment Variable is a variable that the computer creates and maintains automatically. It assists the system in determining where to install files, locate programs, and check for user and system preferences. It may also be accessed from anywhere on the computer by graphical and command-line tools.

It is required to set up the environment variables so that system knows the executable file that it needs to run on a given command.

Step 1

Press Windows+S to open the search menu and type "environment", and press open.

Step 2

Click "Environment Variables...".

Step up Python PATH variable

The PATH environment variable includes a list of folders that the computer searches for executable files. It searches each directory in the list from top to bottom for the specified executable file. It also stops looking for a matching executable file and starts the application or command.

Step 3

Double click on the "Path".

Step 4

Click on "New" and add the following one by one.

  1. C:\Python27\Scripts\
  2. C:\Python27
  3. C:\Python310\Scripts\
  4. C:\Python310

Note: You may find Python 3 already in the path if you have selected "Add to Path" during the installation process.

Running Python Versions

 

1. Running Python 2

Execute the following command to run your python program using python2

python27

2. Running Python 3

Execute the following command to run your python program using python3

python3

3. Running default Python version

I have kept Python 2 as my default version, as if I run python then it will open Python 2.7. You can check what is the default version for your system.

python

There is another method that can be helpful in handling different python versions, which is to use virtual environments. You can use the following command to create virtual environments specific to the python version.

# Python 2.7:
python27 -m venv venv2

#Python 3.10:
python310 -m venv venv3

Use the following command to activate the virtual environments

# Python 2.7:
venv2\scripts\activate

# Python 3.10:
venv3\scripts\activate

Use the following to opt-out of the virtual environment.

deactivate

Conclusion

In this article, we discussed how we can install and manage different python versions on Windows 11.

Visit C# Corner to find answers to more such questions.



C# Corner
MVP Program Director