31

I updated my computer today, and am getting the following error message when I attempt to import pandas:

import pandas as pd        
AttributeError: module 'numpy' has no attribute '__version__'

I have tried the suggestions from the following links:

  1. AttributeError: 'module' object has no attribute '__version__'
  2. AttributeError: 'module' object has no attribute '__version__'

I do not have any files names numpy.py other than the numpy package.

I am running anaconda2 within windows 10, and have created a python 3.5 virtual environment.

Following is the full error message:

Python 3.5.5 | packaged by conda-forge | (default, Apr  6 2018, 16:03:44) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\j267156\AppData\Local\Continuum\anaconda2\envs\sf35\lib\site-packages\pandas\__init__.py", line 23, in <module>
    from pandas.compat.numpy import *
  File "C:\Users\j267156\AppData\Local\Continuum\anaconda2\envs\sf35\lib\site-packages\pandas\compat\numpy\__init__.py", line 10, in <module>
    _np_version = np.__version__
AttributeError: module 'numpy' has no attribute '__version__'

Edit: performing the following commands gives an error

import numpy
print(numpy.__file__)
AttributeError: module 'numpy' has no attribute '__file__'
print(numpy.version.version)
AttributeError: module 'numpy' has no attribute 'version'

Edit2: perfoming the following commands gives me the following errors:

import numpy as np
np.zeros(5)
#AttributeError: module 'numpy' has no attribute 'zeros'
vars(np).keys()
#dict_keys(['__name__', '__package__', '__spec__', '__loader__', '__doc__', '__path__'])

Edit3: the following commands result in the following output:

import numpy as np    
In [7]: np.__path__
    Out[7]: _NamespacePath(['C:\\Users\\j267156\\AppData\\Local\\Continuum\\anaconda2\\envs\\sf35\\lib\\site-packages\\numpy'])

    In [8]: np.__loader__
    Out[8]: <_frozen_importlib_external._NamespaceLoader at 0x2033a3c8080>
ktj1989
  • 679
  • 2
  • 8
  • 19
  • 3
    what does `import numpy; print(numpy.__file__)` say? – anthony sottile May 12 '18 at 22:16
  • as well you can try import numpy; numpy.version.version Out[1]: '1.14.3' – NaN May 12 '18 at 23:39
  • @AnthonySottile - I get this message: ----> 1 print(numpy.__file__) AttributeError: module 'numpy' has no attribute '__file__' – ktj1989 May 13 '18 at 00:52
  • @NaN I get this response ----> 1 numpy.version.version AttributeError: module 'numpy' has no attribute 'version' – ktj1989 May 13 '18 at 00:54
  • Try ... import numpy as np .... errors such as ... module 'numpy' has no attribute 'xxx' seem to be associated with errors in installation – NaN May 13 '18 at 04:36
  • A strange lack of attributes. What’s `vars(numpy).keys()`? – Davis Herring May 13 '18 at 14:59
  • @DavisHerring This is my output >>> vars(numpy).keys() dict_keys(['__path__', '__loader__', '__doc__', '__name__', '__spec__', '__package__']) – ktj1989 May 13 '18 at 16:46
  • @NaN I have tried installing and uninstalling both numpy and pandas with no success. import numpy as np gives no error, although when i call np.zeros(5) I get the following error: AttributeError: module 'numpy' has no attribute 'zeros' – ktj1989 May 13 '18 at 16:49
  • @ktj1989: OK: now what are the values `numpy.__path__`, `numpy.__loader__`, _etc._? – Davis Herring May 13 '18 at 17:41
  • @DavisHerring calling the following items: In [7]: np.__path__ Out[7]: _NamespacePath(['C:\\Users\\j267156\\AppData\\Local\\Continuum\\anaconda2\\envs\\sf35\\lib\\site-packages\\numpy']) In [8]: np.__loader__ Out[8]: <_frozen_importlib_external._NamespaceLoader at 0x2033a3c8080> – ktj1989 May 13 '18 at 22:49
  • 1
    This error occurs in my application when using 1.14.3 on Linux. If I do pip install --upgrade numpy (which upgrades to 1.14.5), the error goes away. – RNHTTR Jul 19 '18 at 20:01
  • I also had this issue with numpy 1.15.2, but it resolved with 1.15.4 – kibitzforu Dec 13 '18 at 16:47

6 Answers6

19

Try this

In Jupyter notebook first uninstall the existing numpy by using !pip uninstall numpy.

Then install it !pip install numpy. Now restart the kernel (Python Notebook). It will work.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Sathick Ibrahim
  • 199
  • 1
  • 5
  • https://github.com/ipython/ipyparallel/issues/349 this is relevant as well. – claude Mar 08 '19 at 21:05
  • 2
    This should be updated to use the modern `%pip install` magic. The use of an exclamation point with `pip` (and `conda`) can cause issues. The modern magics for `%pip` and `%conda` were added to insure installations to the proper environment, something the exclamation point alone fails to do. See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more about the modern `%pip install` and `%conda install` magic commands. – Wayne Oct 05 '22 at 17:51
7

You may have named your program/script 'numpy'. I also had that then i renamed my program and then it works. It happens because when you name your program similar to a module the import attribute clashes between programs and modules

AbdulRahim Khan
  • 121
  • 1
  • 3
  • Thanks, I did not do that though. – ktj1989 Jul 29 '19 at 17:55
  • 2
    That is what's happening - statsmodels has a file statsmodels/compat/numpy.py and similarly for pandas pandas\compat\numpy\__init__.py. It casues these problems I think – innisfree Feb 20 '20 at 05:29
  • Thanks! I'm new at Python and I named my little test `random.py` and got trouble with `module 'numpy' has no attribute 'zeros'` and such. After renaming it to `test_random.py` and moved it to another folder than `~/test` it worked. Gobsmacked. – Kjetil S. Sep 30 '20 at 13:11
4

Just had this issue after conda chose to update numpy from 1.13.3 to 1.14.3. Reverting numpy via procedure in http://blog.rtwilson.com/conda-revisions-letting-you-rollback-to-a-previous-version-of-your-environment/ sufficed to recover functionality, followed by pinning the version in the environment.

Kim Reece
  • 1,260
  • 9
  • 11
2

Have you tried this:

pip uninstall -y numpy
pip uninstall -y setuptools
pip install setuptools
pip install numpy
Tomasz Bartkowiak
  • 12,154
  • 4
  • 57
  • 62
  • I have not...I was using a conda installation. I ended up uninstalling and reinstalling conda. – ktj1989 May 28 '20 at 05:20
  • Found existing installation: numpy 1.20.1 Then installed 1.21.2 worked! had this when importing google cloud – Lan Si Oct 17 '21 at 22:26
0

I had this error today while I was running a "brew upgrade" on MacOS and upgrade failed on matplotlib.

<TLDR/>

I run pip (v 2.x) and pip3 installed. It turned out I had not installed numpy with pip3. I ran

pip3 install numpy

brew upgrade then ran successfully.

Nick
  • 328
  • 2
  • 10
0

Problem solved with:

  1. pip uninstall -y numpy
  2. pip uninstall -y setuptools
  3. pip install setuptools or conda install setuptools
  4. pip install bumpy or conda install numpy

Any one will work. Conda/pip

pppery
  • 3,731
  • 22
  • 33
  • 46