Virtual Environments in Python (venv, pipenv, conda)

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.

Table of Contents

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.

Virtual Environments in Python

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.

Virtual Environments Python

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.

Python Virtual Environments

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.

Option 3: conda — For Data Science and Scientific Computing

conda is a different beast from venv and pipenv. It’s not just a Python environment manager — it’s a full package and environment manager that handles non-Python dependencies too. When you install NumPy with conda, it also handles the underlying C/Fortran libraries that NumPy depends on. That’s something pip can’t do.

conda comes with either Anaconda (the full distribution, includes 250+ packages) or Miniconda (minimal install, just conda itself — my preference).

Check out: Add Two Numbers in Python Without Using Arithmetic Operators

Create a conda Environment

conda create -n myproject python=3.12

This creates an environment named myproject with Python 3.12. You can also specify packages at creation time:

conda create -n datascience python=3.12 numpy pandas matplotlib

Activate a conda Environment

conda activate myproject

You’ll see (myproject) in your prompt.

Install Packages in a conda Environment

From the default channel:

conda install matplotlib

From conda-forge (recommended — more up to date):

conda install -c conda-forge matplotlib

You can also use pip inside a conda environment when a package isn’t available on conda:

pip install some-package

Just use conda first whenever possible, and pip as a fallback. Mixing them heavily can sometimes cause dependency issues.

List All Your conda Environments

conda env list

This shows every environment you’ve created, with their paths. conda stores environments centrally (not inside your project folder), so you can easily see and manage all of them.

Export and Sharing a conda Environment

conda env export > environment.yml

For better portability across operating systems (without platform-specific build numbers):

conda env export --no-builds > environment.yml

Recreate the environment from this file:

conda env create -f environment.yml

Deactivate

conda deactivate

Delete a conda Environment

conda env remove -n myproject

Read: Find the Sum of Two Numbers without Using Arithmetic Operators in Python

When to Use conda

conda is the right choice when:

  • You’re doing data science, machine learning, or scientific computing
  • You need libraries like NumPy, SciPy, or TensorFlow that have complex non-Python dependencies
  • You need to switch between Python versions easily across projects
  • You’re on Windows and want the smoothest experience with scientific packages
  • You work with Jupyter Notebooks regularly

venv vs pipenv vs conda — Side by Side

venvpipenvconda
Built into Python✅ Yes❌ No❌ No
Manages Python version❌ No❌ No✅ Yes
Handles non-Python dependencies❌ No❌ No✅ Yes
Dependency lockingManual (pip freeze)✅ Pipfile.lock✅ environment.yml
Security vulnerability scanning❌ No✅ Yes❌ No
Best forGeneral projects, beginnersWeb appsData science, ML
Environment storedInside project folderCentralizedCentralized
Learning curveLowMediumMedium

Check out: Increment and Decrement Operators in Python

Best Practices I Follow for Every Project

These habits have saved me a lot of headaches over time:

  • Always create a virtual environment before installing anything — even for small, quick scripts. It costs 10 seconds and prevents future confusion.
  • Name your environment consistently — I use venv for venv projects and descriptive names (like project-name-env) for conda environments.
  • Always add your environment folder to .gitignore — never commit venv/ or .venv/ to version control.
  • Commit your requirements.txt or Pipfile or environment.yml — this is what your teammates and your future self will use to recreate the environment.
  • Document the Python version your project needs — either in a README.md or a .python-version file. Don’t make people guess.
  • One environment per project — don’t share environments between projects. When things break, you’ll thank yourself for the isolation.
  • Pin your dependency versions for production — pip freeze > requirements.txt captures exact versions. This prevents “it worked last week” problems when a dependency gets a breaking update.

How Virtual Environments Work in VS Code

VS Code has excellent virtual environment support. When you open a project folder that contains a venv folder or .venv folder, VS Code usually detects it automatically and prompts you to use it as the Python interpreter.

If it doesn’t auto-detect:

  1. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
  2. Type Python: Select Interpreter
  3. Choose the interpreter inside your venv folder — it’ll show the path

Once selected, the integrated terminal in VS Code will automatically activate your environment when you open it. Very convenient.

VS Code tip: Name your environment venv (not .venv). VS Code sometimes struggles to locate interpreters when the folder name starts with a dot.

How Virtual Environments Work in PyCharm

PyCharm has even tighter virtual environment integration. When you create a new project in PyCharm, it gives you the option to create a new virtual environment right from the project creation dialog.

To set or change the interpreter for an existing project:

  1. Go to File > Settings (Windows/Linux) or PyCharm > Preferences (Mac)
  2. Navigate to Project > Python Interpreter
  3. Click the gear icon → Add Interpreter
  4. Choose Existing environment and point to your venv/bin/python (Mac/Linux) or venv\Scripts\python.exe (Windows)

PyCharm also lets you create conda environments directly from this settings panel, which is handy.

Common Questions

Do I need a virtual environment for every Python project?

I’d say yes — get into the habit for everything, including small scripts. The overhead is tiny (one command to create, one to activate) and it prevents a class of problems that are genuinely annoying to debug.

Can I use multiple virtual environments at the same time?

Not in the same terminal session — only one environment can be active at a time. But you can open multiple terminal windows, each with a different environment activated.

What happens if I forget to activate my environment before installing a package?

The package gets installed globally — into your system Python. Run pip uninstall <package> globally to clean it up, then activate your environment and reinstall there.

My terminal doesn’t show (venv) after activation. What’s wrong?

On Windows, your PowerShell execution policy might be blocking the activation script. Run:
bashSet-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Then try activating again.

Should I use venv or conda for machine learning projects?

conda. Setting up TensorFlow, PyTorch, or CUDA-dependent packages is significantly smoother with conda because it handles the underlying C/CUDA dependencies that pip struggles with.

Can I convert a requirements.txt to a conda environment.yml?

Not directly, but you can create a conda environment and then install from requirements.txt using pip inside it:
bashconda create -n myenv python=3.12
conda activate myenv
pip install -r requirements.txt

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.