If you are working with Python and trying to use the numpy library, you may encounter the “NameError: name ‘numpy’ is not defined” error. In this tutorial, we will explore why this error occurs and the steps required to fix it such that your Python code can successfully run without errors.

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 'numpy' is not defined`

error occur?

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

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

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

?

Before we proceed with the above scenario and their solutions, make sure that you have the `numpy`

module installed. If `numpy`

is not installed and you try to import it, you’ll get a `ModuleNotFoundError`

. Alternatively, you can check if you have numpy installed by running `pip list`

in your terminal or command prompt. If numpy is not listed, install it by running `pip install numpy`

.

If you are using a virtual environment or a different Python environment, make sure numpy is installed in that environment. You can activate the environment and run `pip list`

to check if numpy is installed.

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

### The `numpy`

module is not imported

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

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

For example, let’s try to use `numpy`

without importing it and see what we get.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# note that numpy is not imported # create an array ar = numpy.array([1, 2, 3, 4]) # print the array print(ar)

Output:

--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 # note that numpy is not imported 2 3 # create an array ----> 4 ar = numpy.array([1, 2, 3, 4]) 5 # print the array 6 print(ar) NameError: name 'numpy' is not defined

We get a `NameError`

stating that the name `numpy`

is not defined. To use `numpy`

, you need to import it first.

import numpy # create an array ar = numpy.array([1, 2, 3, 4]) # print the array print(ar)

Output:

[1 2 3 4]

Here, we are importing the `numpy`

module first and then using it. You can see that we did not get any errors here.

Note that generally people use the `import numpy as np`

statement to import the numpy module with the alias `np`

.

### The `numpy`

module is imported using a different name

If you import the numpy module using a different name, for example `import numpy as np`

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

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

Let’s look at an example.

import numpy as np # create an array ar = numpy.array([1, 2, 3, 4]) # print the array print(ar)

Output:

--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 import numpy as np 3 # create an array ----> 4 ar = numpy.array([1, 2, 3, 4]) 5 # print the array 6 print(ar) NameError: name 'numpy' is not defined

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

. This is because we have imported the `numpy`

module with the name `np`

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

.

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

import numpy as np # create an array ar = np.array([1, 2, 3, 4]) # print the array print(ar)

Output:

[1 2 3 4]

In the above example, we are importing `numpy`

as `np`

and then using `np`

to access the `numpy`

module’s methods.

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

without any aliases and simply use `numpy`

to avoid the `NameError`

.

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

error?

You can similarly get the `NameError: name 'np' is not defined`

if you’ve not used `np`

as the name for numpy while importing it. For example.

import numpy # create an array ar = np.array([1, 2, 3, 4]) # print the array print(ar)

Output:

--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 import numpy 3 # create an array ----> 4 ar = np.array([1, 2, 3, 4]) 5 # print the array 6 print(ar) NameError: name 'np' is not defined

We get this error because the name `np`

does not exit in the namespace. To fix this error, use the import statement `import numpy as np`

.

import numpy as np # create an array ar = np.array([1, 2, 3, 4]) # print the array print(ar)

Output:

[1 2 3 4]

We don’t get an error now.

## Conclusion

In conclusion, the “NameError: name ‘numpy’ is not defined” error can occur due to two scenarios – numpy is not imported or numpy is imported with a different name. If numpy is not imported, you can simply import it using the command `import numpy`

. On the other hand, if numpy is imported with a different name, for example, `import numpy as np`

you can use “np” to use numpy methods. By following these steps, you can easily fix the “NameError: name ‘numpy’ is not defined” error and continue with your computation tasks utilizing numpy.

You might also be interested in –