pypa / pip

The Python package installer
https://pip.pypa.io/
MIT License
9.45k stars 3k forks source link

ImportError: numpy.core.multiarray failed to import #6270

Open 1fish2 opened 5 years ago

1fish2 commented 5 years ago

Environment

Description I spent a few hours debugging

ImportError: numpy.core.multiarray failed to import

NumPy imports just fine but an installed package could not import numpy. (I don't actually know if that installed package could ever import numpy.)

Lots of discussions with this error message on the web, including the TensorFlow discussion with various approaches and limited successes indicate that other people have wrangled with this problem as well.

After ruling out a bunch of hypotheses (reboot; reinstall the pip that can't import numpy; reinstall python; reinstall numpy; uninstall alternate pyenv virtualenvs that contained other versions of numpy; create a minimal test virtualenv containing only numpy, pip, setuptools, wheel, and the package that can't import numpy; uninstall all other pyenv virtualenvs that contain numpy except the system's own python which is locked by System Integrity Protection), I finally fixed it with this:

pip install --force-reinstall --no-cache-dir package-that-can't-import-numpy

In other words, re-download and reinstall the calling package. (In my case, the relevant package was stochastic-arrow==0.1.4.)

Before- & after-tests showed that this step fixed the problem in my minimal test virtualenv and separately in my main working virtualenv.

Thinking about likely causes in common to the reported cases around the web leads to these ideas:

Expected behavior Packages that happily import numpy.

How to Reproduce Alas, I do not know how to get the computer back into the broken state.

Output

$ python -c 'import numpy'
$ python -c 'from arrow import StochasticSystem'
ImportError: No module named _multiarray_umath
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/var/pyenv/versions/test/lib/python2.7/site-packages/arrow/__init__.py", line 1, in <module>
    from reference import derive_reactants, calculate_dependencies, evolve, GillespieReference
  File "/usr/local/var/pyenv/versions/test/lib/python2.7/site-packages/arrow/reference.py", line 6, in <module>
    import obsidian
ImportError: numpy.core.multiarray failed to import
chrahunt commented 5 years ago

When we download source distributions (which stochastic-arrow provides), we typically build a wheel and cache and install it. For legacy builds (for projects that don't use pyproject.toml, the build takes place in whatever the current environment is using whatever dependencies are available. If stochastic-arrow was installed in an environment with a numpy==1.X and then installed in another environment with numpy==1.Y, the latter environment would get the cached wheel and that could potentially cause issues like what you're seeing.

There are some related issues like #2882, however I can't find one that matches this exactly.

chrahunt commented 5 years ago

4582 is the issue I was thinking of, specifically this comment.