Python Libraries

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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.

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:

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.

import math
angle_90_degrees = math.pi / 2
print(f"The sine of 90 degrees is {math.sin(angle_90_degrees)}.")
# 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)}.")
from math import pi, sin

angle_90_degrees = pi / 2
print(f"The sine of 90 degrees is {sin(angle_90_degrees)}.")
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?”

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.

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.

Installing Packages

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

pip install package_name
pip install package_1 package_2 package_3
pip install --user 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

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
pip install --user -r requirements.txt
package_1
package_2
package_3
package_1
package_2

# The project crashes if
# you don't use version 2.0
package_3==2.0
pip freeze > requirements.txt

Listing Installed Packages

To see which packages are installed, use this command:

pip list
Package    Version
---------- -------
package_1  1.3.4
package_2  8.7.5
package_3  20.9
[
  {"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
pip install --upgrade -r requirements.txt
pip install --user --upgrade package_name

Uninstalling Packages

To uninstall a specific package, use pip uninstall:

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