user3276418
user3276418

Reputation: 1797

Cannot use scipy.stats

I get an errr when using scipy.stats. in a script after importing scipy.

AttributeError: 'module' object has no attribute 'stats'

Within script editor I can click on stats after typing scipy. from the pulldown menu, within python console I can not select python.stats from the pulldown menu, it's not there. I'm using pandas 2.7 and SciPy 0.13.0 Why is that? Any known issues?

Upvotes: 52

Views: 61724

Answers (3)

Talha Tayyab
Talha Tayyab

Reputation: 27277

if you import scipy alone like this:

import scipy

then you use:

scipy.stats

You will get:

AttributeError: module 'scipy' has no attribute 'stats'

You have to import like this:

import scipy.stats

or

import scipy
import stats

Upvotes: 0

Smaurya
Smaurya

Reputation: 187

This is expected. Most of the subpackages are not imported when you just do import scipy. There are a lot of them, with a lot of heavy extension modules that take time to load. You should always explicitly import the subpackages that you want to use.

https://github.com/scipy/scipy/issues/13618

Upvotes: 2

Josef
Josef

Reputation: 22897

expanding on my comment (to have a listed answer).

Scipy, as many other large packages, doesn't import all modules automatically. If we want to use the subpackages of scipy, then we need to import them directly.

However, some scipy subpackages load other scipy subpackages, so for example importing scipy.stats also imports a large number of the other packages. But I never rely on this to have the subpackage available in the namespace.

In many packages that use scipy, the preferred pattern is to import the subpackages to have them available by their names, for example:

>>> from scipy import stats, optimize, interpolate


>>> import scipy
>>> scipy.stats
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'stats'
>>> scipy.optimize
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'optimize'

>>> import scipy.stats
>>> scipy.optimize
<module 'scipy.optimize' from 'C:\Python26\lib\site-packages\scipy\optimize\__init__.pyc'>

Upvotes: 65

Related Questions