Expert · Python in the real world · topic 48 of 50 · 25 min
Virtual environments and pip
Explanation
Packages and PyPI
The standard library comes with Python. Everything else, like Django for websites or pandas for data, is a package. A package is a folder of Python code that you can install and import. PyPI (the Python Package Index, at pypi.org) is the free public store where hundreds of thousands of packages live.
pip is the tool that downloads a package from PyPI and installs it on your computer. pip comes with Python. You type pip commands in a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux), not inside a Python file.
python -m pip install requests
python -m pip list
python -m pip uninstall requestsThe problem: one computer, many projects
Imagine your college project needs Django 4 and your new internship project needs Django 5. If you install packages for the whole computer, only one version can be there at a time. One project breaks.
Creating and using a virtual environment
- Open a terminal inside your project folder.
- Create the environment: python -m venv .venv (this makes a folder named .venv).
- Activate it. Your prompt now starts with (.venv).
- Install packages with pip. They go into .venv only.
- When you finish, type deactivate.
python -m venv .venv
# Windows (Command Prompt or PowerShell)
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate
python -m pip install requests
deactivaterequirements.txt: a shopping list of packages
pip freeze prints every package installed in the environment, with its exact version. Save that list in a file named requirements.txt. Anyone (or any server) can then install the same versions with one command.
python -m pip freeze > requirements.txt
# on another computer, inside a new venv
python -m pip install -r requirements.txt# requirements.txt
requests==2.32.3
Django==5.0.6
# comments start with #Watch the lesson
Virtual environments and pip · 1/7
Real projects use packages written by other people. In this lesson you will learn how to install them safely.
Prefer a video by a teacher? Find free videos on “python virtual environment venv and pip tutorial” on YouTube ↗
In real life: Two college projects on one laptop
Sneha has a final-year project built with an older Django and a hackathon project using the latest FastAPI. She gives each folder its own .venv and its own requirements.txt. When her teammate Arjun clones the hackathon project, he runs two commands and has exactly the same packages, with no "it works on my laptop" problem.
requirements = """requests==2.32.3
fastapi==0.111.0
"""
for line in requirements.splitlines():
name, version = line.split("==")
print(name, "version", version)Practice (0/3 done)
A line of requirements.txt looks like "requests==2.32.3". Write a function parse_requirement(line) that returns a pair (name, version). If the line has no "==", return (name, None). Remove spaces around the line.
Write a function read_requirements(text) that takes the whole text of a requirements.txt file and returns a dict {name: version}. Skip empty lines and lines that start with #. Every other line has the form name==version.
On your own computer (not in the browser), make a folder called hello-venv, create a virtual environment in it, activate it, install the package "requests", and save requirements.txt.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.