Skip to content

PIP

There are a huge number of libraries in Python ready to be imported and used when writing a program. The code for these libraries is a collection of functions and classes specialized in solving a specific problem. For example, the math module contains a large number of implemented mathematical functions, often based on non-trivial algorithms. Thanks to Python programmers, and because such functions are often needed, they have been saved in one library and shared by adding a package to the Python installer. Libraries supplied with Python are called standard (stdlib).

In addition to importing built-in library, we also have the ability to attach to the currently written modules other whose authors are ourselves (or are those of our team).

Because Python also has the ability to create our own packages, there is the opportunity to attach entire directories - not necessarily ours. All those packages that are not part of the Python installer, but are shared, ready to download and join the environment, are called external libraries.

All these libraries are stored on the server and are available for download. At the moment, the counter indicates over 200,000 libraries and this is partly the answer to the question "why they are external, and not installed together with the Python interpreter". There are simply too many of them. In advanced projects, several to several dozen external libraries are used, the rest of the libraries are unnecessary. If necessary, it can be easily downloaded.

Types of libraries in Python

As already mentioned, libraries are divided according to the source they come from:

  • internal (built in) - available immediately after installing python on your computer,
  • external - not available immediately after installation, it is necessary to download them first.

Below are some examples. Some of them should already be known:

Built-in libraries:

  • os,
  • sys,
  • math,
  • random.

External libraries:

  • flask

A light framework for creating applications and websites.

  • django

A high level platform for creating web applications.

  • tensorFlow

Google framework for creating machine learning models.

  • ipython

An interactive shell with several amenities that are not found in a regular terminal.

It is recommended to explore the information about these packages by referring to the documentation.

pip tool

The need to download external libraries and attach them to Python correctly, had to be addressed in the right way. This form of things contributed to the creation of a separate tool with the short name pip (eng. Package Installer for Python).

pip is a package manager that allows you to manage and include packages that are not part of the standard Python library. It contains a large number of commands that should be used to manage external libraries (download, delete, list, etc.)

Python developers have created a huge pool of packages that can be downloaded with the pip tool. If any functionality is needed, it is likely that it has already been implemented and made available in one way or another. Just search the web.

Since Python version 3.4, pip is included in the installation package, making it an extremely important tool for this language programmer.

pip tool commands

The most important and commonly used commands are summarized below.

Package installation

pip is used to install and manage additional packages.

The basic commands of the pip tool can be obtained using the command:

pip help.

help

To search for the package you want to install, use the command:

pip search <package_name>

help

Installing packages

When we find the package that interests us, we can install it using the command:

pip install <package_name>

pip, in addition to the indicated package, it will also install all dependent libraries whose versions are not on the computer or in the Python environment.

help

If we try to reinstall a package that is already in our Python environment, we will receive information that the requirements have been met - this will mean that the library has already been installed.

help

Uninstall packages

The packages are uninstalled using the command:

pip uninstall <package_name>

Execution of the command will list and ask for confirmation of deleting files (there may be many of them, below the sample of responses to the command):

help

When the uninstallation is successful, the appropriate information will be displayed:

help

Listing installed libraries

To check all currently installed packages and their versions, use the command:

pip freeze

help

Searching for newer versions of packages

To search for installed packages that have a newer version ready for download, use the command:

pip list -o

help

Package update

The selected package can be installed using the command:

pip install -U <package_name>

The program will always use one specific version of each library.

Installing a specific version of the package

Libraries can be installed in a specific version by command:

pip install <package_name>==version

If you do not want to agree on a certain version of the package (because it may be faulty), you can download it in a different one than indicated:

pip install <package_name>!=version

Installing from a specific version of the package.

Library installation only from a certain version:

pip install <package_name>>=version

Installing a package with versions between specific releases:

pip install <package_name>>=version_min, <version_max

External libraries in projects

As mentioned earlier, in advanced projects often use a huge amount of additional packages (up to several dozen per project). It's hard to imagine running the pip install command 50 times before our program can run (without all required libraries, the code will not be full, Python will not know how to use functions that cannot be accessed). Besides, if several programmers are working on the project, they should work on exactly the same versions of the libraries.

Therefore, a system of quick and simple introduction of external libraries to the project had to be added.

It turns out that the pip install command is able to use a text file as an argument and install all libraries whose names will be inside this file. Most often, such a dependency file is called requirements.txt and stored in the main repository directory.

File requirements.txt - save

To create a requirements.txt file that stores information about installed external libraries, use the command:

pip freeze > requirements.txt

File requirements.txt - read

To restore the environment (installed libraries) from the requirements.txt file, use the command:

pip install -r requirements.txt