If you are a Python developer, you may have encountered the error message “NameError name ‘statistics’ is not defined” while working with the statistics library in Python. In this tutorial, we will explore the possible causes of this error and provide step-by-step instructions on how to fix it.

We will cover common causes of the error and provide solutions to help you get your code up and running quickly. So, let’s get started!

## Why does the `NameError: name 'statistics' is not defined`

error occur?

This error occurs when you try to use the statistics library in your Python code, but Python cannot find the statistics module in its namespace. The following are some of the scenarios in which this error usually occurs.

- You have not imported the statistics module.
- You have imported the statistics module using a different name.

## How to fix the `NameError: name 'statistics' is not defined`

?

The statistics library in Python is a built-in module that provides functions for calculating mathematical statistics of numeric data. It includes functions for measures of central tendency (mean, median, mode), measures of dispersion (variance, standard deviation), correlation, regression, and more. Since this library is part of the Python Standard Library, you don’t need to separately install it. You can simply import it and start using it.

Once imported, you can use the various functions provided by the library to perform statistical analysis on your data.

Let’s now look at the above scenarios in detail.

### The `statistics`

module is not imported

It can happen that you are trying to use the `statistics`

module without even importing it. This is because Python does not recognize the statistics library and its functions until it is imported into the code.

For example, let’s try to use the `statistics`

module without importing it and see what we get.

# note that statistics is not imported # get the median value in a list ls = [1, 3, 4, 5, 7] print(statistics.median(ls))

Output:

--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 5 1 # note that statistics is not imported 2 3 # get the median value in a list 4 ls = [1, 3, 4, 5, 7] ----> 5 print(statistics.median(ls)) NameError: name 'statistics' is not defined

We get a `NameError`

stating that the name `statistics`

is not defined. To use the `statistics`

library, you need to import it first.

import statistics # get the median value in a list ls = [1, 3, 4, 5, 7] print(statistics.median(ls))

Output:

4

Here, we are importing the `statistics`

module first and then using it to calculate the median value in a list of integers. You can see that we did not get any errors here.

You can also get a `NameError`

if you are importing only specific parts of the library and then trying to access the entire `statistics`

library. For example –

from statistics import median # get the median value in a list ls = [1, 3, 4, 5, 7] print(statistics.median(ls))

Output:

--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 5 3 # get the median value in a list 4 ls = [1, 3, 4, 5, 7] ----> 5 print(statistics.median(ls)) NameError: name 'statistics' is not defined

We get a `NameError`

here because we are importing only the `median()`

function from the `statistics`

library but we are trying to access the entire library. To resolve the above error, either only use the specific method imported or import the `statistics`

library altogether.

### The `statistics`

module is imported using a different name

If you import the statistics module using a different name, for example `import statistics as st`

, and then try to use the name “statistics” to use it, you will get a `NameError`

because the name “statistics” is not defined in your current namespace.

Let’s look at an example.

import statistics as st # get the median value in a list ls = [1, 3, 4, 5, 7] print(statistics.median(ls))

Output:

--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 5 3 # get the median value in a list 4 ls = [1, 3, 4, 5, 7] ----> 5 print(statistics.median(ls)) NameError: name 'statistics' is not defined

We get a `NameError: name 'statistics' is not defined`

. This is because we have imported the `statistics`

module with the name `st`

but we’re trying to use it using the name `statistics`

.

To fix this error, you can either access statistics using the name that you have used in the import statement or import statistics without an alias.

import statistics as st # get the median value in a list ls = [1, 3, 4, 5, 7] print(st.median(ls))

Output:

4

In the above example, we are importing `statistics`

as `st`

and then using `st`

to access the `statistics`

module’s methods.

Alternatively, as seen in the example in the previous section, you can import `statistics`

without any aliases and simply use `statistics`

to avoid the `NameError`

.

## Conclusion

In conclusion, encountering a `NameError: name 'statistics' is not defined`

error can be frustrating, but it is a common issue that can be easily fixed. By ensuring that the `statistics`

module is imported correctly and that the correct syntax is used when calling its functions, you can avoid this error and successfully execute your code.

You might also be interested in –