Cannot import Numpy and Pandas

I have installed numpy and pandas using pip. After executing my python code it shows the following error:

Traceback (most recent call last): File "demo.py", line 1, in <module> import numpy as np File "/home/tauhid/.local/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module> from . import core File "/home/tauhid/.local/lib/python3.6/site-packages/numpy/core/__init__.py", line 59, in <module> from . import numeric File "/home/tauhid/.local/lib/python3.6/site-packages/numpy/core/numeric.py", line 3093, in <module> from . import fromnumeric File "/home/tauhid/.local/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 17, in <module> from . import _methods File "/home/tauhid/.local/lib/python3.6/site-packages/numpy/core/_methods.py", line 158, in <module> _NDARRAY_ARRAY_FUNCTION = mu.ndarray.__array_function__
AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__'

I have tried uninstalling and reinstalling the packages, but same error reoccurs.

2 Answers

The import numpy as np, pandas command won't work unless you run it with Python 3.x, because you installed numpy and pandas for Python 3.6. To install Numpy and Pandas for Python 2.7 open the terminal and type:

sudo apt install python-numpy # 20.04 and earlier
sudo apt install python-pandas # 18.04 and earlier 

The equivalent of the above command for Python 3.x is:

sudo apt install python3-numpy python3-pandas 

Numpy and Pandas are currently installed locally in the home directory, not globally. To install the latest stable versions of Numpy and Pandas globally with pip run this command:

sudo python3 -m pip install numpy pandas 

Before you make a hasty decision to install Numpy and Pandas globally with pip, remember that Numpy and Pandas packages are in the default Ubuntu repositories too.

Try this:

sudo python -m pip install --upgrade numpy
sudo python -m pip install --upgrade scipy

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like