Libraries in Python#

Learning Objectives

Questions:

  • What is the difference between Core Python and external libraries?

  • How do I install a Python library?

  • How do I use a Python library in my code?

Objectives:

  • Understand what libraries are and why they are useful.

  • Use external libraries in your Python code with confidence.

  • Know where to look for reliable libraries and their documentation.


Motivation#

Writing every piece of code yourself can be time-consuming and difficult. Many tasks, such as working with data, creating plots, or connecting to the internet, have already been solved by others.

By using external libraries, you can save time, avoid mistakes, and focus on the problem you actually want to solve.
Libraries allow you to extend Core Python with tools that are reliable, widely used, and often easier than building everything from scratch.


Introduction#

So far, we have mainly worked with Core Python. Core Python is everything that comes with Python when you install it - the built-in functions, data types, and modules that are always available.

However, Python really shines because of its external libraries. A library is a collection of code that someone else has written, which you can use to make your own work easier.
Instead of writing everything from scratch, you can rely on libraries that already solve common problems.


Installing vs importing#

Understanding the difference between installing and importing a library is essential when working with Python. Although these steps are closely related, they serve different purposes - and the distinction is often unclear to beginners.

Installing libraries#

When you install Python, it comes with Core Python - the built-in parts of the language. To do more advanced things, you often need to install external libraries.

Libraries can be installed from the Python Package Index (PyPI) using pip, Python’s package installer.

This command installs the requests library using pip:

pip install requests

If you have not installed requests before, this will download and install the latest version of the library along with its dependencies.

pip commands are most often run through a shell such as the Terminal on macOS or the Command Prompt on Windows.
However, you can also run shell commands directly from Jupyter notebooks by adding an exclamation mark ! in front of the command:

! pip install requests

Sometimes you want to make sure you have the newest version of a library. You can do this by adding the --upgrade flag:

pip install --upgrade requests

Adding --upgrade not only installs the library if it is not already installed but also updates it to the latest version if you already have it.
This is useful for making sure you have the newest features and bug fixes.

Again, in Jupyter notebooks you can run this with:

! pip install --upgrade requests

Importing libraries#

Installing or upgrading makes a library available on your computer. To actually use it in your code, you need to import it. Importing happens inside your Python program.

Example:

import requests

You only need to install a library once before you can use it - and you may occasionally upgrade it to get the latest version.
In contrast, you must import the library at the beginning of every Python script or notebook where you intend to use it.

Using aliases#

Sometimes libraries have long names, and programmers like to shorten them when importing. This is called using an alias.

For example, the pandas library is almost always imported like this:

import pandas as pd

This tells Python: “Import the library called pandas, but in my code I will refer to it as pd.”

Now you can use:

data = pd.DataFrame({"A": [1, 2, 3]})
print(data)

Other standard conventions include:

import numpy as np
import matplotlib.pyplot as plt

These conventions save typing, and because so many people use them, they make your code easier to read and share.


Who makes libraries, and are they safe?#

Libraries are written by individuals, research groups, companies, and communities of developers. Some of the most widely used libraries - such as numpy, pandas, and matplotlib - are maintained by large teams and used by millions of people.

In general, libraries that are widely used and actively maintained are considered safe. However, because libraries are just code that someone else has written, it is possible for poorly maintained or unknown libraries to contain errors or even malicious code.

Tips for safety:

  • Prefer well-known libraries with good documentation

  • Look at how often the library is updated

  • When in doubt, ask or research before installing an unfamiliar library


Where to find libraries and documentation#

Most Python libraries are listed on the Python Package Index (PyPI):

  • Website: https://pypi.org

  • This is where pip downloads from when you install a library.

Well-known libraries usually have their own documentation websites. A few examples:

Documentation tells you what functions the library provides, how to use them, and often includes tutorials and examples. Learning how to read documentation is an important skill as you progress.


Exercises#

Exercise 1: Install and import Pandas#

  1. Check whether you have pandas installed. If not, install it:

pip install pandas
  1. Open a Python session and import pandas with the alias pd:

import pandas as pd
  1. Print the version of pandas you have installed:

print(pd.__version__)

This prints the version of pandas installed on your system, for example 2.3.3.

Exercise 2: Upgrade Pandas#

  1. Make sure your pandas library is up to date by running:

pip install --upgrade pandas
  1. If you are working in Jupyter, remember that you can also run this command with an exclamation mark:

! pip install --upgrade pandas
  1. After upgrading, check the version again:

import pandas as pd
print(pd.__version__)

This will upgrade pandas to the newest available version and then print the version number to confirm the update.


Key points#

  • Core Python is what comes built-in when you install Python.

  • External libraries extend Python’s capabilities.

  • Installing puts the library on your computer; importing brings it into your program.

  • You can find libraries on PyPI and read more about them in their official documentation.