Pip is Python’s package installer. If you’re wondering what Pip stands for: the name Pip is a recursive acronym, which stands for ‘Pip Installs Packages’. There are two ways of installing Python packages using pip:
- Manual installation
- Using a
requirements.txtfile that defines the required packages and their version numbers.
But before we start working with Pip, we must first make sure it’s installed!
Make sure it’s needed
Good news; chances are that Pip is already installed on your system. Most Python installers also install Pip. Pip is already installed if you are using Python 2 >=2.7.9 or Python 3 >=3.4 downloaded from python.org. If you are working in a virtual environment, pip is also installed for you.
Some people still end up without Pip on their system, so let’s first take a look at how you can manually install Pip. Before you try to install Pip, make sure it’s not installed. Open a terminal (Linux/MacOS) or a Windows shell, and type in the following command:
pip command gives an error, try
pip3 instead. On some systems, Python 2 and 3 can be installed next to each other. On those systems, pip is often installed under the name
If that didn’t work either, you can try the pip module that is build into most modern Python installations:
python3 -m pip help
If that failed too, you need to install pip.
Install Pip on Windows and MacOS
On Windows and MacOS, you can download a Python script to install pip, called get-pip.py. Download the file and run it with Python from a command prompt or terminal window:
Make sure you are in the directory where the script was downloaded.
Install Pip on Linux
You can install pip with the apt package manager on Debian, Ubuntu, Linux Mint, and other Debian derivatives. It’s the most recommended method and ensures your system will stay in a consistent state.
$ sudo apt install python3-pip
If you system uses the yum package manager, pip can often be installed with:
$ yum -y install python-pip
Pip is part of EPEL (Extra Packages for Enterprise Linux), so you might need to enable that first.
If these methods fail, you can also download a Python script that will install pip for you, with the following commands:
$ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" .... $ python3 get-pip.py
Install Python packages with Pip
We can use pip to install packages inside our newly created environment. Pip is installed inside your virtual environment by default. Because everything in our venv is installed locally, you don’t need to become a superuser with
Alternatively, you can install packages outside of your venv as well. This is only recommended for very generic packages, like pip itself. In fact, let’s try to upgrade our system-wide pip installation first. Make sure you are not currently in a virtual environment and enter:
pip3 install --upgrade pip
This command asks
pip to install
pip, and update it if it’s already installed.
Now that we’re up-to-date, let’s try to install
simplejson. Enter your virtual environment and type in:
$ pip install simplejson
Install Python packages using a requirements.txt file
In a virtual environment, it’s a good habit to install specific versions of packages. It ensures that you reap the full benefits of using virtual environments in the first place. After all, we do this to make sure our software always works as intended by pinning down specific dependency versions.
requirements.txt file contains a very simple list of dependencies, one per line. In its most simple form, it could look like this:
But what we really want is to pin the versions. That’s not hard either:
You can also relax these constraints a little, by using >= and <=, or even a combination of those:
How do you know what range to use? Unfortunately, there are no rules to this. You will have to read the release notes and such from the package in question.
You can make your life a little easier by creating your requirements file using pip’s freeze option. First, write your software and install all the requirements you need as you go with pip. Once you’re done, use the following command:
$ pip freeze > requirements.txt
Pip will create a requirements file with all the currently installed dependencies, including version numbers. Neat!
Finally, to install all the dependencies listed in this file, use:
$ pip install -r requirements.txt
If you want to learn more about pip and all its options, head over to the official pip documentation.