TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Python / Software Development

How To Use Python pip (and Why You Need To)

This tutorial will help you to become familiar with the pip package management system for Python.
Sep 14th, 2024 6:05am by
Featued image for: How To Use Python pip (and Why You Need To)
Featured image via Unsplash+.

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:


On Fedora-based distributions, the command is:


If you’re using macOS, you can install pip with the following command:


If you have Python installed as Python3, the command would be:


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:


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:

  1. Download pip with the command wget https://bootstrap.pypa.io/get-pip.py.
  2. Install the downloaded package into a local directory (such as ~/.local/bin) with python3 get-pip.py --user.
  3. Add .local/bin to your $PATH by adding the following to the bottom of your ~/.bashrc file: export PATH="$HOME/.local/bin:$PATH". (Where USER is your Linux username).
  4. Source the .bashrc file with source ~/.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:


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:


In that file, add each package you want to install on individual lines like this:


Save and close the file.

Run the installation with:


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:


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:


or

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:


The above command creates a virtual environment called .venv. You then must activate the environment with the command:


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:


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:

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.