Skip to content

Creating Virtual Environments with Specific Python Versions

Problem Statement

Developers often need to work with different Python versions for various projects, especially when compatibility issues arise with specific libraries or frameworks. The challenge is creating isolated Python environments (virtual environments) with different Python versions without affecting the system-wide Python installation or other projects.

This commonly occurs when:

  • A project requires an older Python version for compatibility
  • Different projects need different Python versions simultaneously
  • Testing code across multiple Python versions

Using uv (Modern Approach)

NEW

uv is a fast Python package installer and resolver written in Rust, released in 2024

uv provides the most straightforward method for managing Python versions and virtual environments:

bash
# List available Python versions
uv python list

# Create a virtual environment with Python 3.8
uv venv --python 3.8

uv automatically handles Python version installation if needed, storing them in ~/.local/share/uv on Linux systems.

WARNING

uv-created environments don't include pip by default, as it assumes you'll use uv for package management.

If you prefer to use uv only for Python version management but want a standard venv environment:

bash
uv run --python 3.8 python -m venv myenv

Using Standard venv with Specific Python Interpreters

The most direct approach uses Python's built-in venv module with a specific Python interpreter:

bash
# Syntax
pythonX.Y -m venv /path/to/venv

# Example with Python 3.8
python3.8 -m venv myenv

This requires having the desired Python version already installed on your system.

Using the Python Launcher on Windows

Windows users can leverage the Python launcher (py) to manage multiple versions:

bash
# List available Python installations
py --list

# Create venv with specific version
py -3.8 -m venv test_env

# Activate on Windows
.\test_env\Scripts\Activate.ps1

Platform-Specific Installation Guides

Ubuntu/Debian Systems

WARNING

Never replace the system Python on Ubuntu, as system components depend on it.

bash
# Add deadsnakes PPA for additional Python versions
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update

# Install Python 3.8
sudo apt install python3.8 python3.8-venv

# Create virtual environment
python3.8 -m venv ~/.venvs/my-project-env

# Activate
source ~/.venvs/my-project-env/bin/activate

macOS with Homebrew

bash
# Install Python 3.8
brew install python@3.8

# Create virtual environment
python3.8 -m venv venv

# Activate
source venv/bin/activate

Windows

  1. Download the desired Python version from python.org
  2. During installation, avoid adding to PATH if you have existing Python installations
  3. Create virtual environment using the full path:
bash
# Find Python installations
py -0p

# Create venv
C:\Path\To\Python38\python.exe -m venv myenv

# Activate
myenv\Scripts\Activate.ps1

Advanced Version Management

Using pyenv

pyenv is excellent for managing multiple Python versions:

bash
# Install pyenv (method varies by OS)
# For Arch Linux: yay -S pyenv

# Install specific Python version
pyenv install 3.8.12

# Set local version for a directory
pyenv local 3.8.12

# Create virtual environment
python -m venv .venv

Using conda

For data science projects, conda can manage both packages and environments:

bash
# Create environment with specific Python version
conda create -n myenv python=3.8

# Activate
conda activate myenv

Verification

After creating your virtual environment, verify the Python version:

bash
# Activate the environment
source venv/bin/activate  # Linux/macOS
# or
.\venv\Scripts\Activate.ps1  # Windows

# Check Python version
python --version

# Check pip version
pip --version

Best Practices

  1. Isolate projects: Use separate virtual environments for each project
  2. Document requirements: Use pip freeze > requirements.txt to document dependencies
  3. Version control: Exclude virtual environments from version control (add to .gitignore)
  4. Clear naming: Use descriptive names for environments that include Python version
  5. Regular updates: Keep your Python versions and dependencies updated for security

TIP

Visual Studio Code automatically detects virtual environments in your workspace. When you open a project with a virtual environment, VS Code will typically detect and offer to use it automatically.

Summary Table

MethodBest ForCross-PlatformComplexity
uvModern workflows, fast setupYesLow
venvStandard Python developmentYesLow
pyenvMultiple version managementYesMedium
condaData science, complex dependenciesYesMedium
System packagesUbuntu/Debian systemsLimitedMedium

Choose the method that best fits your workflow. For most developers, uv or standard venv with specific Python interpreters provides the simplest solution for creating virtual environments with different Python versions.