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 installinto your system Python.
Why isolate?
Without isolation, every project shares one set of packages:
- Project A needs
django==3.2django==3.2, Project B needsdjango==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:
# Create a virtual environment in the .venv folder
python -m venv .venv# Create a virtual environment in the .venv folder
python -m venv .venvThis 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.
source .venv/bin/activatesource .venv/bin/activate.venv\Scripts\Activate.ps1.venv\Scripts\Activate.ps1Once active, your prompt usually shows (.venv)(.venv). To leave it:
deactivatedeactivateTip: add
.venv/.venv/to your.gitignore.gitignore— environments are rebuilt fromrequirements.txtrequirements.txt, not committed.
Installing packages with pip
With the environment active:
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 packagepip 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| Command | Does |
|---|---|
pip install NAMEpip install NAME | Install the latest version. |
pip install NAME==X.Ypip install NAME==X.Y | Install an exact version. |
pip install --upgrade NAMEpip install --upgrade NAME | Upgrade a package. |
pip uninstall NAMEpip uninstall NAME | Remove a package. |
pip listpip list | List installed packages. |
pip show NAMEpip show NAME | Show details about a package. |
requirements.txt — reproducible installs
Record exact versions so anyone can recreate the environment.
# Save the current environment's packages
pip freeze > requirements.txt
# Recreate it elsewhere (in a fresh venv)
pip install -r requirements.txt# Save the current environment's packages
pip freeze > requirements.txt
# Recreate it elsewhere (in a fresh venv)
pip install -r requirements.txtA requirements.txtrequirements.txt looks like:
requests==2.31.0
flask==3.0.0
python-dotenv==1.0.0requests==2.31.0
flask==3.0.0
python-dotenv==1.0.0A typical project workflow
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 ...
deactivatemkdir 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 ...
deactivateCommon pitfalls
- Forgetting to activate —
pip installpip installthen lands in global Python. Check your prompt for(.venv)(.venv). - Committing
.venv/.venv/— it’s large and machine-specific; commitrequirements.txtrequirements.txtinstead. - Mixing
pippipand the system package manager — inside a venv, always usepippip. pythonpythonvspython3python3— on some systems the command ispython3python3; the same goes forpippip/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/activateorActivate.ps1Activate.ps1),deactivatedeactivateto leave. - Use pip to
installinstall,upgradeupgrade,uninstalluninstall, andlistlistpackages. - Pin versions with
pip freeze > requirements.txtpip freeze > requirements.txtand restore withpip 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 coffeeWas this page helpful?
Let us know how we did
