1

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?

1
  • 1
    You can debug circular imports with python -v. Commented Feb 19, 2021 at 21:27

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I've solved the problem, which was because of >= 3 installations of python on the same computer. I'm a perl guy, so I'm not used to dealing with these problems... I should close the question since it was solved by sudo python3.8 -m pip install numpy after uninstallation the same way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.