How To Fix NameError: name ‘scipy’ is not defined in Python

NameError: name 'scipy' is not defined in Python

SciPy is a scientific computation library that uses NumPy underneath. When you start using it, you may get an error NameError: name ‘scipy’ is not defined, possibly because you haven’t imported this library. This article will give a few situations and how to fix them.

What is SciPy ?

Scipy is a very convenient set of algorithms built on top of the NumPy library. It powers Python by providing users with commands and high-level classes to manipulate data intuitively. When you want to use a function from a module, you need to import that module.

Why does the error “NameError: name ‘scipy’ is not defined in Python” occur?

This error occurs because you have not installed the “scipy” module or you do not import it into the code. The cause is that you import the scipy.integrate module and assign it to a variable named integrate. That’s why you are getting this error. Scipy has yet to be imported into your program.

Example:

# Error occurred because only import submodule
import scipy.integrate as integrate

exact = scipy.integrate.ode(5.23)
print(exact)

Output:

NameError: name 'scipy' is not defined

The next possible cause is that you do not have Scipy installed. You can check if the module is installed or not. If not, install it.

Solution for NameError: name ‘scipy’ is not defined

Try reinstalling Scipy

Reinstalling Scipy, because maybe Scipy is not installed. You can use the following syntax

pip install scipy

or

pip3 install scipy

After installation, you import with the syntax:

import scipy

And you can use Scipy:

# Always have to import scipy
import scipy
import scipy.integrate as integrate

exact = scipy.integrate.ode(5.23)
print(exact)

Output:

<scipy.integrate._ode.ode object at 0x7f3c687d1d00>

Using Scipy’s sub-modules

If you don’t want to add “scipy.” into each statement, you can import each sub-modules, and we can use it directly.

# Always have to import scipy
import scipy
import scipy.special as extra
import scipy.integrate as immaculate

dimension = immaculate.quad(lambda x: extra.jv(3.5, x), 0, 5.6)
print(dimension)

Output:

(1.090928018062381, 1.8708133067367122e-09)

The Scipy library includes several submodules with specific functionality. If you want to learn, you can type “help()” command on Scipy.
You can also import only the functions you need.

import scipy
from scipy.integrate import quad

def integrand( dimensionX, dimensionA, dimensionB):
	return dimensionA * dimensionX ** 2 + dimensionB

dimensionA = 2
dimensionB = 1

# Using only the quad() function
result = quad(integrand, 0, 1, args = (dimensionA, dimensionB))
print(result)

Output:

(1.6666666666666667, 1.8503717077085944e-14)

You can refer to more articles related to Scipy.

Summary

Through the article, we have found the cause of the Error. The reason is not to import the Scipy module first. Hope you get it fixed soon.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *