Python pip Package Manager

In the last lesson, we learned about Python Virtual Environments. Now let us use pip to install and manage packages inside a project.

What Is pip in Python?

pip is a tool that comes with Python. It connects to the Python Package Index (PyPI) and lets us install packages from there.

PyPI is a huge repository of Python packages. There are hundreds of thousands of packages for almost anything you can think of.

How to Check if pip Is Installed

Most Python installations come with pip. We can check by running:

Terminal window
pip --version

Output

pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.10)

If we see a version number, pip is installed.

How to Install a Package with pip

To install a package, we use pip install.

Terminal window
pip install requests

That installs the requests package. We can then import it in our Python code.

import requests
response = requests.get("https://api.github.com")
print(response.status_code)

Output

200

That is how we install and use a package.

How to Install a Specific Version with pip

Sometimes we need a specific version of a package.

Terminal window
pip install requests==2.28.0

That installs version 2.28.0 of requests.

We can also install a minimum version.

Terminal window
pip install "requests>=2.28.0"

That installs version 2.28.0 or newer.

How to Upgrade a Package with pip

To upgrade a package to the latest version:

Terminal window
pip install --upgrade requests

Or shorter:

Terminal window
pip install -U requests

That updates the package to the newest version.

How to Uninstall a Package with pip

To remove a package:

Terminal window
pip uninstall requests

pip will ask for confirmation. Type y and press Enter.

How to List Installed Packages with pip

To see all installed packages:

Terminal window
pip list

Output (may vary):

Package Version
---------- -------
pip 23.0.1
requests 2.28.0
numpy 1.24.0

That shows all packages and their versions.

How to Save and Install from requirements.txt

We can save the list of installed packages to a file.

Terminal window
pip freeze > requirements.txt

That creates a file with all packages and their exact versions.

To install from that file:

Terminal window
pip install -r requirements.txt

That installs all the packages listed. This is very useful for sharing projects.

How to Search for Packages with pip

pip does not have a built-in search command anymore. But we can search on PyPI’s website: pypi.org.

We can also use pip index versions to see available versions of a package.

Terminal window
pip index versions requests

That lists all available versions.

A Real-Life Example – Setting Up a Project with pip

Let us create a new project and install some packages.

Terminal window
mkdir myproject
cd myproject
python -m venv venv

Activate the environment.

On Windows:

Terminal window
venv\Scripts\activate

On Mac or Linux:

Terminal window
source venv/bin/activate

Install packages.

Terminal window
pip install requests flask

Save requirements.

Terminal window
pip freeze > requirements.txt

Now the project is set up. Anyone can install the same packages using:

Terminal window
pip install -r requirements.txt

That is a standard workflow.

Common Mistakes with pip in Python

Mistake 1: Installing packages globally

If we install packages without a virtual environment, they go into the global Python. That can cause conflicts.

Fix: Always use a virtual environment for projects.

Mistake 2: Forgetting to activate the environment

If the environment is not active, pip install installs globally.

Fix: Activate the environment first.

Mistake 3: Using pip instead of pip3

On some systems, pip points to Python 2. We should use pip3 for Python 3.

Terminal window
pip3 install requests

Fix: Use pip3 if pip does not work.

Mistake 4: Not saving requirements

If we do not save requirements, others cannot reproduce our environment.

Fix: Run pip freeze > requirements.txt regularly.

Mistake 5: Installing packages that are already installed

Terminal window
pip install requests

If requests is already installed, this does nothing. It is not an error. But it is good to check first.

Terminal window
pip list | grep requests

That shows if the package is installed.

Next, we will learn about Python Iterators and process items one at a time using the iterator protocol.