You install a package for one project, and another project breaks. Or a tutorial says pip install and your terminal answers with “externally-managed-environment.” Both problems have the same fix: a Python virtual environment, a private folder of packages for each project.
Python gives you three popular ways to make one: the built-in venv, Astral’s uv and conda. This tutorial shows how each works on Windows, macOS and Linux, and when to pick which. We also timed venv on a Windows PC, and the result explains why people say it’s slow.
Quick answer: A Python virtual environment is a folder with its own Python and packages, so projects don’t clash. Use python -m venv .venv if you want zero extra installs, uv venv if you want speed and a lockfile, and conda if you need non-Python libraries such as CUDA or GDAL. Don’t commit the folder to Git, and recreate it rather than copying it.
- On our Windows test PC,
python -m venvtook a median 18.47 seconds, but only 0.20 seconds with--without-pip. - The slow part is installing pip, not creating the environment.
- VS Code finds any
.venvfolder in your project on its own.
What is a Python virtual environment?
It’s a directory that holds a link to a Python interpreter plus its own site-packages folder. When the environment is active, pip install puts packages there instead of into your system Python. Delete the folder and everything inside it is gone, with nothing left behind.
Here’s what python -m venv .venv created on our Windows machine with Python 3.12.4:
.venv\
Include\
Lib\site-packages\
Scripts\ (activate, activate.bat, Activate.ps1, python.exe, pip.exe ...)
pyvenv.cfg
# pyvenv.cfg
home = C:\Python312
include-system-site-packages = false
version = 3.12.4
How a Python virtual environment works
The pyvenv.cfg file is the trick. When Python starts and finds it next to the interpreter, it treats that folder as its prefix and reads packages from there. The home line points back to the Python you used to create it, which is why the environment is small: it doesn’t copy the whole standard library.
You can check whether you’re inside one from Python itself. The official venv docs say comparing sys.prefix with sys.base_prefix is enough:
import sys
print(sys.prefix != sys.base_prefix) # True inside a venv
We ran that in our new environment and it printed True, with sys.base_prefix showing C:\Python312.
Option 1: venv, built into Python
Nothing to install. If you have Python 3, you have venv. Run this in your project folder:
python -m venv .venv
On macOS and many Linux distros the command is python3. Debian and Ubuntu sometimes need the python3-venv package first.
Python virtual environment activate commands
| System and shell | Command |
|---|---|
| Windows, PowerShell | .venv\Scripts\Activate.ps1 |
| Windows, cmd.exe | .venv\Scripts\activate.bat |
| Windows, Git Bash | source .venv/Scripts/activate |
| macOS / Linux, bash or zsh | source .venv/bin/activate |
| macOS / Linux, fish | source .venv/bin/activate.fish |
Type deactivate to leave. Your prompt usually shows (.venv) while it’s active.
Install packages and save them
pip install requests
pip freeze > requirements.txt # save exact versions
pip install -r requirements.txt # rebuild on another machine
The requirements.txt file is what you share and commit. The .venv folder isn’t.
Option 2: uv, the fast one
uv is a package and project manager from Astral, the company behind the Ruff linter. It’s written in Rust, and its docs claim it’s “10-100x faster” than pip. It can also download Python versions for you, which venv can’t.
# Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
# macOS / Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
uv venv # creates .venv
uv venv --python 3.11 # with a specific Python version
uv pip install requests # pip-style install into .venv
You activate a uv environment the same way as a venv one, because it’s the same kind of folder. Most uv users don’t bother, though. The project commands handle it:
uv init my-app
cd my-app
uv add requests # updates pyproject.toml and the lockfile
uv run main.py # runs inside the project's environment
uv isn’t installed on our test PC, and we didn’t install it system-wide just for this article, so we haven’t timed it ourselves. The speed claim above is Astral’s.
Option 3: conda, for more than Python
Conda manages environments and packages that aren’t Python at all. Its environment docs even show Node.js inside an environment file. That’s why data scientists like it: compiled libraries for GPUs, geospatial work or scientific computing come pre-built.
conda create -n myenv python=3.12
conda activate myenv
conda env list
conda env export > environment.yml
conda deactivate
conda env remove --name myenv
Conda environments live in a central folder, not in your project, and you activate them by name. The docs warn that “issues may arise when using pip and conda together,” and suggest installing with conda first, then pip only for whatever’s left.
venv vs uv vs conda: which to use
| venv | uv | conda | |
|---|---|---|---|
| Install needed | No, built in | Yes, one command | Yes (Miniconda or similar) |
| Installs Python versions | No | Yes | Yes |
| Non-Python packages | No | No | Yes |
| Lockfile | No (pip freeze) | Yes | environment.yml export |
| Env location | Your project | Your project | Central folder |
| Best for | Learning, small scripts | Most new projects | Data science with C/GPU libraries |

