# Import modules and libraries in Python

Previously, you explored libraries and modules. You learned that a **module** is a Python file that contains additional functions, variables, classes, and any kind of runnable code. You also learned that a **library** is a collection of modules that provide code users can access in their programs. You were introduced to a few modules in the Python Standard Library and a couple of external libraries. In this reading, you'll learn how to import a module that exists in the Python Standard Library and use its functions. You'll also expand your understanding of external libraries.

## The Python Standard Library

The **Python Standard Library** is an extensive collection of Python code that often comes packaged with Python. It includes a variety of modules, each with pre-built code centered around a particular type of task.

For example, you were previously introduced to the the following modules in the Python Standard Library:

<div class="rc-CML" dir="auto" id="bkmrk-the-re-module%2C-which"><div><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml" data-track-page="item_layout" role="presentation"><div data-track="true" data-track-action="click" data-track-app="open_course_home" data-track-component="cml_link" data-track-page="item_layout"><div class="css-1kgqbsw" data-testid="cml-viewer">- The <var>re</var> module, which provides functions used for searching for patterns in log files
- The <var>csv</var> module, which provides functions used when working with <var>.csv</var> files
- The <var>glob</var> and <var>os</var> modules, which provide functions used when interacting with the command line
- The <var>time</var> and <var>datetime</var> modules, which provide functions used when working with timestamps

</div></div></div></div></div>Another Python Standard Library module is <var>statistics</var>. The <var>statistics</var> module includes functions used when calculating statistics related to numeric data. For example, <var>mean()</var> is a function in the <var>statistics</var> module that takes numeric data as input and calculates its mean (or average). Additionally, <var>median()</var> is a function in the <var>statistics</var> module that takes numeric data as input and calculates its median (or middle value).

## How to import modules from the Python Standard Library

To access modules from the Python Standard Library, you need to import them. You can choose to either import a full module or to only import specific functions from a module.

### **Importing an entire module**

To import an entire Python Standard Library module, you use the <var>import</var> keyword. The <var>import</var> keyword searches for a module or library in a system and adds it to the local Python environment. After <var>import</var>, specify the name of the module to import. For example, you can specify <var>import statistics</var> to import the <var>statistics</var> module. This will import all the functions inside of the <var>statistics</var> module for use later in your code.

As an example, you might want to use the <var>mean()</var> function from the <var>statistics</var> module to calculate the average number of failed login attempts per month for a particular user. In the following code block, the total number of failed login attempts for each of the twelve months is stored in a list called <var>monthly\_failed\_attempts</var>. Run this code and analyze how <var>mean()</var> can be used to calculate the average of these monthly failed login totals and store it in <var>mean\_failed\_attempts\\</var>

```python
import statistics
monthly_failed_attempts = [20, 17, 178, 33, 15, 21, 19, 29, 32, 15, 25, 19]
mean_failed_attempts = statistics.mean(monthly_failed_attempts)
print("mean:", mean_failed_attempts)
```

```
mean: 35.25
```

The output returns a mean of <var>35.25</var>. You might notice the outlying value of <var>178</var> and want to find the middle value as well. To do this through the <var>median()</var> function, you can use the following code:

```python
import statistics
monthly_failed_attempts = [20, 17, 178, 33, 15, 21, 19, 29, 32, 15, 25, 19]
median_failed_attempts = statistics.median(monthly_failed_attempts)
print("median:", median_failed_attempts)
```

```
median: 20.5
```

This gives you the value of <var>20.5</var>, which might also be useful for analyzing the user's failed login attempt statistics.

**Note:** When importing an entire Python Standard Library module, you need to identify the name of the module with the function when you call it. You can do this by placing the module name followed by a period (<var>.</var>) before the function name. For example, the previous code blocks use <var>statistics.mean()</var> and <var>statistics.median()</var> to call those functions.

### **Importing specific functions from a module**

To import a specific function from the Python Standard Library, you can use the <var>from</var> keyword. For example, if you want to import just the <var>median()</var> function from the <var>statistics</var> module, you can write <var>from statistics import median</var>.

To import multiple functions from a module, you can separate the functions you want to import with a comma. For instance, <var>from statistics import mean, median</var> imports both the <var>mean()</var> and the <var>median()</var> functions from the <var>statistics</var> module.

An important detail to note is that if you import specific functions from a module, you no longer have to specify the name of the module before those functions. You can examine this in the following code, which specifically imports only the <var>median()</var> and the <var>mean()</var> functions from the <var>statistics</var> module and performs the same calculations as the previous examples:

```python
from statistics import mean, median
monthly_failed_attempts = [20, 17, 178, 33, 15, 21, 19, 29, 32, 15, 25, 19]
mean_failed_attempts = mean(monthly_failed_attempts)
print("mean:", mean_failed_attempts)
median_failed_attempts = median(monthly_failed_attempts)
print("median:", median_failed_attempts)
```

```
mean: 35.25
median: 20.5
```

It is no longer necessary to specify <var>statistics.mean()</var> or <var>statistics.median()</var> and instead the code incorporates these functions as <var>mean()</var> and <var>median()</var>.

## External libraries

In addition to the Python Standard Library, you can also download external libraries and incorporate them into your Python code. For example, previously you were introduced to Beautiful Soup (<var>bs4</var>) for parsing HTML files and NumPy (<var>numpy</var>) for arrays and mathematical computations. Before using them in a Jupyter Notebook or a Google Colab environment, you need to install them first.

To install a library, such as <var>numpy</var>, in either environment, you can run the following line prior to importing the library:

<var>%pip install numpy</var>

This installs the library so you can use it in your notebook.

After a library is installed, you can import it directly into Python using the <var>import</var> keyword in a similar way to how you used it to import modules from the Python Standard Library. For example, after the <var>numpy</var> install, you can use this code to import it:

<var>import numpy</var>

## Key takeaways

The Python Standard Library contains many modules that you can import, including <var>re</var>, <var>csv</var>, <var>os</var>, <var>glob</var>, <var>time</var>, <var>datetime</var>, and <var>statistics</var>. To import these modules, you must use the <var>import</var> keyword. Syntax varies depending on whether or not you want to import the entire module or just specific functions from it. External libraries can also be imported into Python, but they need to be installed first.