con
con

Reputation: 6093

Cannot import numpy library in Python 3.8.0

I'm attempting to load a numpy library into python 3.8.0 on Ubuntu.

from numpy import loadtxt

gives a very verbose error, without a solution:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 16, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray' from partially initialized module 'numpy.core' (most likely due to a circular import) (/usr/lib/python3/dist-packages/numpy/core/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/usr/lib/python3/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python3/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python3/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 26, in <module>
    raise ImportError(msg)
ImportError: 
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

Original error was: cannot import name 'multiarray' from partially initialized module 'numpy.core' (most likely due to a circular import) (/usr/lib/python3/dist-packages/numpy/core/__init__.py)

Based on advice that I saw on Python circular importing? I changed the import line to import all of numpy:

import numpy

but this gives the same error.

As the error suggests, I tried re-installing numpy with pip:

sudo pip install --upgrade --force-reinstall numpy

the same error happens with

sudo pip3 install --upgrade --force-reinstall numpy

which worked, and installed numpy-1.19.5. However, this didn't fix the import problem.

I tried implementing a solution from https://github.com/numpy/numpy/issues/9047 which involves setting

export PYTHONPATH=/usr/lib/python3/dist-packages/$PYTHONPATH in ~/.bashrc, but this failed as well.

How can I import numpy?

Upvotes: 1

Views: 8099

Answers (2)

con
con

Reputation: 6093

The problem was that pip and python were linked to different versions.

The solution was to install via sudo /usr/bin/python3.8 -m pip uninstall numpy and then sudo /usr/bin/python3.8 install numpy

Upvotes: 4

Gustin
Gustin

Reputation: 191

After looking into circular imports for a bit, it looks like you might just be better off doing import numpy and altering your code accordingly rather than using the from syntax

Upvotes: 0

Related Questions