You are currently viewing Using Different Python Versions with virtualenv

Sometimes we would be required to run different versions of Python by using the virtualenv. Python Virtual environments enable us to run your python script with different versions.

Advertisements

As Python is being released new versions regularly, we would be required to keep our project code compatible with a newer version. Also, to avoid conflicts between packages and dependencies, developers use virtual environments to isolate their projects. In this article, we will learn how to use different versions of python with virtualenv.

The virtualenv package allows you to create and manage multiple Python environments on a single machine. With virtualenv, you can create a separate environment for each project, with specific Python versions and dependencies, making it easy to switch between projects.

1. Quick Guide to Python Version Controlling with Virtualenv

Before getting into detail, this section will give you a quick guide on how to use the virtualenv package in python to deal with different versions of python.

Follow the following steps to use a different version with virtual env:

  1. Install the virtualenv package
  2. Now create a virtual environment
  3. Activate the virtual environment
  4. If you want to create another virtual environment, deactivate the virtual environment
  5. Now create the new Virtual Environment with a different version of python
  6. As needed, you can switch to any of these virtual environment that runs on different versions.

These are the steps that you should follow each time for working with a Python virtual environment having different versions of Python. Below is a step-by-step guide in detail:

2. Installing Virtualenv

As you might know virtualenv is a third-party Python package, so you need to install it first before using it. You can install it using any package manager in Python. It can be pip or conda. In this article, I will use the pip command to install packages.

To install virtualenv use the following command on your terminal:


# Install virtualenv using pip
pip install virtualenv

If you are using conda than use the following command:


# Install virtualenv using conda
conda install virtualenv

You can make sure that you have installed the virtualenv package by running the below command on your terminal:


# Verify virtualenv is installed succesfully
virtualenv --version

3. Creating a Virtual Environment with virtualenv

Once you have installed the virtualenv package. The next step is to create a virtual environment. The virutalenv package allows you to create the virtual environment using any of the python version. This means that you can have global environment different than your virtual environment.

First, navigate to the directory where you want to create your virtual environment, and then run the below command on your terminal:


# Create virtual environment
python -m venv myenv

This command will create a virtual environment in your current directory. The version of Python in the virtual environment using this command will be the same is the global version of python.

If you want to create a Python virtual environment using a specific version of Python, follow the below steps:

  1. Make sure you have installed the required version of Python. Suppose you want to create a virtual environment with python version 3.9 so make sure you have that version of Python installed.
  2. Locate the installation directory of Python. It will be in C Drive by default.
  3. Now copy the path and run the below command

python -m venv -p (python-exe-path) env-name

Replace the following:

  1. python-exe-path is the path you copied
  2. env-name is the environment name you want to create

See the below command in my case:


# Create virtual environment with Python 3.11 version
python -m virtualenv -p D:\Python311\python.exe my_second_env

The most important thing is to put the python.exe at the end of the path as you will face the access-denied error when creating a virtual environment using virtualenv. This error is quite misleading and can waste tons of time.

4. Activating a Virtual Environment

Now you have two virtual environments with two different Python versions. You can activate any of them using the activate.bat file. Run the below command on your terminal to activate the environment.

To activate Python virtual environment run the below command on your terminal.


# Activete virtual environment
env/Scripts/activate

A lot of the time this code can produce an error that you are not authorized to use this script, you can change the authorization policy by running the below code on your terminal:


Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
.\first_env\Scripts\activate : File D:\Project\first_env\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. 
For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\first_env\Scripts\activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

5. Deactivating a Virtual Environment

When you feel the need to deactivate the virtual environment, you can run the below command on your terminal. It is a good practice to deactivate the virtual environment before exiting the project.


# Deactive virtual environment
env/Scripts/deactivate

6. Using Virtualenv with Different Python Versions

As discussed earlier, the Python package virtualenv allows us to work with different Python versions at the same time. You can switch to any version of the Python virtual environment by deactivating one and activating another.

See the below picture, I have two virtual environments, one is Python with a Python version 3.9 and the other is 3.11. I can switch to any of these by following the above methods.

different versions of python

7. Summary and Conclusion

In this article, we have discussed how to use different Python versions with virtualenv. We have learned, how to create, activate and deactivate a virtual environment. Working with different versions of Python is easy as you just have to switch from one version to another. Let me know if you have any questions.

Happy Coding!!!

Related Article