Python Libraries

Libraries

One factor behind Python’s success is its vast collection of libraries, especially for scientific computing, data analysis, and machine learning. The availability and wide selection of pre-made, often production-ready, code that can easily be incorporated into projects have helped speed up the development process. Python’s active communities have helped ensure that its libraries are well-maintained, regularly updated, and documented.

Unless you’re planning to build an AI project (or, for that matter, almost any kind of Python project) from first principles, you’ll most likely use at least a handful of libraries. In this section, you’ll learn how to install them on your system and import them into your projects.

Modules, Packages, and Libraries

Python programmers often use the terms modules, packages, and libraries interchangeably. Each actually refers to something different; only two have a technical definition in Python:

  • Module: A single Python file (a file with a .py filename extension) that contains Python code. Modules are Python’s smallest unit of organization for source code.
  • Package: A directory that can contain multiple Python files. They can also contain subdirectories, act as sub-packages, and organize a package into a hierarchy. Packages are Python’s largest unit of organization for source code.

The term library has no formal meaning in Python. It’s simply a catch-all term for “pre-made code that can import into my own project” and generally refers to large packages aimed at a problem domain or purpose.

The distinction between modules and packages is often more important when building them rather than using them. Most of the material in this lesson is about using them.

Importing Modules and Packages

Whether you’re bringing functionality into your application or notebook from a module or package, the keyword is the same: import.

Here’s the most basic use of import. It imports the entire math module, which provides a set of math constants, such as π and e, and functions, such as trigonometric and rounding functions:

import math

Once you’ve imported the math module, you can access items from it by specifying the module name and item name using dot notation. The code example below uses math’s pi constant and sin() function:

angle_90_degrees = math.pi / 2
print(f"The sine of 90 degrees is {math.sin(angle_90_degrees)}.")

Because some modules, packages, and libraries can have long names, Python provides the as keyword, which allows you to refer to an imported module using a shorter alias:

# Use a *really* short alias for “math”
import math as m

angle_90_degrees = m.pi / 2
print(f"The sine of 90 degrees is {m.sin(angle_90_degrees)}.")

If you need only a few specific items, you can use the fromimport syntax, which lets you use them without having to specify the name of the module or package you’re importing:

from math import pi, sin

angle_90_degrees = pi / 2
print(f"The sine of 90 degrees is {sin(angle_90_degrees)}.")

Note: fromimport lets you import everything from a module or package without having to refer to the module or package’s name using fromimport *, but it’s generally not recommended, especially with packages that provide a lot of functionality, because it can lead to namespace conflicts.

When using fromimport syntax, you can use the as keyword to create an alias for a function. The example below uses as to shorten the sin() function to s():

from math import pi, sin as s

angle_90_degrees = pi / 2
print(f"The sine of 90 degrees is {s(angle_90_degrees)}.")

Using the Python Standard Library

After looking at the previous examples, you probably wonder, “Where did math come from?”

It’s one of many modules and packages in the Python Standard Library, which is included in every standard Python installation, especially the official one from python.org. You don’t have to do anything special to make anything from this library available to your project — just import the modules and packages you need.

The Python Standard Library provides ready-to-use functionality for common programming tasks. The list below shows only a small fraction of the Library’s modules and packages, but these are ones that you’ll likely use when building Python projects:

  • csv: Reading and writing CSV files.
  • datetime: Working with dates and times.
  • difflib: Computing differences in sequences. It’s especially useful for strings, lists, files, and web pages.
  • json: Processing JSON data
  • math: Math functions beyond basic arithmetic, especially “scientific” math.
  • os: Miscellaneous operating system commands.
  • random: Generating pseudo-random numbers.
  • re: Processing regular expressions.
  • sqlite3: Working with SQLite databases.
  • string: Additional string constants and functions.
  • sys: System-specific parameters and functions.
  • venv: Working with virtual environments, which you’ll use later in this lesson.

