Skip to content

Python Virtual Environments & pip

A virtual environment is an isolated Python installation for a single project. It keeps each project’s packages separate so versions never clash, and it keeps your global Python clean. pip is the tool that installs and manages those packages. (For installing Python itself, see the Installation page.)

Golden rule: one virtual environment per project, and never pip installpip install into your system Python.

Why isolate?

Without isolation, every project shares one set of packages:

  • Project A needs django==3.2django==3.2, Project B needs django==4.2django==4.2 — they can’t coexist globally.
  • A global install can break system tools that rely on specific versions.
  • You can’t reproduce “exactly what this project needs” for a teammate or server.

A virtual environment solves all three.

Creating an environment with venv

venvvenv ships with Python. Create an environment (commonly named .venv.venv) in your project folder:

terminal
# Create a virtual environment in the .venv folder
python -m venv .venv
terminal
# Create a virtual environment in the .venv folder
python -m venv .venv

This makes a .venv/.venv/ directory containing a private copy of Python and a place for packages.

Activating and deactivating

You must activate the environment so pythonpython and pippip point at it.

macOS / Linux
source .venv/bin/activate
macOS / Linux
source .venv/bin/activate
Windows (PowerShell)
.venv\Scripts\Activate.ps1
Windows (PowerShell)
.venv\Scripts\Activate.ps1

Once active, your prompt usually shows (.venv)(.venv). To leave it:

terminal
deactivate
terminal
deactivate

Tip: add .venv/.venv/ to your .gitignore.gitignore — environments are rebuilt from requirements.txtrequirements.txt, not committed.

Installing packages with pip

With the environment active:

terminal
pip install requests              # latest version
pip install "django==4.2"         # a specific version
pip install "flask>=2,<3"         # a version range
pip install --upgrade requests    # upgrade to the newest
pip uninstall requests            # remove a package
terminal
pip install requests              # latest version
pip install "django==4.2"         # a specific version
pip install "flask>=2,<3"         # a version range
pip install --upgrade requests    # upgrade to the newest
pip uninstall requests            # remove a package
CommandDoes
pip install NAMEpip install NAMEInstall the latest version.
pip install NAME==X.Ypip install NAME==X.YInstall an exact version.
pip install --upgrade NAMEpip install --upgrade NAMEUpgrade a package.
pip uninstall NAMEpip uninstall NAMERemove a package.
pip listpip listList installed packages.
pip show NAMEpip show NAMEShow details about a package.

requirements.txt — reproducible installs

Record exact versions so anyone can recreate the environment.

terminal
# Save the current environment's packages
pip freeze > requirements.txt
 
# Recreate it elsewhere (in a fresh venv)
pip install -r requirements.txt
terminal
# Save the current environment's packages
pip freeze > requirements.txt
 
# Recreate it elsewhere (in a fresh venv)
pip install -r requirements.txt

A requirements.txtrequirements.txt looks like:

requirements.txt
requests==2.31.0
flask==3.0.0
python-dotenv==1.0.0
requirements.txt
requests==2.31.0
flask==3.0.0
python-dotenv==1.0.0

A typical project workflow

terminal
mkdir myproject && cd myproject
python -m venv .venv
source .venv/bin/activate          # (Windows: .venv\Scripts\Activate.ps1)
pip install requests flask
pip freeze > requirements.txt
# ... write code ...
deactivate
terminal
mkdir myproject && cd myproject
python -m venv .venv
source .venv/bin/activate          # (Windows: .venv\Scripts\Activate.ps1)
pip install requests flask
pip freeze > requirements.txt
# ... write code ...
deactivate

Common pitfalls

  • Forgetting to activatepip installpip install then lands in global Python. Check your prompt for (.venv)(.venv).
  • Committing .venv/.venv/ — it’s large and machine-specific; commit requirements.txtrequirements.txt instead.
  • Mixing pippip and the system package manager — inside a venv, always use pippip.
  • pythonpython vs python3python3 — on some systems the command is python3python3; the same goes for pippip/pip3pip3.

Practice Exercises

These run plain Python that mirrors what the tools do under the hood.

Exercise 1 – Check the running Python version

Exercise 2 – Parse a requirements line

Exercise 3 – Build a pip install command

Summary

  • A virtual environment isolates one project’s packages; create it with python -m venv .venvpython -m venv .venv.
  • Activate it before working (source .venv/bin/activatesource .venv/bin/activate or Activate.ps1Activate.ps1), deactivatedeactivate to leave.
  • Use pip to installinstall, upgradeupgrade, uninstalluninstall, and listlist packages.
  • Pin versions with pip freeze > requirements.txtpip freeze > requirements.txt and restore with pip install -r requirements.txtpip install -r requirements.txt.
  • Commit requirements.txtrequirements.txt, not the .venv/.venv/ folder.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did