СHRISTMAS
WHEEL OF FORTUNE

Tap the button and win a guaranteed prize right now!

By registering, you are signing up to receiving e-mails.
JT
February 26 2024
Updated February 26 2024

The pip command. Usage guide

Linux Python

Every developer or anyone involved in the use of code has encountered mucky and repetitive tasks that just had to be automated! One such task is finding and connecting libraries to existing or new projects. If before we had to find a repository with the latest or a particular version of a package, download it, check the electronic digital signatures and prescribe it in the project and variable environments, now there is a pip answer to that!

What is pip?

PIP or Python Installs Packages is a Python package manager, is a standalone solution in the form of a module for managing packages, libraries for projects. It is called with a simple syntax:

pip install package-label

Automates all of the above tasks, and also has the ability to manage virtual environments in the case of several projects deployed on the machine. Let's consider the work of the utility on the example of a cloud solution from Serverspace, for this purpose let's go to the creation of a server on any of the two platforms vStack cloud or VMware cloud. Click on the Create Server button and select a configuration suitable for our tasks, then click Order.

Create Server
Screenshot №1 — Create Server

After some time the servers will be available via any of the main connection methods, for the current tasks Debian 12 with a data centre in Istanbul has been chosen.

It is now necessary to update the indexes and packages with your system package manager, for compatibility of services and utilities:

apt update && apt upgrade -y
Update system's package
Screenshot №2 — Update system's package

In standard versions of Linux distributions python or python3 is a pre-installed language with basic libraries and an interpreter. It is the main component of the package manager. Let's check its presence on the OS with the command:

python || python3 || python3.8
Check version of software
Screenshot №3 — Check version of software

How to install pip?

This should open the interpreter, which can then be closed with Ctrl+D. Once we know the version of Python, we now need to install the appropriate pip package, different versions have different requirements:

  • Python 2.x requires the installation of the python-pip package;
  • Python 3.x or 3.8.x requires the python3-pip package.

To install the selected version, specify the required package in your package manager command:

apt install python3-pip
Python modules installation
Screenshot №4 — Python modules installation

The download speed depends on the quality of your connection, in case it is broken, you need to reinstall the packages.

How can I check if pip is installed?

You can invoke the utility in one of two convenient ways, or by contacting it directly, write the command according to the installed version:

pip3

Either by specifying the package manager as a language module, referring to the interpreter:

python3 -m pip
Python modules installation
Screenshot №5 — Python modules installation

The screenshot also shows an example of direct access to the interpreter, let's imagine that you don't have the usual python3 or python command working. You can specify the path to your interpreter instead!

How to search for packages in pip?

When using the pip search package manager command, an error may occur indicating that the package search function is disabled. As of 2022 it is recommended to use web search, the reason for this was a case of overloading the project service when using pip. This overloaded resources for a while due to its search method, however, if web search is not an option for you, you can download an alternative project search tool:

pip3 install poetry

In the future, which can be invoked using the command:

python3 -m poetry

You can also search for repositories with projects by specifying the package you are looking for. In our example, we will search for everything with dns:

python3 -m poetry search dns
Search packets
Screenshot №6 — Search packets

For ease of use, let's create an alias for our command and replace it with a shorter version:

echo "alias poetry='python3 -m poetry'" >> ~/.bashrc

Be sure to apply the changes made to the system:

source ~/.bashrc

You can check the result of the changes by using the poetry command, which we specified as alias!

Where does pip install the packages?

Packages installed with pip are usually installed in the directory where Python is installed. They are located in the "site-packages" subdirectory. If the installation takes place in a virtual environment, the packages are installed in the directory of that virtual environment. Again, they will be in the "site-packages" subdirectory.

How to fix "Error: externally-managed-environment" in Debian?

If you do use the Debian distribution, you may encounter a problem when installing packages via pip. The error states that the global environment is under external control.

The global environment is an abstract concept that combines environment variables, configurations, and used libraries on a system.

The problem comes from the fact that when you use the Python global environment, which is managed by the system's package manager, there are dependency and version conflicts between packages. When you install packages through the Python global environment, they become available to all applications and scripts on your computer. However, this can also cause different applications that require different versions of the same package to conflict with each other.

You can ignore the warning and delete the file that blocks packages from being installed in the global environment:

sudo rm -rf /usr/lib/python3.11/EXTERNALLY-MANAGED
Error with first solution
Screenshot №7 — Error with first solution

However, you may see a warning about possible incorrect operation of the manager right after installation. In order to get the system state back - you need to remove conflicting packages that are installed using PIP.

Using virtual environments solves the problem in a safer way by creating isolated Python environments for each project or application. This allows each project to have its own dependencies and package versions, independent of the global environment or other projects. It is possible to manually create and activate, however, there is an automated pipx solution. Which will not only install, but also manage your virtual environments.

apt install pipx

.

Using pip in Python simplifies package management, and virtual environments provide dependency isolation, preventing conflicts between projects. This greatly improves the management and development of Python software.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.