Python pip Package Manager
Table of Contents + −
In the last lesson you set up Virtual Environments, so each project gets its own clean space. Now you have that space. But it is empty. So how do you put useful code into it? That is what pip does. It is the tool that downloads packages other people wrote and installs them into your project.
🤔 Why pip?
Say you want to fetch a web page in Python. You could write all the networking code yourself. That is a lot of work. And people far smarter than us already solved it years ago.
Here is the pain. Useful code lives in thousands of separate packages out on the internet, not in Python itself. You need a way to grab the one you want and put it into your project. pip is that way. You type one command. It downloads the package. And now you can import it.
A package is just a bundle of Python code someone published so others can reuse it. Think of requests for talking to websites. Or pandas for working with data. You did not write them. But you get to use them.
📦 Where the packages come from
When you run pip, where does it get the code? From a big public library called PyPI, the Python Package Index. It is a website that stores hundreds of thousands of packages, all free to download.
Here is the simple picture:
- You ask pip for a package by name.
- pip goes to PyPI and finds it.
- pip downloads it and installs it into your environment.
So PyPI is the warehouse. pip is the delivery person who brings the package to your door.
Tip
pip usually comes already installed with Python. You can check by running pip --version in your terminal. If that prints a version number, you are ready to go.
⬇️ Installing a package
The command to install a package is pip install followed by the package name. Let us install requests, a popular package for fetching things from the web.
Run this in your terminal, not inside the Python prompt:
pip install requestsYou will see pip download the package. Then a few lines tell you it is collecting and installing things. When it finishes, the package is ready.
Output
Collecting requests Downloading requests-2.32.3-py3-none-any.whl (64 kB)Installing collected packages: requestsSuccessfully installed requests-2.32.3Now you can use it in your code. This short script asks a website for a page. Then it prints back the status code, which is just a number saying whether the request worked.
import requests
response = requests.get("https://www.python.org")print(response.status_code)Run that, and you get:
Output
200The number 200 means “OK, the page was found”. You just used code you never wrote, in two simple steps. Install with pip. Then import.
If you want a specific version of a package, add == and the version number. This is handy when a newer version changed something and you need the older behaviour.
pip install requests==2.31.0📋 Seeing what you have installed
After a while you forget what is installed. The pip list command shows you every package in the current environment, along with its version.
pip listYou get a tidy table like this:
Output
Package Version------------------ ---------certifi 2024.7.4charset-normalizer 3.3.2idna 3.7pip 24.0requests 2.32.3urllib3 2.2.2Notice you only asked for requests. But the list has more. That is normal. Some packages need other packages to work, and pip installs those helpers for you automatically. Those helpers are called dependencies.
🗑️ Removing a package
When you no longer need a package, remove it with pip uninstall and the name. pip asks you to confirm by typing y.
pip uninstall requestsOutput
Found existing installation: requests 2.32.3Uninstalling requests-2.32.3: Would remove: .../site-packages/requests/*Proceed (Y/n)? y Successfully uninstalled requests-2.32.3Caution
pip uninstall requests removes only requests itself. The dependencies it brought along, like certifi and urllib3, stay behind. That is on purpose, since another package might still need them.
🔒 Sharing your project’s packages
Here is a real problem. You build a project on your laptop and install five packages. Then a teammate, Riya, downloads your project. Her machine has none of those packages. How does she install the exact same set without you reading them out one by one?
The answer is a file called requirements.txt. It is a plain list of every package your project needs, with the versions pinned. You make it once. You share it. And anyone can install the whole set in one command.
First, you create the file. The pip freeze command prints your installed packages in the right format. The > symbol saves that output into a file.
pip freeze > requirements.txtOpen the file and it looks like this. Each line is a package and its exact version:
Output
certifi==2024.7.4charset-normalizer==3.3.2idna==3.7requests==2.32.3urllib3==2.2.2Now Riya gets your project with that file inside it. She runs one command. pip reads the file and installs every package listed, at the exact versions. The -r flag means “read the requirements from this file”.
pip install -r requirements.txtBoth of you now have the same packages at the same versions. So the code that runs on your machine runs the same way on hers. No surprises.
Tip
Commit requirements.txt to your project. It is a small text file. And it tells anyone, including future you, exactly what the project needs to run.
🧪 pip and virtual environments
Remember the virtual environment from the last lesson? pip and virtual environments work as a team.
When you activate a virtual environment first, then run pip install, the package goes into that environment only. It does not touch the rest of your system. So each project keeps its own packages at its own versions.
Here is the usual flow on macOS or Linux:
python -m venv venvsource venv/bin/activatepip install requestsOn Windows the activate step is slightly different:
python -m venv venvvenv\Scripts\activatepip install requestsOnce the environment is active, every pip command works inside that environment. That includes install, list, freeze, and uninstall.
Note
A quick way to tell if your environment is active: most terminals show the environment name in brackets at the start of the line, like (venv). If you see that, pip is installing into the right place.
⚠️ Common Mistakes
A few traps catch almost everyone at the start:
- Forgetting to activate the virtual environment first, so the package lands in the wrong place. Activate, then install.
- Running pip commands inside the Python prompt. pip is a terminal command, not Python code. Run it where you would run
python, not after starting Python. - Confusing the import name with the install name. They are usually the same, but not always. You install
beautifulsoup4but importbs4.
# ❌ Avoid: running this inside the python >>> prompt>>> pip install requests
# ✅ Good: run it in the plain terminalpip install requestsAnother common slip is forgetting to update your requirements file after adding a package.
# ❌ Avoid: installing a new package and never recording itpip install pandas
# ✅ Good: install, then refresh the requirements filepip install pandaspip freeze > requirements.txt✅ Best Practices
Small habits that keep your projects clean and easy to share:
- Always work inside a virtual environment, one per project. It keeps packages from clashing.
- Pin versions in
requirements.txtso everyone installs the same thing.pip freezedoes this for you. - Run
pip freeze > requirements.txtagain whenever you add or remove a package, so the file stays correct. - Use
pip listnow and then to see what is actually installed. - Only install packages you trust. Anyone can publish to PyPI, so stick to well-known, widely used ones.
🧩 What You’ve Learned
A quick recap of what you can now do:
- ✅ Explain that pip installs packages from PyPI, the public library of Python code.
- ✅ Install a package with
pip install requestsand use it withimport. - ✅ See your installed packages with
pip listand remove one withpip uninstall. - ✅ Save your project’s packages with
pip freeze > requirements.txt. - ✅ Recreate the same setup anywhere with
pip install -r requirements.txt. - ✅ Run pip inside an activated virtual environment so packages stay per project.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is PyPI?
Why: PyPI, the Python Package Index, is the public warehouse of packages that pip downloads from.
- 2
Which command saves your installed packages and their versions into a file?
Why: pip freeze prints packages with pinned versions, and > saves that output into requirements.txt.
- 3
What does pip install -r requirements.txt do?
Why: The -r flag tells pip to read the file and install each package it lists.
- 4
Why activate a virtual environment before running pip install?
Why: With the environment active, pip installs into that environment, keeping each project's packages separate.
🚀 What’s Next?
You can now bring outside code into any project and share your setup with anyone. Next we look at how Python loops through data one item at a time, which unlocks a lot of how the language works underneath.