Skip to main content

How to Create a Python Virtual Environments with a Specific Version

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*
# Output might include: python3.8, python3.10, python3.11

On Windows, use the Python Launcher:

py --list
# Output:
# - 3.11-64
# - 3.9-64
note

If you are missing the required version, you must install it first (e.g., sudo apt install python3.9 on Ubuntu or downloading the installer from python.org for Windows).

Method 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.

# ⛔️ Incorrect: Uses the system default (e.g., Python 3.10)
python3 -m venv my_env

# ✅ Correct: Explicitly call the specific version executable
# Syntax: [path/to/python_executable] -m venv [env_name]
python3.9 -m venv my_env

On Windows, if you use the Python Launcher:

# ✅ Correct: Windows specific version launch
py -3.9 -m venv my_env

This 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 virtualenv

To create an environment with a specific version, use the -p (python) flag pointing to the executable.

# ✅ Correct: Pointing to the executable path
virtualenv -p /usr/bin/python3.8 my_legacy_env

# On Windows
virtualenv -p C:\Python38\python.exe my_legacy_env

Activating 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 SystemShellCommand
Linux / macOSBash/Zshsource my_env/bin/activate
WindowsCommand Promptmy_env\Scripts\activate.bat
WindowsPowerShellmy_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:

deactivate

Verifying the Environment

After activation, it is crucial to verify that the environment is using the correct Python version and path.

# Activate the environment first
source my_env/bin/activate

# Check the version
python --version
# Output: Python 3.9.x (Should match your target)

# Check the path (ensure it points to your env folder, not /usr/bin)
which python # Linux/Mac
where python # Windows
# Output: .../my_env/bin/python
tip

Always check which python or where python immediately after activation to ensure you are not accidentally installing packages into your global system.

Conclusion

To create a Python virtual environment with a specific version:

  1. Install the desired Python version on your host machine.
  2. Execute venv using that specific binary (e.g., python3.8 -m venv .venv).
  3. Activate the environment using the script in the bin (Linux/Mac) or Scripts (Windows) folder.
  4. Verify using python --version.