Managing dependencies is a cornerstone of Python development. A Virtual Environment is an isolated directory containing a specific Python interpreter and a set of libraries. This allows you to work on Project A using Python 3.10 and Django 4.0, while Project B uses Python 3.8 and Django 2.2, without conflicts.
This guide explains how to create a virtual environment targeting a specific Python version using the built-in venv module and the third-party virtualenv tool.
Prerequisites: Installing Python Versions
To create a virtual environment with a specific Python version (e.g., Python 3.9), that version must be installed on your system. You cannot create a Python 3.9 environment if you only have Python 3.10 installed.
Checking Installed Versions
On Linux/macOS, check your available executables:
ls /usr/bin/python*On Windows, use the Python Launcher:
py --listMethod 1: Using the Built-in venv Module
The venv module is the standard way to create environments in Python 3.3+. The key logic is: The environment will be created using the version of Python that runs the command.
To create an environment with Python 3.9, you must invoke the venv module using the python3.9 executable.
python3 -m venv my_env
python3.9 -m venv my_envOn Windows, if you use the Python Launcher:
py -3.9 -m venv my_envThis creates a directory named my_env containing a copy (or symlink) of the Python 3.9 binary and a standalone pip installer.
Method 2: Using virtualenv (Alternative)
virtualenv is a popular third-party tool that predates venv. It is useful if you need to create environments for older Python versions (Python 2) or if you want slightly faster creation times.
First, ensure it is installed:
pip install virtualenvTo create an environment with a specific version, use the -p (python) flag pointing to the executable.
virtualenv -p /usr/bin/python3.8 my_legacy_env
virtualenv -p C:\Python38\python.exe my_legacy_envActivating and Deactivating
Creating the environment is only the first step. You must activate it to start using the isolated interpreter and libraries.
Activation Commands
| Operating System | Shell | Command |
|---|---|---|
| Linux / macOS | Bash/Zsh | source my_env/bin/activate |
| Windows | Command Prompt | my_env\Scripts\activate.bat |
| Windows | PowerShell | my_env\Scripts\Activate.ps1 |
Once activated, your command prompt will usually change to indicate the active environment:
(my_env) user@machine:~$Deactivating
To exit the environment and return to the system global Python, simply run:
deactivateVerifying the Environment
After activation, it is crucial to verify that the environment is using the correct Python version and path.
source my_env/bin/activate
python --version
which python # Linux/Mac
where python # WindowsConclusion
To create a Python virtual environment with a specific version:
- Install the desired Python version on your host machine.
- Execute
venvusing that specific binary (e.g.,python3.8 -m venv .venv). - Activate the environment using the script in the
bin(Linux/Mac) orScripts(Windows) folder. - Verify using
python --version.