Skip to content

Expert · Python in the real world · topic 48 of 50 · 25 min

Virtual environments and pip

Explanation

A package is ready-made code that someone else wrote and shared. pip downloads packages from PyPI, the Python Package Index. A virtual environment is a private folder of packages for one project, so two projects never fight over versions.

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.

bash
python -m pip install requests
python -m pip list
python -m pip uninstall requests
Install, list and remove a package. Writing python -m pip makes sure you use the pip of the right Python.

The 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.

Think of it like this: A virtual environment is like each student having their own geometry box instead of sharing one box for the whole class. If Ravi breaks his compass, Meena's box is not affected. Each project gets its own box of packages.

Creating and using a virtual environment

  1. Open a terminal inside your project folder.
  2. Create the environment: python -m venv .venv (this makes a folder named .venv).
  3. Activate it. Your prompt now starts with (.venv).
  4. Install packages with pip. They go into .venv only.
  5. When you finish, type deactivate.
bash
python -m venv .venv

# Windows (Command Prompt or PowerShell)
.venv\Scripts\activate

# macOS / Linux
source .venv/bin/activate

python -m pip install requests
deactivate
The same steps on Windows and on macOS/Linux.
Careful: On Windows PowerShell you may see "running scripts is disabled on this system" when you activate. Use Command Prompt instead, or ask your system administrator; do not change security settings you do not understand.

requirements.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.

bash
python -m pip freeze > requirements.txt

# on another computer, inside a new venv
python -m pip install -r requirements.txt
Save the list, then rebuild the same environment anywhere.
output
# requirements.txt
requests==2.32.3
Django==5.0.6
# comments start with #
Each line is name==version.
Tip: Never commit the .venv folder to Git. It is big and belongs to one computer. Commit requirements.txt instead and add .venv to your .gitignore file.
Tip: In the browser on this site you cannot pip install, and you do not need to: every task uses only the standard library.

Watch the lesson

Virtual environments and pip · 1/7

pip and virtual environments

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.

python
requirements = """requests==2.32.3
fastapi==0.111.0
"""
for line in requirements.splitlines():
    name, version = line.split("==")
    print(name, "version", version)
Reading a requirements list in plain Python.

Practice (0/3 done)

Task 1 · Code · Python

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.

Task 2 · Code · Python

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.

Task 3 · Practice

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

Question 1

1.What is PyPI?

Question 2

2.Why use a virtual environment?

Question 3

3.Which command saves the exact installed versions to a file?

Question 4

4.Which should you commit to Git?

Question 5

5.Where do you type pip install?

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.

← Previous: Stacks and queues