You can learn more about the Python Standard Library and its vast collection of modules and packages in the official documentation’s Python Standard Library section.

Using PyPI, the Python Package Index

Python has a centralized official directory where developers can find and distribute modules and packages: PyPI, the Python Package Index.

PyPI provides a comprehensive, searchable catalog of modules and packages, each with its own page. Each page provides a description of the module or package, some basic documentation, and, most importantly, how to install the module or package on your system using pip.

Using pip, the Python Package Installer

pip is Python’s standard package management system, which helps you install and manage libraries that are not part of the Python Standard Library. It’s a command-line tool you’ll often use as a Python developer.

You’ll use pip to install packages from PyPI most of the time, but it can also install packages from version control, your local filesystem, and other sources.

Installing Packages

To install a package listed on PyPI with pip, use the following code on the command line:

pip install package_name

Here, package_name is the name of the package you want to install.

You can install multiple packages using a single pip command:

pip install package_1 package_2 package_3

If you don’t have admin rights on your system, you might need to install packages only for your user account. Use the --user flag in this case:

pip install --user package_name

pip installs a package’s latest version by default. If your project needs a specific version, specify the version by following the package name with == and the version number. For example, the following pip command installs version 2.4.6 of the package named package_name:

pip install package_name==2.4.6

Confirming a Package Is Installed

The simplest way to confirm that a package is installed is to use pip show:

pip show package_name

If package_name is installed, pip will respond with the package’s name, version, brief description, home page, contact information for the package’s creator, and license information.

If package_name is not installed, pip will respond with this message: WARNING: Package(s) not found: package_name.

Installing Multiple Packages With requirements.txt

Many projects import functionality from multiple packages, so pip can also import multiple packages whose names are listed in a requirements.txt file. The following command makes pip install all the packages listed in requirements.txt:

pip install -r requirements.txt

If you don’t have admin rights on the system, use the --user flag:

pip install --user -r requirements.txt

The -r requirements.txt specifies that pip should install the packages listed in a specified requirements file and that the file’s name is requirements.txt.

Note: The requirements file can be a text file with any name, but it’s Python convention to use requirements.txt.

The contents of requirements.txt are pretty simple: only the names of the packages to be imported. For example, here’s what a requirements.txt file should contain if you wanted pip to install packages names package_1, package_2, and package_3:

package_1
package_2
package_3

You can use == if you need to specify a specific version for a specific package, and you can use # to add comments:

package_1
package_2

# The project crashes if
# you don't use version 2.0
package_3==2.0

pip can generate a requirements file from the current environment. Here’s a command to create such a file and save it as requirements.txt:

pip freeze > requirements.txt

Listing Installed Packages

To see which packages are installed, use this command:

pip list

This will produce a list in this format:

Package    Version
---------- -------
package_1  1.3.4
package_2  8.7.5
package_3  20.9

pip list takes several useful flags, including:

  • --uptodate: List only those packages that are up-to-date.
  • --outdated: List only those packages that are outdated.
  • --format json: Provide the list in JSON format, which produces output like this (formatted for readability):
[
  {"name": package_1, "version": 1.3.4}
  {"name": package_2, "version": 8.7.5}
  {"name": package_3, "version": 20.9}
]

Upgrading Packages

If you found some out-of-date packages while running pip list --outofdate, you can upgrade them using the --upgrade flag. Here’s a command to upgrade a package named package_name:

pip install --upgrade package_name

You can even upgrade packages listed in requirements.txt:

pip install --upgrade -r requirements.txt

If you don’t have admin rights on your system, you can limit the upgrade to your account only with the --user flag:

pip install --user --upgrade package_name

Uninstalling Packages

To uninstall a specific package, use pip uninstall:

pip uninstall package_name

You can uninstall multiple packages on the command line:

pip uninstall package_1 package_2 package_3

You can also uninstall the packages listed in requirements.txt:

pip uninstall -r requirements.txt
See forum comments
Download course materials from Github
Previous: Introduction Next: Python Libraries Demo