How to Use Different Python Versions With Virtualenv

Avatar

By squashlabs, Last Updated: November 2, 2023

How to Use Different Python Versions With Virtualenv

Introduction

Python is a versatile and widely-used programming language that is constantly evolving. As new versions of Python are released, it can be beneficial to use different versions for different projects or to test compatibility. Virtualenv is a popular tool that allows you to create isolated Python environments, enabling you to install different Python versions and packages without interfering with your system-wide Python installation.

Related Article: How To Fix The 'No Module Named Pip' Error

Step 1: Install Virtualenv

The first step to using different Python versions with Virtualenv is to install Virtualenv itself. You can install Virtualenv using pip, the package installer for Python. Open your terminal or command prompt and run the following command:

pip install virtualenv

This will install Virtualenv globally on your system.

Step 2: Create a Virtual Environment

Once Virtualenv is installed, you can create a new virtual environment for your project. A virtual environment is a self-contained directory that contains a Python installation and any packages you install within it. To create a virtual environment, navigate to the directory where you want to create it and run the following command:

virtualenv myenv

This will create a new directory called “myenv” that contains the virtual environment.

Step 3: Activate the Virtual Environment

After creating the virtual environment, you need to activate it before you can use it. The activation process differs depending on your operating system.

For Windows, run the following command:

myenv\Scripts\activate

For macOS and Linux, run the following command:

source myenv/bin/activate

Once the virtual environment is activated, your shell prompt will be prefixed with the name of the virtual environment, indicating that you are now using the isolated Python environment.

Related Article: How to Force Pip to Reinstall the Current Version in Python

Step 4: Install a Different Python Version

With the virtual environment activated, you can now install a different version of Python. You can specify the Python version during the creation of the virtual environment by using the --python flag followed by the path to the Python interpreter executable. For example, to create a virtual environment with Python 3.9, you would run the following command:

virtualenv --python=/path/to/python3.9 myenv

Alternatively, if you have already created the virtual environment without specifying a Python version, you can still change the Python version by manually installing a different version of Python within the virtual environment. To do this, activate the virtual environment and then use the pip command to install the desired Python version. For example, to install Python 3.9, you would run:

pip install python==3.9

Note that you may need to specify the desired version using the correct package name. Check the Python documentation or package index for the specific package name for the desired version.

Step 5: Verify the Python Version

After installing the desired Python version, you can verify that the correct version is being used within the virtual environment. Simply run the following command:

python --version

This will display the version of Python that is currently active in the virtual environment.

Step 6: Install Packages

Once you have the desired Python version installed in the virtual environment, you can start installing packages specific to your project. Activate the virtual environment and use the pip command to install packages just as you would in a regular Python environment. For example:

pip install requests

This will install the “requests” package within the virtual environment.

Related Article: How to Pip Install From a Git Repo Branch

Step 7: Deactivate the Virtual Environment

When you are finished working in the virtual environment, you can deactivate it to return to your system-wide Python environment. Simply run the following command:

deactivate

After deactivating the virtual environment, your shell prompt will no longer be prefixed with the virtual environment’s name.

Alternative Approach: Using pyenv

Another approach to using different Python versions is to use pyenv, a popular Python version management tool. Pyenv allows you to easily install and switch between multiple Python versions on the same machine. Here are the basic steps to use pyenv:

1. Install pyenv by following the instructions in the official pyenv repository: [pyenv](https://github.com/pyenv/pyenv#installation)

2. Install the desired Python versions using pyenv. For example, to install Python 3.9, run the following command:

   pyenv install 3.9.0

3. Create a new virtual environment using the desired Python version. For example:

   pyenv virtualenv 3.9.0 myenv

4. Activate the virtual environment:

   pyenv activate myenv

This will set the desired Python version for the current shell session.

5. Install packages and work on your project within the activated virtual environment.

6. Deactivate the virtual environment when you are done:

   pyenv deactivate

Pyenv provides a flexible and convenient way to manage multiple Python versions and virtual environments, making it a popular choice for developers who frequently switch between different Python environments.

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How to Install Specific Package Versions With Pip in Python

Guide on installing a specific version of a Python package using pip. Learn different methods such as using the == operator, specifying version ranges, and using the pip... read more

How to Upgrade Pip3 in Python

Upgrading Pip3 in Python is essential for ensuring that your Python packages are up to date and compatible with the latest features and bug fixes. This article provides... read more

How to Uninstall All Pip Packages in Python

A guide to uninstalling all Pip packages in Python, including checking installed packages, uninstalling individual packages, removing all packages, using a... read more

How to Remove a Virtualenv in Python

Removing a Python virtual environment is a simple process that can be done in a few steps. In this article, we will guide you through the process step-by-step, ensuring... read more

How To Fix ‘Pip’ Not Recognized As Internal Or External Command

Python developers often encounter the frustrating error message 'pip' is not recognized as an internal or external command. This article provides a step-by-step guide to... read more

How To Install Packages With Pip From Requirements.Txt

Installing packages with pip from requirements.txt is a common task for Python developers. In this article, you will learn how to efficiently use pip to install packages... read more