How To Use Python pip (and Why You Need To)
Python is one of the best languages for learning the ins and outs of programming. Not only is the language easy to learn, it’s efficient, powerful and has plenty of tools to make your life easier.
One such tool is pip. What is pip (besides a really fun word to say)? pip is the Python package manager that allows you to install and manage Python software packages. pip connects to an online repository of packages called the Python Package Index (PyPi), which is where all relevant Python packages are housed.
Think of pip like apt or dnf for Python. Not only does it install the package you want but also the required dependencies for said package. Without pip, you would have a far more challenging time expanding Python beyond what it includes out of the box.
To that end, I want to show you how to install and use Python’s pip. Once you know how to use this package manager, the sky’s the limit for what you can do with Python.
Installing pip
Installing Python pip is quite easy and can be done from the standard repositories of most Linux distributions.
On Ubuntu-based distributions, the installation command would be:
|
1 |
sudo apt-get install python3-pip -y |
On Fedora-based distributions, the command is:
|
1 |
sudo dnf install python-pip -y |
If you’re using macOS, you can install pip with the following command:
|
1 |
curl https://bootstrap.pypa.io/get-pip.py | python |
If you have Python installed as Python3, the command would be:
|
1 |
curl https://bootstrap.pypa.io/get-pip.py | python3 |
For Windows, download the standalone ZIP application and use it with any supported version of Python.
As soon as you install it, upgrade it with the command:
|
1 |
pip install --upgrade pip |
It seems odd that you’d upgrade pip with pip, but that’s how it goes. You might run into instances where pip will fail to function properly because it requires an upgrade. That’s why the first thing I do is upgrade pip immediately after every pip installation.
Before continuing, there’s something important you’ll want to consider. If you install pip on Linux in the above fashion, you might run into several instances where applications can only be installed with pip using sudo, which can lead to possible security issues. To get around that, you’ll want to follow the following instructions:
- Download pip with the command
wget https://bootstrap.pypa.io/get-pip.py. - Install the downloaded package into a local directory (such as
~/.local/bin) withpython3 get-pip.py --user. - Add
.local/binto your$PATHby adding the following to the bottom of your~/.bashrcfile:export PATH="$HOME/.local/bin:$PATH". (WhereUSERis your Linux username). - Source the
.bashrcfile withsource ~/.bashrc.
With that taken care of, you should be able to use pip without resorting to sudo.
Once you have it installed, what can you do with pip? Let’s find out.
Using pip
There are two ways to install packages with pip, directly from the command line or using a requirements.txt file. Let’s examine the command line usage first.
Say, for example, you want to install the PyInstaller package, which bundles a Python application and all of its dependencies into a single package. From the command line, that installation would look like this:
|
1 |
pip install -U pyinstaller |
The -U option informs pip to also upgrade the package to the newest available version.
The next method is to install packages from the requirements.txt file. When you use this method, you can create a list of packages to be installed and then install them all at once with a single command. This method is far easier, especially when you need to install several packages in multiple environments (or machines), so you don’t have to go through the process of installing the packages one by one.
First, create the requirements.txt file with:
|
1 |
nano requirements.txt |
In that file, add each package you want to install on individual lines like this:
|
1 2 3 |
pyinstaller simplejson numpy |
Save and close the file.
Run the installation with:
|
1 |
pip install -r requirements.txt |
Every package in the file will be installed and ready to go. You can also install specific releases of packages this way. For example, say you want to install simplejson version 3.18.4. For that, the entry would be:
|
1 |
simplejson==3.18.4 |
You could use the following specifiers:
==to install a specific release.>to install a release greater than what is specified.>=to install a release greater than or equal to what is specified.<=to install a release less than or equal to what is specified.
Finally, you can list out all packages installed by pip with one of the following commands:
|
1 |
pip freeze |
or
|
1 |
pip list |
Using pip in Virtual Environments
You can also use pip in virtual environments, which is always a safe route because it means you install those packages within an isolated environment. When doing this, everything goes away when you destroy the virtual environment (so there’s no risk of harming your operating system).
First create a new virtual environment with the command:
|
1 |
python3 -m venv .venv |
The above command creates a virtual environment called .venv. You then must activate the environment with the command:
|
1 |
source .venv/bin/activate |
Your terminal prompt will now begin with (.venv) to indicate you’re using it. You can then install any Python application with pip in the same way you did above (even using requirements.txt files).
Once you’re done with that environment, deactivate it with:
|
1 |
deactivate |
Remember, when you install a package in a virtual environment, it is not installed on your regular Python environment.
And that’s how you get started with the Python pip package manager. Learn more about this handy tool by reading the man page with the command:
|
1 |
man pip |