If you are working with Python and trying to use the scipy library, you may encounter the “NameError: name ‘scipy’ is not defined” error. In this tutorial, we will explore why this error occurs and the steps required to fix it so 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 'scipy' is not defined
error occur?
This error occurs when you try to use the scipy library in your Python code, but Python cannot find the scipy module in its namespace. The following are some of the scenarios in which this error usually occurs.
- You have not imported the scipy module.
- You have imported the scipy module using a different name.
How to fix the NameError: name 'scipy' is not defined
?
Before we proceed with the above scenario and their solutions, make sure that you have the scipy
module installed. If scipy
is not installed and you try to import it, you’ll get a ModuleNotFoundError
. Alternatively, you can check if you have scipy installed by running pip list
in your terminal or command prompt. If scipy is not listed, install it by running pip install scipy
.
If you are using a virtual environment or a different Python environment, make sure scipy is installed in that environment. You can activate the environment and run pip list
to check if scipy is installed.
Let’s now look at the above scenarios in detail.
The scipy
module is not imported
It can happen that you are trying to use the scipy
module without even importing it. This is because Python does not recognize the scipy library and its functions until it is imported into the code.
For example, let’s try to use scipy
without importing it and see what we get.
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 scipy is not imported # create a matrix matrix = scipy.array([[1, 2], [3, 4]]) # get the inverse matrix inv_matrix = scipy.linalg.inv(matrix) # print the inverse matrix print(inv_matrix)
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 # note that scipy is not imported 2 3 # create a matrix ----> 4 matrix = scipy.array([[1, 2], [3, 4]]) 5 # get the inverse matrix 6 inv_matrix = scipy.linalg.inv(matrix) NameError: name 'scipy' is not defined
We get a NameError
stating that the name scipy
is not defined. To use scipy
, you need to import it first.
import scipy # create a matrix matrix = scipy.array([[1, 2], [3, 4]]) # get the inverse matrix inv_matrix = scipy.linalg.inv(matrix) # print the inverse matrix print(inv_matrix)
Output:
[[-2. 1. ] [ 1.5 -0.5]]
Here, we are importing the scipy
module first and then using it. You can see that we did not get any errors here.
Note that instead of importing the entire scipy
library, you can also import only specific parts of the library that you need. For example, in the above code, we can use the import statements from scipy import array
and from scipy.linalg import inv
and use the functions using their names.
The scipy
module is imported using a different name
If you import the scipy module using a different name, for example import scipy as sp
, and then try to use the name “scipy” to use it, you will get a NameError
because the name “scipy” is not defined in your current namespace.
Let’s look at an example.
import scipy as sp # create a matrix matrix = scipy.array([[1, 2], [3, 4]]) # get the inverse matrix inv_matrix = scipy.linalg.inv(matrix) # print the inverse matrix print(inv_matrix)
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 import scipy as sp 3 # create a matrix ----> 4 matrix = scipy.array([[1, 2], [3, 4]]) 5 # get the inverse matrix 6 inv_matrix = scipy.linalg.inv(matrix) NameError: name 'scipy' is not defined
We get a NameError: name 'scipy' is not defined
. This is because we have imported the scipy
module with the name sp
but we’re trying to use it using the name scipy
.
To fix this error, you can either access scipy using the name that you have used in the import statement or import scipy without an alias.
import scipy as sp # create a matrix matrix = sp.array([[1, 2], [3, 4]]) # get the inverse matrix inv_matrix = sp.linalg.inv(matrix) # print the inverse matrix print(inv_matrix)
Output:
[[-2. 1. ] [ 1.5 -0.5]]
In the above example, we are importing scipy
as sp
and then using sp
to access the scipy
module’s methods.
Alternatively, as seen in the example in the previous section, you can import scipy
without any aliases and simply use scipy
to avoid the NameError
.
How to fix the NameError: name 'sp' is not defined
error?
You can similarly get the NameError: name 'sp' is not defined
if you’ve not used sp
as the name for scipy while importing it. For example.
import scipy # create a matrix matrix = sp.array([[1, 2], [3, 4]]) # get the inverse matrix inv_matrix = sp.linalg.inv(matrix) # print the inverse matrix print(inv_matrix)
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 import scipy 3 # create a matrix ----> 4 matrix = sp.array([[1, 2], [3, 4]]) 5 # get the inverse matrix 6 inv_matrix = sp.linalg.inv(matrix) NameError: name 'sp' is not defined
We get this error because the name sp
does not exit in the namespace. To fix this error, use the import statement import scipy as sp
.
import scipy as sp # create a matrix matrix = sp.array([[1, 2], [3, 4]]) # get the inverse matrix inv_matrix = sp.linalg.inv(matrix) # print the inverse matrix print(inv_matrix)
Output:
[[-2. 1. ] [ 1.5 -0.5]]
We don’t get an error now.
Conclusion
In conclusion, the “NameError: name ‘scipy’ is not defined” error can occur due to two scenarios – scipy is not imported or scipy is imported with a different name. If scipy is not imported, you can simply import it using the command import scipy
. On the other hand, if scipy is imported with a different name, for example, import scipy as sp
you can use “sp” to use scipy methods. By following these steps, you can easily fix the “NameError: name ‘scipy’ is not defined” error and continue with your computation tasks utilizing scipy.
You might also be interested in –