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
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
- macOS (with Homebrew):
brew install openssl readline sqlite3 xz zlib
brew install pyenv
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
- 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
Then reload your shell:
source ~/.bashrc # or ~/.zshrc
Step 3: Install a Specific Python Version
pyenv install 3.10.12
🧪 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
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
Option 2: Using virtualenv
(Alternative Approach)
First, install virtualenv
:
pip install virtualenv
Then run:
virtualenv -p /usr/bin/python3.10 venv-py310 # Linux
virtualenv -p /opt/homebrew/bin/python3.10 venv-py310 # macOS
🚀 Activating the Virtual Environment
To activate the environment:
source venv-py310/bin/activate
You should now see the environment name in your prompt like:
(venv-py310) user@hostname:~/project$
🧹 Deactivating and Cleaning Up
To deactivate:
deactivate
To delete the environment:
rm -rf venv-py310
🧩 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.