Introduction
Sooner or later it happens: Project A needs requests 2.28, Project B needs 2.31, and pip happily breaks one while installing the other. Virtual environments are the fix — and the concept is simpler than the jargon suggests: each project gets its own private Python and its own private packages.
This guide explains what actually happens when you run those magic commands, so venvs stop being a ritual and start being a tool.
The Problem: One Global Python
By default, pip install puts packages into your system-wide Python. Every project shares that one pile of packages. Consequences:
A virtual environment is just a folder containing its own Python and its own `site-packages`. Activating it makes your terminal use that folder's Python. Nothing more mysterious than that.
Step 1: Create and Activate
cd my-project
python -m venv .venv
# Activate:
source .venv/bin/activate # macOS / Linux
.venv\Scripts\activate # WindowsYour prompt now shows (.venv). Proof it worked:
which python # macOS/Linux — points inside .venv
where python # Windows
pip install streamlitThe Streamlit you just installed went into .venv/lib/.../site-packages — invisible to every other project on your machine.
Step 2: requirements.txt — Your Project's Recipe
pip freeze > requirements.txtThis writes every installed package *with exact versions* (streamlit==1.41.1). Anyone — future you, a teammate, a deployment server — recreates the identical environment with:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtThis two-file pair (code + requirements.txt) is what makes a project portable. It's also what deployment platforms ask for — the Streamlit deployment guide depends entirely on this file.
Step 3: The Habits That Matter
.gitignore (see the Git guide); it's thousands of files of machine-specific binaries. requirements.txt *is* the portable form.pip freeze.Step 4: Everyday Management
deactivate # leave the venv
pip list # what's installed here
pip show streamlit # version + location of one package
pip install --upgrade streamlit # update one package
pip uninstall requests # remove oneRemoving a venv is just deleting the folder — it's self-contained by design. Recreating it is two commands plus one install, which is why venvs are disposable: don't clean them, replace them.
Common Errors & Fixes
which python and set your editor's interpreter to .venv/bin/python.Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser once.Key Concepts
What to Try Next
.venv, install its imports, freeze the requirements..venv/ to a global gitignore so you never think about it again.FAQ
Do I really need a venv for a tiny script?
Strictly no, practically yes — it costs ten seconds and builds the habit that saves you on the day packages conflict. Scripts that grow into projects never announce themselves in advance.
Where did the name 'venv' come from?
It's the standard-library module (python -m venv) since Python 3.3. Before that, virtualenv was a third-party package doing the same job — the name stuck as the generic term.
Can two projects share one venv?
Technically yes; practically never. The moment they need different versions of anything, you're back to the original problem — with extra confusion about which venv you're in.