In this tutorial, **you’ll learn how to work with external libraries in Python**. By the end of this tutorial, you’ll have learned why external libraries are essential to your Python programming journey, and how to install and import external libraries. You’ll also gain an understanding of how external libraries work.

Table of Contents

## What are External Libraries in Python?

So far, you’ve learned how to use the built-in functions and objects that come with Python. One of the biggest perks of using Python is the availability of vast numbers of libraries. These libraries are incredibly diverse and of high quality and make data science much, much easier in Python.

An external library is a custom set of functions, objects, and more that were written to eliminate having to write code from scratch. There are hundreds of thousands of external libraries with a vast variety of abilities that they provide.

Some of these libraries are part of the standard library. What this means is that they are installed with Python and do not need to be installed. The benefit of this is that all Python users will have the same libraries available to them. To learn more about the standard library, check out the official documentation.

Other libraries need to be installed. These libraries aren’t part of the standard library, but can provide many additional features. Later in this tutorial, you’ll learn how to install libraries using different tools.

## Why Use External Libraries in Python?

External libraries in Python open up significant potential in data science. Many data science libraries open up significant opportunities for data analysis, data visualization, and for machine learning. Rather than writing all your functions from scratch, you can lean on the hard work of others.

Python developers make many of their tools available for free. This gives you the opportunity to produce data analysis much quicker and reliably than coding everything yourself.

What’s more is that these libraries are often extensively tested and maintained by groups of developers. This means that these libraries are often very robust, providing incredibly easy ways to make your day to day coding experience significantly easier.

## How to Import an External Library in Python

Let’s take a look at how we can import an external library in Python. In this section, we’ll start off by using a library that’s part of the standard library, meaning that you don’t need to worry about installing it.

The example that we’ll use for this section is the `statistics`

library. The statistics library provides a number of functions for calculating, well, statistics of numeric data.

In order to import a library, we use the `import`

keyword. Let’s see how we can import the `statistics`

library.

```
# Importing a library
import statistics
```

It’s as easy as that! We now have access to all the functions and objects available in the statistics library. Let’s take a look at what happens when we check the type of the library using the `type()`

function.

```
# Checking the type of a library
import statistics
print(type(statistics))
# Returns: <class 'module'>
```

We can see that the type is a `'module'`

, which is an interchangeable term of library.

### Import Only a Single Function in Python

One of the functions available in the statistics library is the `median()`

function. In order to actually use the function, you need to prefix the function with the library name. If you’re only planning on using a single function from the library, you can import only that function.

Let’s see how this works:

```
# Importing only a single function
from statistics import median
# Allows you to use the median() function without writing statistics.median()
```

### Importing a Python Library with an Alias

You can also change the name of a library as you’re importing it by using an alias. This allows you to use an abbreviation for different libraries. You’ll see this used very frequently when we start working with other libraries like numpy, pandas and seaborn.

In order to use an alias, we use the `as`

keyword. Let’s import the statistics library using the alias `st`

.

```
# Importing a library using an alias
import statistics as st
```

What should you keep in mind when using an alias?

When importing libraries it’s important to follow conventions! It may seem fun to give your imported libraries a cheeky alias, but using conventional aliases, or no aliases at all (when that’s convention) allows you to troubleshoot much quicker!

## How to Install External Libraries in Python

There may be many times when you need to install an external library. While Python comes with many excellent modules as part of its standard library, there are many libraries that you may wish to install. Later in this tutorial series, you’ll make extensive use of libraries such as `pandas`

and `seaborn`

in order to work with tabular data and to produce beautiful data visualizations.

In order to install these modules, we can make use of the `pip`

terminal command. `pip`

is the package installer for Python and allows us to install modules from the Python Package Index.

Typically `pip`

will come installed when you install Python. That being said, you may not actually have it installed. In order to check whether or not it’s installed or not, run the following in either your command line or terminal:

`python3 -m pip --version`

python -m pip --version

What if you don’t have pip installed?

If you end up not having Python installed, you can install it directly by downloading it from bootstrap. Download the file linked here and then run it by running `python get-pip.py`

in your terminal from the location where you downloaded the file.

Once you have pip up and running, you can install external libraries which aren’t part of standard library using pip in the terminal.

For example, to install the `pandas`

library, you can simply run the following in your terminal or command line:

`pip install pandas`

`pip`

will handle installing the library by downloading any requirements for it, such as other required dependencies. Feel free to install `pandas`

now or wait until we make it to that section of the course!

## How to Use External Libraries in Python

Now that you know how to import and install external libraries, let’s take a look at how to actually use them. We’ll take a closer look at the `statistics`

library again. Let’s begin by importing the library again.

```
# Importing the statistics library
import statistics
```

We can now access all the different functions available in the library. But, how do you actually know what functions are now available to you? For this, we can use the `dir()`

function, which will return all the different functions and objects available:

