How to Use Different Python Versions With Virtualenv

Muhammad Abdullah Arif
2 min readMay 29, 2024
Photo by Hitesh Choudhary on Unsplash

Step 1: Install Virtualenv

First, ensure you have virtualenv installed. If not, you can install it using pip:

pip install virtualenv

Step 2: Create a Virtual Environment

To create a virtual environment with a specific Python version, use the -p flag followed by the path to the desired Python executable. For example:

virtualenv -p python3.7 myenv

This command creates a virtual environment named myenv using Python 3.7.

Step 3: Activate the Virtual Environment

Activate the virtual environment using the appropriate command for your operating system:

On Windows:

  • myenv\Scripts\activate

On macOS/Linux:

  • source myenv/bin/activate

Step 4: Install a Different Python Version

If you haven’t already, install the desired Python version on your system. Then, create a new virtual environment using that version:

virtualenv -p python3.8 myotherenv

Step 5: Verify the Python Version

--

--

Muhammad Abdullah Arif

Python developer. The facts are the facts but opinions are my own.