Connect with us

Website

How To Install Pip And What You Need To Know

Published

on

How To Install Pip And What You Need To Know

Earlier, I’d written about how to use Python to change directories. You can see from that, that Python is a very user-friendly language and you can already do much with it out of the box. For example, in the linked article, I used the standard library function called “os”, which is included with python, so I could import it into my code like this:

import os

There was no need to add anything else. However, the true strength of python lies in the thousands of packages created by 3rd parties These packages are placed in the Python Package Index or PyPI. As I write this, there are currently 528,500 packages available, giving you a sense of how much power is locked behind python packages. So if you want to exploit python’s power, the first thing you need to learn is how to install pip and import the packages for your own use.

Trying to Import an Unsupported Package without Pip

To show you why we need pip, let’s say we want to use the “Pandas” package from the PyPI. This is a popular and powerful tool to work with relational and labeled data. If we try and simply import pandas using the python3 command line, we get this error:

Failing to Import Pandas without Pip
Failing to Import Pandas without Pip

You can see that “import pandas” doesn’t work and Python throws an error saying “ModuleNotFound”. So this is why we need pip.

How to Install Pip on Linux for Python

Installing pip is easy, using the built-in package manager for your flavor of Linux. For example, I’m using Ubuntu and the command is:

sudo apt install python3-pip

After going through with the installation, you can verify the pip version using the command below:

pip3 --version

And here’s the output;

Advertisement
Pip3 Version
Pip3 Version

It’s that simple. If you want to install pip on CentOS instead, the command is:

sudo dnf install python3-pip

Follow the same instructions as before.

Testing Pip for Importing Packages

Now that we’ve installed pip, it’s time to see if we can install the pandas package that we were unable to import earlier. But we can’t just import the package cold turkey. First, we need to install it. To install pandas, we use the command:

pip3 install pandas

The tool will search the PyPI, locate the resources, and download and install them as shown below:

Installing Pandas
Installing Pandas

This is for installing the general package. If, however, you want to install a specific package from the PyPI, you can specify the version you want as shown here:

pip install pandas==2.2.1

If you don’t specify a version, pip will download the latest version of that package as uploaded on the PyPI. However, we can override this by using various mechanisms that I’ll detail below. You can create a kind of configuration file that specifies the release version of various packages that your project needs.

Once the installation is complete, you should be able to import the pandas package into your script as shown here:

Advertisement
Importing Pandas and Using it
Importing Pandas and Using it

You can see above, that the “import pandas” command doesn’t generate an error anymore. Here, I use “pd” as an alias, so I don’t have to type “pandas” each time. In this code, I create some data about people and use the pandas library to display it neatly in a table. The Pandas package lets you do a lot more, like summarize information, import data from a CSV file, and more.

So that’s a simple example from start to finish about how to install pip for Python on Linux. But there’s a lot more to the equation.

Managing Virtual Environments with Pip

The above process is very basic and is but a taste of what you can achieve with pip. If you’re just experimenting with python, then working in the regular python environment from the command line is fine. But as soon as you start working on an actual project, you need to understand the concepts of virtual environments.

A virtual environment in python is nothing but a space for your project. Each project has its own files and dependencies. The packages that are installed for one project might not be necessary for another, so we need a way to keep everything separate. Projects are installed in folders, and python will create the folder structure for you.

Pr-Requisites for the Virtual Environment Project

If you’re using an Ubuntu machine like I am, you need to install one additional module before the system allows you to create virtual environments. The “ensurepip” module lets pip function in virtual environments. To install it, use the following command:

sudo apt install python3.10-venv

If you don’t want to bother with a specific version, you might simply be able to use:

Advertisement
sudo apt install python3-venv

Another option is to first check with python3 version you’re using with the command:

python3 --version

And then use the version number to install the appropriate python3-venv version. Either way, the installation process is easy and only takes a minute or so.

Creating a Virtual Environment and Activating it

Let’s say you have a folder called “python_project” and you want to create a new virtual environment inside it. We use the command:

python3 -m venv ./python_project

The above command will install a new project in the “python_project” folder. The folder needs to be empty for this to work. If you already have stuff inside it, the tool will reject the creation of a new environment. As part of the setup process, it’ll create new files and folders as shown in the screenshot below:

Files and Folders in the Virtual Python Environment
Files and Folders in the Virtual Python Environment

Now that we’ve created the project, we can “activate” it using the following command, assuming that you’re already in the project folder.

source ./bin/activate

Activating a virtual environment changes the system prompt to something like this:

Advertisement
(python_project) bhagwad@bhagwad:~/python_project$

As you can see, a new prefix has been added to the prompt, indicating the name of the virtual environment. From now on, any Python-related activities will be executed in the context of this project. For example, if you install a package, it will be installed to the “python_project”, and not the global python environment. It’s important to note that the activation doesn’t prevent you from switching the directory and even entering the project space of another virtual environment. But all the python paths and packages will only affect the current activated virtual environment.

Deactivating a Virtual Environment

Once you’re done working on a particular python project, you can deactivate it as follows:

deactivate

Since the system already knows which project is currently activated, you don’t need to specify which project you want to deactivate from. Once deactivated, you’re free to switch to another project. If you want to delete a virtual environment, simply get rid of its files and folders after deactivating it. It’s simple!

Using pip in a virtual environment is as straightforward as using it in the global python environment. There’s no need to install it again, for example.

Conclusion

As you can see, once you get familiar with the concept, installing pip with python is a simple matter, and very intuitive. And creating virtual environments for projects is also straightforward. Hopefully, this guide is enough to get you started, and you can use it to install your very first project!

Advertisement

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.