Python pip Package Manager
Table of Contents
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:
pip --versionOutput
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.
pip install requestsThat 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
200That 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.
pip install requests==2.28.0That installs version 2.28.0 of requests.
We can also install a minimum version.
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:
pip install --upgrade requestsOr shorter:
pip install -U requestsThat updates the package to the newest version.
How to Uninstall a Package with pip
To remove a package:
pip uninstall requestspip will ask for confirmation. Type y and press Enter.
How to List Installed Packages with pip
To see all installed packages:
pip listOutput (may vary):
Package Version---------- -------pip 23.0.1requests 2.28.0numpy 1.24.0That 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.
pip freeze > requirements.txtThat creates a file with all packages and their exact versions.
To install from that file:
pip install -r requirements.txtThat 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.
pip index versions requestsThat lists all available versions.
A Real-Life Example – Setting Up a Project with pip
Let us create a new project and install some packages.
mkdir myprojectcd myprojectpython -m venv venvActivate the environment.
On Windows:
venv\Scripts\activateOn Mac or Linux:
source venv/bin/activateInstall packages.
pip install requests flaskSave requirements.
pip freeze > requirements.txtNow the project is set up. Anyone can install the same packages using:
pip install -r requirements.txtThat 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.
pip3 install requestsFix: 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
pip install requestsIf requests is already installed, this does nothing. It is not an error. But it is good to check first.
pip list | grep requestsThat shows if the package is installed.
Next, we will learn about Python Iterators and process items one at a time using the iterator protocol.