Summary: You can run multiple versions of Python on Windows using one of the following methods:
- Using entire path to execute the code.
- Creating A Shortcut or Symbolic Link to the executable files.
- Using Pylauncher:
- Use Shebang (#) in The Script
- Run Pylauncher Command
- Using Virtual Environments.
➠ Problem Formulation: You might have two versions of Python installed on your Windows machine (versions 2.7 and 3.8). Now you want to run one of the projects on 2.6 for one project and 2.5 for another. So, how can you specify which version you want to use for a specific script?
Thus in this article, you will learn how to run multiple Python versions on Windows with visuals and illustrations. But before proceeding further, you must be aware of the different versions of Python installed on your system.
❖ How to check all the installed Python versions on Windows?
Type the following command in your command prompt to find the versions of Python available on your system:
The above command will list all the versions of Python installed on your system. If you want to find the different versions installed as well as the path to each installed version, use the following command:
The following graphical illustration will help you to understand the above commands clearly.
Once you have verified the Python versions installed on your system, you can use one of the methods mentioned below to run a specific script in a specific version.
❖ Method 1: Using Path
When you try to run Python in the command prompt, what it does is that it searches the
%PATH% environment variable and checks for an executable file which can either be a batch file (
.bat), command file (
.exe) or any other executable file (
.exe) that matches the name given. Once the correct file is found, it executes the program using that file. Now, if you have two versions of Python installed on your system (Python 2.7 and 3.8), then the path variable will contain the location of both the directories. But, the problem is once Windows finds the first match, it will stop examining any other path.
To overcome this barrier, you have to explicitly call one or both of the applications using their path. For example, as you can see below, I have two versions of Python installed on my system.
- To execute your code in Python 2.7 then you must call
- To execute your code in Python 3.8, you must call
C:\ProgramData\Anaconda3\python.exe. However this is not necessary as the default selected version is already Python-3.8 in this case.
❖ Method 2: Creating A Shortcut or Symbolic Link
➥ Creating a Shortcut: If you want to avoid using the entire path, then you can create a shortcut for each individual
python.exe file and rename them as python27 and python38. Now you can run each version using their respective shortcuts directly.
In order to create the shortcuts, follow the given steps:
- Navigate to the folder containing the Python version you want to create a shortcut for,
- Right-click and create shortcut.
- Rename the shortcut.
➥ Creating a Symbolic Link: Alternatively, you can also a symbolic link to the respective executable files and rename them. Then you can execute your code using python27, i.e. the name that was given by you.
Note: Put the shortcuts somewhere which is included in the path so that they can be easily invoked.
C:\Windows\System32>cd C:\bin C:\bin>mklink python27.exe C:\Python27\python.exe symbolic link created for python27.exe <<===>> C:\Python27\python.exe
Once the shortcut and the links have been created, you can easily invoke a specific version using the name specified by you, as shown below:
❖ Method 3: Using Python Launcher
You can use pylauncher and then use one of the following approaches to execute your script in the version you want to.
Note: If you have Python 3.3 and above, there is no need to install it manually because it is already available with newer versions of Python.
➥ Approach 1: Use Shebang (#) in The Script
You can simply use a shebang line in your script to mention the version you want the script to be executed in.
#! c:\[path to Python 2.7]\python.exe – To execute scripts with Python 2.7.
#! c:\[path to Python 3.8]\python.exe – To execute scripts with Python 3.8.
Example: The following image shows how you can use shebang in your script and execute it directly in your command prompt.
➥ Approach 2: Run Pylauncher Command
If you are uncomfortable with shebang, you can directly run the script in a specific version using the pylauncher command,i.e.
py -2.6– for running script in Python version 2.6
py -2– for running script in the latest installed version of Python 2.x
py -3.4– for running script in Python version 3.4
py -3– or running script in latest installed version Python 3.x
❖ Method 4: Using Virtual Environments
Another approach to run different versions of Python on Windows is to install virtualenv and create two independent virtual environments for the respective Python versions.
virtualenv -p c:\[path to required Python version]\python.exe [path where you want to keep virtualenv that uses the Python version specified]\[name of virtualenv]
C:\Users\DELL\Desktop>virtualenv -p C:\Python27\python.exe c:\venvs\2.7 C:\Users\DELL\Desktop>virtualenv -p C:\Users\DELL\AppData\Local\Programs\Python\Python38\python.exe c:\venvs\3.8
Once the virtual environments have been created, you can activate a specific environment to use that version, as shown below:
To deactivate it use the command:
❖ How To Select Python Version In PyCharm?
If you are using Pycharm to execute your code, then you can do so with the help of the following steps:
- Go to File
- Select Settings
- Select Project: project_name ➟ Python Interpreter
- Select the interpreter (version) you want. (Add it if it’s not listed.)
Note: In PyCharm 2019.1 and above, there is a new feature known as Interpreter in status bar. This feature allows switching between python interpreters and verifying which version of Python you’re using much easier and user-friendly. This feature has been shown in the image below (highlighted in red at the bottom right corner).
- Do you want to master the most popular Python IDE fast?
- This course will take you from beginner to expert in PyCharm in ~90 minutes.
- For any software developer, it is crucial to master the IDE well, to write, test and debug high-quality code with little effort.
Join the PyCharm Masterclass now, and master PyCharm by tomorrow!