Third-party libraries and pip

Third-party libraries are everything that's not part of Python's standard distribution: web frameworks, data-science libraries, ML toolkits, parsers, and thousands more. You install them with pip, the standard Python package manager.

What are third-party libraries?

Third-party libraries are Python modules that are not part of the standard library and are developed by independent developers or organizations.

What is pip?

pip (Package Installer for Python) is a package management system used to install and manage software packages written in Python. It downloads packages from the Python Package Index (PyPI) and can also update, remove, and pin project dependencies.

Installing and using pip

Usually pip is already installed along with Python. You can check the presence and version of pip like this:

pip --version

Expected result:

pip 23.1.2 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)

Basic pip commands

Installing a package

pip install requests

Expected result:

Collecting requests
  Downloading requests-2.31.0-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.6/62.6 kB 1.2 MB/s eta 0:00:00
Installing collected packages: requests
Successfully installed requests-2.31.0

Installing a specific version

pip install requests==2.25.1

Expected result:

Collecting requests==2.25.1
  Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.2/61.2 kB 1.8 MB/s eta 0:00:00
Installing collected packages: requests
Successfully installed requests-2.25.1

List of installed packages

pip list

Expected result:

Package    Version
---------- ---------
certifi    2023.5.7
charset-normalizer 3.1.0
idna       3.4
pip        23.1.2
requests   2.31.0
setuptools 67.8.0
urllib3    2.0.3

Removing a package

pip uninstall requests -y

Expected result:

Found existing installation: requests 2.31.0
Uninstalling requests-2.31.0:
  Successfully uninstalled requests-2.31.0

Popular third-party libraries

Python has a huge number of third-party libraries for a wide variety of tasks. Here are some of the most popular and useful:

LibraryDescriptionMain applicationLink
RequestsConvenient handling of HTTP requestsInteraction with web resources, APIsDocumentation
PandasPowerful tool for data analysisProcessing and analyzing tabular dataDocumentation
NumPyWorking with arrays and mathematical calculationsScientific computing, array operationsDocumentation
MatplotlibData visualizationCreating plots and chartsDocumentation
SeabornStatistical data visualizationBeautiful statistical graphicsDocumentation
scikit-learnMachine learningBuilding and training machine learning modelsDocumentation
TensorFlowDeep learning and neural networksComplex deep learning modelsDocumentation
PyTorchDeep learning with dynamic computational graphsResearch in deep learningDocumentation
FlaskMicro-framework for web developmentCreating web applications and APIsDocumentation
DjangoFull-featured web frameworkLarge web projectsDocumentation
Beautiful SoupParsing HTML and XMLExtracting data from web pagesDocumentation
PillowImage processingEditing and analyzing imagesDocumentation
SQLAlchemyORM for working with databasesInteraction with SQL databasesDocumentation
PygameCreating games and multimedia applications2D game developmentDocumentation
PyQtCreating desktop applicationsGraphical user interfaces (GUI)Documentation

The choice of library depends on the specific task you want to solve. The Python community is very active, and ready-made solutions already exist for most practical tasks, which can be installed via pip.

Example of installing a popular library:

# Installing pandas for data analysis
pip install pandas

# Installing Flask for web development
pip install flask

Virtual environments

If you install every library into your system Python, sooner or later you'll hit a conflict: one project needs django 3.0, another needs django 4.2, but only one version can be installed. The standard practice is to create a separate virtual environment for each project.

A virtual environment (venv) is an isolated Python environment where you can install packages without affecting other projects or the system Python.

Two projects, each with its own venv holding its own package versions: django 3.0 + requests 2.20 in project A, django 4.2 + requests 2.31 in project B

Creating a virtual environment

# Creating a virtual environment
python -m venv myenv

# Activating the virtual environment
# On Windows:
myenv\Scripts\activate

# On macOS/Linux:
source myenv/bin/activate

# After activation, the environment name will appear at the beginning of the command prompt
(myenv) $

Installing packages in a virtual environment

# Installing packages in the activated virtual environment
pip install pandas matplotlib

Saving and installing dependencies

# Saving the list of installed packages
pip freeze > requirements.txt

# The contents of the requirements.txt file will look something like this:
# matplotlib==3.7.2
# numpy==1.25.2
# pandas==2.0.3
# ...

# Installing packages from the requirements.txt file
pip install -r requirements.txt

Understanding Check

Let's check how well you've learned about third-party libraries and pip:

Which command will correctly install pandas version 1.5.0?