If you’ve been writing Python for a little while, you’ve probably heard someone say, “Just use a virtual environment.” But what does that actually mean, and why does it matter?
In this guide, I’ll explain virtual environments in plain language, show you how to set one up using three popular tools, venv, pipenv, and conda, and help you figure out which one is right for your project.
Why Virtual Environments Exist
Here’s the problem they solve. Imagine you have two Python projects on your machine:
- Project A uses Matplotlib 3.6
- Project B needs Matplotlib 3.10 (which introduced breaking changes)
If you install both versions globally, into your system Python, they’ll conflict. Only one version can live in the same Python installation at a time. When you upgrade Project B, Project A breaks.
A virtual environment is an isolated Python installation for a single project. Each environment has its own packages, its own versions, and its own pip. What happens in one environment doesn’t affect any other.
Think of it like this: instead of everyone sharing one kitchen, each project gets its own kitchen with exactly the tools it needs.
Read: Use Python While with Assignment
What a Virtual Environment Actually Is
Under the hood, a virtual environment is just a folder. It contains:
- A copy of (or a symlink to) a Python interpreter
- Its own site-packages folder, where installed packages live
- Activation scripts that tell your terminal to use this environment instead of the system Python
When you “activate” a virtual environment, your terminal’s PATH is updated so that python and pip points to the tools inside your environment folder, not the global ones.
That’s really all there is to it. The magic is in the isolation.
Option 1: venv — Built Into Python, No Install Needed
venv is the simplest option and the one I’d recommend for most beginners. It’s built directly into Python 3 (no installation required) and works on Windows, macOS, and Linux.
Create a Virtual Environment With venv
Navigate to your project folder first, then run:
python -m venv venv
You can see the output in the screenshot below.

This creates a folder called venv inside your project directory. You can name it anything, but venv or .venv is the convention most people use.
To create it with a specific Python version (when you have multiple installed):
python3.12 -m venv venv
Activate the Environment
On macOS/Linux:
source venv/bin/activate
On Windows (Command Prompt):
venv\Scripts\activate.bat
On Windows (PowerShell):
venv\Scripts\Activate.ps1
Once activated, you’ll see (venv) at the start of your terminal prompt. That’s your confirmation that the environment is active.
Install Packages Inside the Environment
With the environment active, install packages normally:
pip install matplotlib
pip install pandas numpy
All packages go into your environment’s folder — nowhere else.
Verify the Install Is Isolated
pip list
You’ll only see packages installed in this environment, not your global ones.
Save and Share Your Dependencies
When you want to share your project with someone else (or deploy it), save your dependencies to a file:
pip freeze > requirements.txt
This creates a requirements.txt file listing every package and its exact version. Anyone else can recreate your exact environment with:
pip install -r requirements.txt
Deactivate the Environment
When you’re done working:
deactivate
Your terminal goes back to using the global Python.
Delete an Environment
Just delete the venv folder:
rm -rf venv # macOS/Linux
rmdir /s /q venv # Windows
Since all packages are inside that folder, deleting it removes everything cleanly.
One Important Thing: Don’t Commit Your venv Folder to Git
Add it to your .gitignore file:
text# .gitignore
venv/
.venv/
You share the requirements.txt file instead — your teammates run pip install -r requirements.txt to get the same setup.
Option 2: pipenv — pip + venv Combined
pipenv was created to simplify the workflow of managing a virtual environment and tracking dependencies at the same time. Instead of a requirements.txt, it uses two files: Pipfile (human-readable) and Pipfile.lock (exact pinned versions for reproducibility).
Install pipenv
pipenv is not built into Python, so install it globally first:
pip install pipenv
You can see the output in the screenshot below.

Or on macOS with Homebrew:
brew install pipenv
Check out: Python While Multiple Conditions
Create an Environment and Install Packages
Navigate to your project folder and run:
pipenv install matplotlib
pipenv automatically:
- Creates a virtual environment for this project (stored in a centralized location, not inside your project folder)
- Creates a Pipfile and Pipfile.lock
- Installs Matplotlib into the environment
To install a development-only dependency (like a testing library):
pipenv install pytest --dev
Activate the pipenv Shell
pipenv shell
This drops you into a new shell session with the environment active. You’ll see the environment name in your prompt.
Run a Script Without Activating
pipenv run python your_script.py
You can see the output in the screenshot below.

I use this a lot — it runs the script in the environment without needing to activate it first.
Install From an Existing Pipfile
If you clone a project that already has a Pipfile:
pipenv install
To also install dev dependencies:
pipenv install --dev
View the Dependency Graph
One of my favorite pipenv features — it shows you not just what’s installed, but what depends on what:
pipenv graph
Check for Security Vulnerabilities
pipenv check
This checks your installed packages against known CVEs. A genuinely useful feature that plain venv doesn’t have.
Read: Add Elements to a List in Python using a For Loop
Exit the pipenv Shell
exit
When to Use pipenv
pipenv is a good fit if:
- You’re building a web application (Django, Flask)
- You want dependency locking built in (like package-lock.json in Node.js)
- You want the security scanning feature
- You work in a team and want a cleaner dependency management workflow
One honest note: pipenv has had a rocky history with slow updates and occasional bugs. For greenfield projects, some developers now prefer Poetry (a more modern alternative) for the same use case. But pipenv is still widely used, well-documented, and works well for most web projects.