Question
How can I create a Python virtual environment using a specific Python version?
For example, if multiple Python versions are installed on my system, how do I choose which interpreter virtualenv should use when creating the environment?
Short Answer
By the end of this page, you will understand how Python virtual environments choose an interpreter, how to create an environment with a specific Python version, and how developers use this in real projects to keep dependencies isolated and reproducible.
Concept
A Python virtual environment is an isolated workspace that contains its own Python executable and installed packages. This lets one project use one set of dependencies while another project uses a different set.
The key idea behind your question is that a virtual environment is created from a specific Python interpreter. If you have Python 3.10, 3.11, and 3.12 installed, the environment will be based on whichever interpreter you tell the tool to use.
This matters because:
- Some projects only support certain Python versions.
- Packages can behave differently across Python versions.
- Teams often need consistent development environments.
- Deployment targets may require matching the production Python version.
In practice, when you create a virtual environment, you usually do one of these:
- Run a specific Python executable with
-m venv - Tell
virtualenvwhich interpreter to use with-p
The environment does not magically switch Python versions later. Its version is decided when the environment is created.
Mental Model
Think of Python versions as different models of the same machine, and a virtual environment as a private workshop built around one machine.
If you build the workshop around Python 3.11, that workshop uses Python 3.11. If you want Python 3.12 instead, you build a new workshop around Python 3.12.
So the important question is not "How do I change the workshop afterward?" but "Which machine do I use when I build it?"
Syntax and Examples
The most common modern approach is to use the interpreter directly:
python3.11 -m venv myenv
This creates a virtual environment named myenv using Python 3.11.
If you are using virtualenv, you can specify the interpreter with -p:
virtualenv -p python3.11 myenv
You can also provide the full path to the interpreter:
virtualenv -p /usr/bin/python3.11 myenv
Example using venv
python3.10 -m venv env310
source env310/bin/activate
python --version
On Windows:
py -3.10 -m venv env310
env310\Scripts\activate
python --version
Explanation:
python3.10 -m venv env310creates the environment with Python 3.10.- Activating it updates your shell so
pythonpoints to the environment's interpreter. python --versionconfirms which version the environment is using.
Step by Step Execution
Consider this example:
python3.11 -m venv demoenv
source demoenv/bin/activate
python --version
pip install requests
Step by step:
-
python3.11 -m venv demoenv- Your system finds the
python3.11executable. - That interpreter creates a new virtual environment folder named
demoenv. - Inside it, Python binaries and package-management tools are set up.
- Your system finds the
-
source demoenv/bin/activate- Your shell is updated so commands like
pythonandpippoint to the environment. - You are still on the same machine, but now using the environment's private Python tools.
- Your shell is updated so commands like
-
python --version- This should print Python 3.11.x.
- That confirms the environment was created from the intended interpreter.
-
pip install requests- The package is installed only inside
demoenv. - It does not affect global packages or other virtual environments.
- The package is installed only inside
A useful check is:
Real World Use Cases
Here are common situations where choosing the Python version matters:
-
Maintaining older applications
- A legacy app may require Python 3.8 even if your system default is 3.12.
-
Testing compatibility
- A library maintainer may create one environment for Python 3.9 and another for 3.11.
-
Matching production
- If a server runs Python 3.10, developers often create local environments with Python 3.10 too.
-
Learning and experimentation
- You may want to compare how code behaves between Python versions.
-
Dependency isolation
- Some packages work only on certain versions, so separate environments avoid conflicts.
Real Codebase Usage
In real projects, developers usually combine version-specific environments with a few standard habits:
-
Pinning the Python version
- Projects often document a required version in a
README,pyproject.toml,.python-version, or CI configuration.
- Projects often document a required version in a
-
Creating fresh environments per project
- Instead of reusing one environment for many apps, each project gets its own environment.
-
Using guard checks
- Scripts may verify the Python version before running:
import sys
if sys.version_info < (3, 10):
raise RuntimeError("Python 3.10 or newer is required")
-
Testing across versions
- Teams often run automated tests against multiple Python versions in CI.
-
Avoiding global installs
- Dependencies are installed into the active environment, not system-wide.
-
Recreating instead of mutating
- If the wrong Python version was used, developers usually delete the environment and recreate it with the correct interpreter.
Common Mistakes
1. Assuming activation changes the Python version
Activation does not choose the Python version. It only starts using the environment that was already created.
Broken assumption:
python3.12 -m venv env
source env/bin/activate
# expecting it to become Python 3.10 somehow
Fix: create the environment with the correct interpreter from the start.
2. Using python -m venv when python points to the wrong version
python -m venv myenv
This uses whatever python currently means on your system, which may not be the version you want.
Fix:
python3.11 -m venv myenv
3. Forgetting to verify the version
After activation, beginners often install packages without checking the interpreter.
Fix:
python --version
4. Trying to change the Python version inside an existing environment
A virtual environment is tied to the interpreter it was built from.
Fix:
Comparisons
| Approach | Example | When to use | Notes |
|---|---|---|---|
venv with explicit interpreter | python3.11 -m venv myenv | Best default choice in modern Python | Built into Python |
virtualenv with -p | virtualenv -p python3.11 myenv | Useful if your workflow already uses virtualenv | Requires virtualenv package |
| Full interpreter path | virtualenv -p /usr/bin/python3.11 myenv | Best when PATH is unclear | Very explicit |
| Windows launcher |
Cheat Sheet
# Create with built-in venv
python3.11 -m venv myenv
# Activate on macOS/Linux
source myenv/bin/activate
# Activate on Windows CMD
myenv\Scripts\activate.bat
# Activate on Windows PowerShell
myenv\Scripts\Activate.ps1
# Check version
python --version
# Using virtualenv instead
virtualenv -p python3.11 myenv
# Using full path
virtualenv -p /path/to/python3.11 myenv
# Windows launcher
py -3.11 -m venv myenv
Key rules:
- The Python version is chosen when the environment is created.
- Activation does not change the environment's Python version.
- If you used the wrong version, recreate the environment.
- Prefer explicit interpreter names when multiple versions are installed.
- Verify with
python --versionafter activation.
FAQ
How do I create a virtual environment with Python 3.11?
Use an explicit interpreter command such as:
python3.11 -m venv myenv
Or on Windows:
py -3.11 -m venv myenv
Can I change the Python version of an existing virtual environment?
No. A virtual environment is tied to the interpreter used to create it. Create a new environment with the correct version instead.
Should I use venv or virtualenv?
For most modern Python projects, venv is enough and is built in. Use virtualenv if your workflow specifically requires it.
Why does my environment use the wrong Python version?
You probably created it with python -m venv when python pointed to a different interpreter than expected.
How can I see which Python versions are installed?
That depends on your system. Common checks include python --version, python3 --version, python3.11 --version, or on Windows py -0.
Mini Project
Description
Create a small project setup that lets you build and verify a virtual environment for a specific Python version. This demonstrates the real-world habit of choosing the correct interpreter before installing dependencies.
Goal
Create and verify a virtual environment that uses a specific Python version, then install a package inside it.
Requirements
- Create a new virtual environment using a specific Python version.
- Activate the environment.
- Confirm the Python version inside the environment.
- Install one package inside the environment.
- Print the installed package information to verify it was installed there.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.