Our pick for most people starting a new project today is uv. If you’re learning Python or working on a locked-down machine where you can’t install tools, plain venv is perfectly fine. Pick conda when a package’s install instructions tell you to, which mostly happens in machine learning and scientific work.
And what about a Python virtual environment vs Docker? They solve different problems. A venv isolates Python packages. Docker isolates the whole operating system, including system libraries. Plenty of projects use a venv inside a Docker image.
We timed venv on Windows
People often say venv is slow. We measured it. A small Python script created a fresh environment seven times with the default settings and seven times with --without-pip, timed each run with time.perf_counter, then deleted it.
Machine: Windows 10 (build 19045), Python 3.12.4, a 4-thread Intel CPU, with normal background software running. Results:
- Default (with pip): median 18.47 s, fastest 12.87 s, slowest 24.39 s, 14.8 MB on disk
- –without-pip: median 0.20 s, fastest 0.19 s, slowest 0.27 s, 0.6 MB on disk

So about 99% of the wait was installing pip into the new environment, which venv does by default. Creating the environment itself took a fifth of a second. Windows antivirus scanning the new files probably adds to the pip step, but we didn’t test with it turned off, so we can’t say how much. The script and raw timings are saved with this post.
The part most tutorials miss: a big share of uv’s “instant” environment comes from not installing pip at all. uv’s CLI reference lists a --seed flag for adding pip, so a plain uv venv leaves it out. That isn’t cheating, since uv installs packages itself and doesn’t need pip. But it means you can get most of that creation speed from plain python -m venv --without-pip .venv, as long as you then install packages with a tool that doesn’t need pip inside the environment.
Python virtual environment in VS Code
According to the VS Code Python docs, the Python extension searches your workspace for any folder named .venv and prefers it over global interpreters. Name your environment .venv and it usually just works.
- Open the Command Palette (Ctrl+Shift+P, or Cmd+Shift+P on a Mac).
- Run Python: Create Environment to make a venv or conda environment, or Python: Select Interpreter to pick one you already have.
- Open a new terminal in VS Code. It normally activates the selected environment for you.
If uv is installed, the docs say the extension uses it automatically when creating venvs and installing packages. It’s also one of the picks in our list of the best VS Code extensions for 2026, next to Ruff for linting and formatting.
Common errors and fixes
“Activate.ps1 cannot be loaded because running scripts is disabled.” That’s PowerShell’s execution policy. The Python docs give this fix, which only affects your user account:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Packages “disappear” after you moved the project folder. Environments aren’t portable. The venv docs say to recreate one in the new location and delete the old one. Keep requirements.txt (or uv.lock) and rebuilding takes one command.
pip installs to the wrong place. You forgot to activate, or you’re calling a different Python. Running python -m pip instead of plain pip makes sure pip matches the interpreter you’re using.
Git shows thousands of new files. Add .venv/ to your .gitignore. The Python docs say environments aren’t meant to be checked into source control. Already committed it? Run git rm -r --cached .venv, then commit again. Our list of Git commands every developer should know covers the everyday commands around that.
Python virtual environment best practices
- One environment per project, stored in the project as
.venv. - Treat it as disposable. If it gets weird, delete it and rebuild from your requirements file.
- Pin versions for anything you deploy.
- Never install project packages into your system Python.
Once your environment’s set up, the fun part is the code. Our Python tricks for beginners and the guide to f-string tricks are good next reads.
Verdict
Use a virtual environment for every project, whichever tool makes it. For a brand-new project in late 2026, uv is the one we’d pick: fast, handles Python versions and keeps a lockfile. Stick with venv when you can’t install anything or you’re teaching beginners, since it’s the one tool every Python already has. Choose conda only when your libraries need it.
Next: put your new environment to work with 15 Python tricks for beginners.
FAQ
Which Python virtual environment should I use?
uv for most new projects, the built-in venv if you can’t install tools, and conda if you need compiled non-Python libraries such as CUDA.
Can a Python virtual environment be copied?
Not reliably. The Python docs describe environments as not movable or copyable. Copy your requirements.txt or lockfile instead and recreate the environment on the new machine.
How do I create a Python virtual environment on Windows?
Run python -m venv .venv in your project folder, then .venv\Scripts\Activate.ps1 in PowerShell or .venv\Scripts\activate.bat in cmd.
Why is my venv so slow to create?
In our test it was pip. The default run took a median 18.47 seconds on Windows, and the same command with --without-pip took 0.20 seconds.