How to Create a Virtual Environment with a Specific Python Version
Prashant Iyenga

Prashant Iyenga @pi19404

Joined:
Sep 9, 2024

How to Create a Virtual Environment with a Specific Python Version

Publish Date: Jul 12
0 0

Managing multiple Python projects often means juggling different package versions—and sometimes entirely different Python versions. This is where virtual environments shine. In this blog post, you'll learn how to create an isolated virtual environment using a specific version of Python, tailored for Linux and macOS users.


🧠 Why Use a Virtual Environment?

Before jumping into the steps, here’s why using virtual environments is considered best practice:

  • Isolated Dependencies: Keeps project requirements isolated from your system Python.
  • Avoids Conflicts: Prevents dependency collisions across different projects.
  • Reproducibility: Makes deployments and collaboration smoother by standardizing environments.

🛠️ Prerequisites

Make sure your target Python version is available on your system. You can verify which versions are installed:

ls /usr/bin/python*          # Linux
ls /opt/homebrew/bin/python*  # macOS with Homebrew
Enter fullscreen mode Exit fullscreen mode

If your desired version is missing, install it using pyenv.


🔧 Installing pyenv (Recommended for Managing Multiple Python Versions)

Step 1: Install Dependencies

  • Linux (Debian/Ubuntu):
sudo apt update
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git libncursesw5-dev \
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
Enter fullscreen mode Exit fullscreen mode
  • macOS (with Homebrew):
brew install openssl readline sqlite3 xz zlib
brew install pyenv
Enter fullscreen mode Exit fullscreen mode

Step 2: Add pyenv to your shell startup file

  • Bash:
echo -e '\n# pyenv setup' >> ~/.bashrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  • Zsh:
echo -e '\n# pyenv setup' >> ~/.zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Then reload your shell:

source ~/.bashrc      # or ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

Step 3: Install a Specific Python Version

pyenv install 3.10.12
Enter fullscreen mode Exit fullscreen mode

🧪 Creating the Virtual Environment

Option 1: Using python -m venv

This is the built-in way to create a virtual environment using a specific Python binary.

# Activate desired Python version (if using pyenv)
pyenv shell 3.10.12

# Create the virtual environment
python -m venv venv-py310
Enter fullscreen mode Exit fullscreen mode

You can also specify the full path to the Python binary:

/opt/homebrew/bin/python3.10 -m venv venv-py310  # macOS
/usr/bin/python3.10 -m venv venv-py310           # Linux
Enter fullscreen mode Exit fullscreen mode

Option 2: Using virtualenv (Alternative Approach)

First, install virtualenv:

pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

Then run:

virtualenv -p /usr/bin/python3.10 venv-py310      # Linux
virtualenv -p /opt/homebrew/bin/python3.10 venv-py310  # macOS
Enter fullscreen mode Exit fullscreen mode

🚀 Activating the Virtual Environment

To activate the environment:

source venv-py310/bin/activate
Enter fullscreen mode Exit fullscreen mode

You should now see the environment name in your prompt like:

(venv-py310) user@hostname:~/project$
Enter fullscreen mode Exit fullscreen mode

🧹 Deactivating and Cleaning Up

To deactivate:

deactivate
Enter fullscreen mode Exit fullscreen mode

To delete the environment:

rm -rf venv-py310
Enter fullscreen mode Exit fullscreen mode

🧩 Final Thoughts

Using a specific Python version in a virtual environment helps avoid compatibility issues, ensures reproducible builds, and keeps your system Python clean. This is especially important for macOS and Linux users, where the system Python might be tied to operating system functions.

Comments 0 total

    Add comment