module 'numpy' has no attribute 'dtype'
Asked Answered
T

3

0

When importing sklearn datasets eg.

from sklearn.datasets import fetch_mldata
from sklearn.datasets import fetch_openml

I get the error

Traceback (most recent call last):
  File "numbers.py", line 1, in <module>
    from sklearn.datasets import fetch_openml
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/__init__.py", line 64, in <module>
    from .base import clone
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/base.py", line 11, in <module>
    import numpy as np
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/core/__init__.py", line 93, in <module>
    from . import numerictypes as nt
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/core/numerictypes.py", line 86, in <module>
    import numbers
  File "/Users/airocoop/repos/Numbers/numbers.py", line 1, in <module>
    from sklearn.datasets import fetch_openml
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/datasets/__init__.py", line 6, in <module>
    from .base import load_breast_cancer
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/datasets/base.py", line 20, in <module>
    from ..utils import Bunch
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/sklearn/utils/__init__.py", line 10, in <module>
    from scipy.sparse import issparse
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/scipy/__init__.py", line 72, in <module>
    from numpy.random import rand, randn
  File "/anaconda2/envs/numbers/lib/python3.5/site-packages/numpy/random/__init__.py", line 143, in <module>
    from .mtrand import *
  File "numpy.pxd", line 87, in init mtrand
AttributeError: module 'numpy' has no attribute 'dtype'

I am not sure why I get this

I don't get this error when running things from a jupyter notebook, which is also weird. Any help on this issue would be greatly appreciated

Thresher answered 11/3, 2019 at 19:19 Comment(4)
Seems like you have inconsistent versions of sklearn and numpy.Zoon
How, exactly is this being run?Denaturalize
It is also possible that the numbers environment inherited a different version of numpy from anaconda2 base environmentWindgall
@Denaturalize I am running the command $ python numbers.py from a terminal on mac OS, in the numbers environment. runing $ conda list gives me (among other things): { numpy 1.16.2 py36hacdab7b_0 }, { numpy-base 1.16.2 py36h6575580_0}, { python 3.6.8 haf84260_0 }, { scikit-learn 0.20.2 py36h27c97d8_0}Thresher
T
7

I figured this out. The answer is that the file I was running was named numbers.py. This screws the whole thing up. If you have this problem check to make sure you don't have a file in the directory called numbers.py

Easy way to check is move the file with the import statement to a different directory and try running it.

Thresher answered 14/3, 2019 at 15:48 Comment(1)
I had this issue as well but the file causing the problem was called "signal.py" in my case. Rather than saying "make sure you don't have a file in the directory called numbers.py", you must say something like "check any recent .py file you've added and try changing its name". When I had the issue I read your answer several times but I didn't pay much attention to it because I didn't have any file called "numbers.py"...Husk
M
6

Broken installation.

Do this:

1)

conda install numpy=1.13

or 2)

pip install numpy --upgrade
Macnamara answered 11/3, 2019 at 19:21 Comment(2)
what if the OP isn't using pip to manage this particular dependency? pip was notoriously problematic when trying to install numpy/scipy on windows, requiring at one point having people install a fortran compiler. I hear the situation has gotten a lot better, but a lot of people use conda. Note, OP is working with /anaconda2/envs/numbers/lib/python3.5Denaturalize
Hey guys thanks for the replies. Your right I am using conda but no luck with trying: $ conda remove -n numbers numpy $ conda install numpy (numbers is my env) also tried the $ conda install numpy=1.13Thresher
A
0

I got the same problem, but for a very different reason. I post here at this is the first question coming out when searching for 'AttributeError: module 'numpy' has no attribute 'dtype' and that may help people like me.

Using:

assert not isinstance(param.dtype, np.dtypes.StrDType)

is valid with Numpy 2.0 but not in Numpy 1.21 that I was using. I put instead:

np.issubdtype(param.dtype, np.str_)  # OK for Numpy 1.21
Arrowy answered 3/7, 2024 at 16:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.