There are several ways to create virtual environments in Python, each with different tools and approaches. A virtual environment is an isolated workspace where you can install Python packages separately from your system-wide Python installation.
Python developers can manage virtual environments in multiple ways, depending on project requirements. The built-in venv module provides lightweight isolation suitable for most projects. virtualenv extends these capabilities with additional features. For environments that require managing multiple Python versions, pyenv offers version control, while pyenv-virtualenv integrates versioning with automatic environment activation. Selecting the appropriate method ensures reproducibility, cleaner dependency management, and greater efficiency in Python development workflows.
Let me walk you through these options:
venv – Built-in Python Module
The simplest approach uses Python’s built-in venv module. venv is built into Python 3.3 and above. So this is the simplest approach if you are using Python version 3.3 and above and if you have just one version of Python installed on your system.
If you are working on multiple projects each with different Python version then using venv alone may not be suffice. Will talk more about using multiple python versions in next sections.
# Go to your project directory
cd your-project-directory
# Create virtual environment
python -m venv .myenv
#### Activate on Linux/Mac
source .myenv/bin/activate
#### Activate On Windows
.myenv\Scripts\activate
# Install Packages
pip install requests
This creates a lightweight virtual environment using the Python version that ran the command.
- python -m venv .myenv creates a
.myenvenvironment folder under current directory. - A copy of the Python interpreter is placed inside the environment folder.
- A separate
site-packagesdirectory is created for installing libraries. - Scripts and executables (like
pip) are tied to that environment.
When you run pip install requests, this installs packages only into myenv, not your global Python.
virtualenv Package – Support Older Python Versions
A more feature-rich option that works with older Python versions. It works almost the same as venv but has more features (e.g., faster environment creation, works with older Python versions, can create environments for multiple interpreters).
# Install virtualenv
pip install virtualenv
# Create environment
virtualenv myenv
# Can specify Python version
virtualenv -p python3.9 myenv
#### Activate on Linux/Mac
source myenv/bin/activate
#### Activate On Windows
myenv\Scripts\activate
# Install Packages
pip install requests
Here, virtualenv myenv does the following
- This creates a folder
myenv/with: - A copy of the Python interpreter
- A
bin/(Linux/Mac) orScripts\(Windows) folder with executables - Its own
site-packages/for installed libraries
pipenv Package – Combines pip & virtualenv
Combines pip and virtualenv with dependency management:
# Install pipenv
pip install pipenv
# Create environment and Pipfile
pipenv install
# Install packages
pipenv install requests
# Activate shell
pipenv shell
# Run commands in environment
pipenv run python script.py
pyenv – Managing Different Python Versions
Python developers often run into version conflicts, different projects (or tools) may need different versions of Python. Virtual environments (venv, virtualenv) isolate packages but not the interpreter version (Python versions).
Imagine you have following scenarios.
Scenario 1: Imagine macOS/Linux ship with Python 2.7 or 3.8 as default. But modern frameworks (like Django, FastAPI, PyTorch) may need Python 3.10+.
Scenario 2: Project A: uses TensorFlow, which works best with Python 3.9. Project B: uses new async features in Python 3.12. Without pyenv, you’d constantly break one project while fixing another.
pyenv is a Python version manager. It helps you install, switch between, and manage multiple Python versions on the same machine.
Without it, you’d be stuck with your system Python (often outdated), and switching versions would be messy.
How pyenv Solves this
- Lets you install multiple Python versions side by side.
- Lets you switch versions per project (via
.python-version). - Works with
venv,virtualenv, or tools likepipenv/poetryfor package management.
Let’s see how to use pyenv
# Install pyenv
brew install pyenv
# Install Python versions
pyenv install 3.10.13
pyenv install 3.12.1
# For ProjectA
cd projectA
pyenv local 3.10.13
python3 --version # Returns 3.10.13
# For ProjectB
cd projectB
pyenv local 3.12.1
python3 --version # Returns 3.12.1
Now, let’ see how to use pyenv with virtualenv and pyenv with venv
pyenv with venv (I use this option frequently)
If your project required Python version 3.3, you can use pyenv with built-in venv module.
# Install Python versions
pyenv install 3.10.13
# For ProjectA
cd projectA
pyenv local 3.10.13
python3 --version # Returns 3.10.13
# Create virtual environment
python -m venv .myenv
source .myenv/bin/activate
# Install package
pip install requests
pyenv-virtualenv
If your project required Python version before 3.3, you can use pyenv with virtualenv module.
# Install Python versions
pyenv install 3.10.13
# Create virtual environment
cd projectA
pyenv virtualenv 3.10.13 myenv
# Set version to the project
pyenv local myenv
Conclusion
Python offers several ways to manage virtual environments, each suited for different workflows and needs. The built-in venv module is lightweight, reliable, and ideal for project-local environments that keep everything self-contained. Tools like virtualenv add extra features, such as faster environment creation and broader compatibility. When version management is also a concern, pyenv excels at handling multiple Python versions, and combining it with venv or the pyenv-virtualenv plugin provides both flexibility and convenience.
Ultimately, the best choice depends on your priorities:
- Choose
venvfor simplicity and portability. - Use
pyenv + venvif you need strict control over Python versions with per-project isolation. - Opt for
pyenv-virtualenvif you prefer centralized management and automatic environment activation.
By understanding these different approaches, you can pick the right tool—or combination of tools—that makes your Python development more organized, reproducible, and efficient.