Programming

Everything you should know about Python's PIP and PyPi

Everything you should know about Python's PIP and PyPi

PIP is the standard package manager available in python. Although Python's standard library comes with many useful packages by default, we are not limited to only those packages. In python, we have a vast repository of packages at PyPI, which are developed by many great contributors. With the pip package manager's help, we can easily install and use any of these packages in our python code.

You may be wondering what's the need for PIP or PyPI in our projects when we can write code without those packages using only the python's standard library. The reasoning behind this is, we can save some time and effort as a python programmer. We get to use many packages available in the PyPI repository to write code fast without reinventing the wheel.

The package manager's concept is nowadays used in many programming languages, including ruby and javascript.

To follow this tutorial, it is recommended to install the latest version of python in your system. If you don't have the latest python installed, you can follow our guide on installing the latest python version on Linux. 

PIP Overview

PIP is the standard package manager for python. This means that by using pip, we can install and manage additional libraries in python that are not a part of the python's standard library. Let us look at all its amazing features, which would help us build a good knowledge of python.

PIP installation

In many Operating Systems, pip is present by default or installed while installing python, making our task easy, and we don't feed any manual installation. You can check whether pip is present or not in your Operating System by typing the following command in your terminal.

pip --version

If the above command runs successfully and prints the version of pip present in your system, you need to install it manually.

For Windows users, there may be a broken installation of python due to which pip is not working, so they can install pip by reinstalling python using the default setup present at python.org. In Linux, we can easily install pip using the default package manager present for the OS. In Linux distros that use DNF or yum package manager, we need to type the following command for installing pip.

sudo yum install python3-pip

or,

sudo yum upgrade python3-pip

To install pip on Ubuntu/Debian system which have the apt package manager, use:

sudo apt install python3-pip

This will install the pip packed manager on your Linux system. You can check your installation by typing:

pip --version

Installing a Package

The pip tool has been built to make our task easy as a python programmer. We can use the pip package manager to install new python packages in our python environment easily. To install python packages using pip, we need to pass the package name as an argument to the pip command's install parameter. Look at the below syntax.

pip install package_name

If we want to install the python package Django, we need to type the following command in the terminal.

pip install django

You can use Django in the projects. On running the code, we will have the following output in our terminal.

installing a package using pip

We can also install a specific version using python by specifying the version in the install parameter. To install the Django package of version 2.1, run the following code.

pip install django==2.1

Here is the output.

installing a specific version of a package using pip

We can also install multiple packages using the pip tool using the below command.

pip install django flask

Using the Requirements.txt file

While installing packages using the pip command directly, it will install the latest version of the packages. But in many cases, we don't need the latest version and need a specified version of the package for the program to run correctly. We can create a requirement.txt file that will contain the packages required in the program with their version info. The pip install command can read this file, and the required packages can be installed easily. We can use this file to install packages required by our program on other machines too. For example, create a requirements.txt file in your current working directory with the following data.

requests==2.21.0 urllib3==1.24.1

Now run the following command in the directory where the requirements.txt file is present.

pip install -r requirements.txt

The above command will install all the packages present in the requirements.txt with the version given in the file. It is a good practice to include a requirements.txt file with the programs we write so everyone can easily install the specified packages as required by the program, making our program run without any errors.

List all packages

We can also use the pip package manager to list all the packages present in our python environment. To do so, we need to use the list parameter with the pip command. For example, run the following command in your terminal.

pip list

This will list all the python packages installed in your system. You will see an output similar to the below image.

listing images with pip

You can also use the pip tool to get the details about a particular package. To do this, we need to pass the package name as an argument to the pip command's show parameter. For example, look at the below code.

pip show django

It will show you the details about Django's package, including its version on running the above command. We will see the following output on running the command.

package details using pip

As you can see in the output, it has provided all the details about the package, including its summary, author, version, location on the device, and links to the homepage. There are also links to the Github page for the code.

Finding new packages

Pip provides a search option that can be used to find new packages. For example, to find packages containing the keyword HTTP, we need to run the following code.

pip search http

On running the above code, we will get the name of all packages containing the keyword HTTP and get a short description of the command, which will help us choose a package to use in our project. We will get the following output on running the above command.

searching a package using pip search

We can also search for a package by visiting the PyPI website. Finding a package using the PyPI website is the best choice as it gives us additional features like filtering the packages. For example, if we search for the keyword HTTP, we will see the following.

searching a package at the PyPI website

We can also click on a package to find the information about the package. For example, when we click on the package http1, we will see the following.

package description at PyPI website

As you can see, we get most info about the package, including the package description, release history author details, etc. There is also the pip command that we will need to run on the terminal for installing that package. You can also find packages to be used in your projects using your favorite search engine in an internet browser.

Updating a Package

We can also use the pip package manager to update the python packages easily. We need to use the upgrade option with the install parameter following the package name we want to update in the pip command. For Example, To update the Django package, we need to type the following command in the terminal.

pip install --upgrade django

upgrading a package using pip

PIP Check command

The pip tool comes with an excellent option, which can be used to check if there are broken dependencies. Using the check option with the pip command, we will get the details of all the unmet dependencies. The presence of broken dependencies can break our code, so it is good to check them and upgrade the packages. We need to type the following command to check broken dependencies.

pip check

On running the command, I get the following output.

checking for unmet dependency using pip

As you can see in the above image, I have some unmet dependencies which need to be resolved. If I don't resolve them, they can cause an error while using on our projects.

Removing a Package

There are times when we want to remove the packages installed from PyPI. We can do it easily using the pip package manager. We can uninstall a package from our system using the uninstall parameter with the pip command and passing the package name as the argument. For example, if we want to remove the package Django from our system, we need to run the following command in the terminal.

pip uninstall django

This will remove the Django package permanently from our python environment. We will see the following output in the terminal.

uninstalling a package using pip

Upgrading PIP

We should also upgrade the pip package manager to download the latest packages from the PyPI repository. Use the following command in the terminal.

pip install -U pip 

We will see the output, as shown in the below image.

updating pip

For the windows system, we need to type the following command to upgrade the pip package manager.

python -m pip install -U pip

PIP Alternatives

PIP is a great tool for python programmers. It is used in many small or enterprise projects and applications for package management. PIP is good for package management, and this tutorial has provided you with the basics you need while using it, but some tools are better alternatives to the pip tool. One such alternative is Anaconda, which we discussed in the tutorial installing anaconda on Linux.

Anaconda is a package, dependency, and environment manager for languages like Python or R. It is primarily used for data science and machine learning. It uses its repository to host all the packages. Anaconda is not only used as a package manager, but also used for creating virtual environments, managing python versions, etc. You should be helpful to look at our tutorial for a good starting point to learn and use anaconda.

Conclusion

That's all for today! We learned the basics to know while using the PIP package manager. Once you are well versed in it, you can easily install and manage packages on your python system. You may also want to see our special guide on using for loops in python.

Cele mai bune 5 jocuri arcade pentru Linux
În zilele noastre, computerele sunt mașini serioase folosite pentru jocuri. Dacă nu puteți obține noul scor mare, veți ști la ce mă refer. În această ...
Battle For Wesnoth 1.13.6 Development Released
Battle For Wesnoth 1.13.6 released last month, is the sixth development release in the 1.13.x series and it delivers a number of improvements, most no...
Cum se instalează League Of Legends pe Ubuntu 14.04
Dacă ești fan al League of Legends, atunci aceasta este o oportunitate pentru tine de a testa rula League of Legends. Rețineți că LOL este acceptat pe...