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.
# Error occurred because only import submodule import scipy.integrate as integrate exact = scipy.integrate.ode(5.23) print(exact)
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
pip3 install scipy
After installation, you import with the syntax:
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)
<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)
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)
You can refer to more articles related to Scipy.
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:
- NameError: name ‘raw_input’ is not defined in Python
- NameError: function is not defined in Python
- NameError: name ‘false’ is not defined in Python
Carolyn Hise has three years of software development expertise. Strong familiarity with the following languages is required: Python, Typescript/Nodejs, .Net, Java, C++, and a strong foundation in Object-oriented programming (OOP).