```
# Using the dir() function to see available functions
import statistics
print(dir(statistics))
# Returns: ['Counter', 'Decimal', 'Fraction', 'NormalDist', 'StatisticsError', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_coerce', '_convert', '_exact_ratio', '_fail_neg', '_find_lteq', '_find_rteq', '_isfinite', '_normal_dist_inv_cdf', '_ss', '_sum', 'bisect_left', 'bisect_right', 'erf', 'exp', 'fabs', 'fmean', 'fsum', 'geometric_mean', 'groupby', 'harmonic_mean', 'hypot', 'itemgetter', 'log', 'math', 'mean', 'median', 'median_grouped', 'median_high', 'median_low', 'mode', 'multimode', 'numbers', 'pstdev', 'pvariance', 'quantiles', 'random', 'sqrt', 'stdev', 'tau', 'variance']
```

One of these functions is the `median()`

function. The median is the middle value, or a value that separates the higher half of a distribution from the lower half.

In order to access the `median()`

function, you need to access the function from the library. It’s not enough to simply call `median()`

. We need to call `statistics.median()`

.

How can you learn how to use the `median()`

function? You can use the `help()`

function in order to gain access to its docstrings:

```
# Getting help!
import statistics
print(help(statistics.median)
# Returns:
# Median(data)
# Return the median (middle value) of numeric data.
# When the number of data points is odd, return the middle data point.
# When the number of data points is even, the median is interpolated by
# taking the average of the two middle values:
# >>> median([1, 3, 5])
# 3
# >>> median([1, 3, 5, 7])
# 4.0
```

We can see from the docstring how the method works. We can pass in a list of numbers to return the middle value.

This is great! We don’t need to develop or test a function ourselves, but we can simply import the function!

## Again, Why use Python Libraries?

Now that you have a much stronger understanding of Python libraries, let’s take another look at how much benefit they give us. We’ll take a look at calculating the standard deviation from scratch and then take a look at how to do this using a built-in library.

As a quick refresher, the standard deviation measures the amount of dispersion in a normally distributed set of data. It’s calculated using the following formula:

σ = √Σ (x_{i} – μ)^{2} / (n-1)

Let’s take a look at how we can calculate this using Python. Don’t worry if some of these pieces don’t yet make sense. If you are interested in doing a deep dive into this, check out this in-depth tutorial on the standard deviation in Python.

```
# Calculating a Standard Deviation from Scratch
sample_list = [170,155,160,185,145]
# Finding Mean value
sums = 0
for value in sample_list:
sums += value
mean = sums / len(sample_list)
# Finding square of difference of mean and each value
difference_squared = 0
for j in sample_list:
difference_squared += (j - mean) ** 2
# Finding Square Root
standard_deviation = (difference_squared / ((len(sample_list)) - 1)) ** 0.5
print(standard_deviation)
# Returns 15.25
```

This is a lot of code! There’s a lot of overhead and complexity in this that’s probably unnecessary. We could, put simply, just use the `stdev`

function from the statistics library. Let’s see how much easier that is!

```
from statistics import stdev
sample_list = [170,155,160,185,145]
print(stdev(sample_list))
# Returns 15.25
```

Isn’t that much, much easier? In the next section, you can complete a couple of checks to see solidify your understanding.

## Exercises

Use the sections below to check your understanding of external libraries in Python:

**What does it mean for a module to be part of the standard library?**

Modules that are part of the standard library come installed with Python. This means that you don’t need to check if the library is installed and that any user of Python (with the same version of Python) will have access to these libraries.

**How would you import the math library?**

`import math`

**How can you find out what functions a library has?**

Using the `dir()`

function.

**How would you install the pandas library?**

`pip install pandas`

## Conclusion and Recap

In this tutorial, you learned what external libraries are and how to use them in Python. You learned how to work with modules that are part of the standard library and how to work with ones you need to install. Some of the highlights of using external libraries are:

- Libraries simplify your code by allowing us to use pre-made functions and objects from other developers
- We can use the
`pip`

tool to install external libraries - When using external libraries, we can import the entire library or only some functions
- You can use the
`dir()`

function to get an understanding of what functions are available in the library - You can access information about different functions using the
`help()`

function

## Additional Resources

To learn more about related topics, check out these tutorials:

Hi Nik,

Thanks for this great tutorial series. I admire your passion for teaching.

I noticed that in the section ‘Again, Why use Python Libraries?’ you state ‘If you are interested in doing a deep dive into this, check out this in-depth tutorial on the standard deviation in Python.’ However, there is no link to a standard deviation tutorial. I would be interested to see that tutorial.

Hi John,

Sorry for the late response.

Thanks so much for your comment and for catching that! I have updated the article with the link to the standard deviation tutorial :).

Have a great day!

Hi Nik! Loved this lesson, thanks, just a little correction:

In the getting help part, there is an issue with the code that may prevent it from executing properly. Specifically, the help() function is meant to be called on a function or module object, but in this case, it is being called on the return value of statistics.median(). Since statistics.median() returns a value, not an object, calling help(statistics.median()) will generate a TypeError because the help() function does not accept an argument of type int or float.

To fix this issue, you can simply remove the parentheses after median so that help() is called on the function object itself, rather than its return value, like this:

import statistics

print(help(statistics.median))

Hey there! Thanks so much for letting me know! I appreciate it and have updated the post! 🙂

Lesson 10, done. Fantastic tutorial.

Thanks for the feedback! I’m glad you’re enjoying it! 🙂