How does one install/fix a failed numpy installati

2019-07-29 11:25发布

I was trying to use numpy in ubuntu but it fails with error:

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.

the catch is that I am using python 3.5 (that I install with the suggestions in this question) instead of the one that comes with the default 3.4. So I installed it as that answer suggested and then I installed numpy with:

pip3 install --target=/usr/local/lib/python3.5/dist-packages numpy

so that it would install to python3.5. After that if I try to use numpy I have the above error. However if I use it on the default python 3.4 I don't have that error. I obviously tried to re-install/update/fix numpy but it didn't do anything. Specifically I did:

pip install --target=/usr/local/lib/python3.5/dist-packages --upgrade numpy

is there a way to install numpy while using python 3.5 in ubuntu? How do I actually fix numpy? Why does my numpy work for 3.4 but not for 3.5?


the full error is:

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

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.5/dist-packages/numpy/__init__.py", line 142, in <module>
        from . import add_newdocs
      File "/usr/local/lib/python3.5/dist-packages/numpy/add_newdocs.py", line 13, in <module>
        from numpy.lib import add_newdoc
      File "/usr/local/lib/python3.5/dist-packages/numpy/lib/__init__.py", line 8, in <module>
        from .type_check import *
      File "/usr/local/lib/python3.5/dist-packages/numpy/lib/type_check.py", line 11, in <module>
        import numpy.core.numeric as _nx
      File "/usr/local/lib/python3.5/dist-packages/numpy/core/__init__.py", line 24, 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. 

as a caveat, I am actually doing all this by first using the cpu tensorflow docker image. So I did all this inside a docker container that has as base image the cpu tensorflow image. In particular:

FROM gcr.io/tensorflow/tensorflow:latest-devel-py3

I also did something sort of hacky to check if at leas that would work and it didn't. What I did was append the path to my python3.4 packages in the sys.path for python3.5. So I went to a command line for python3.5 and then I imported sys and went ahead to add the following string to sys.path:

sys.path.append('/usr/local/lib/python3.4/dist-packages')

it manages to import other libraries I have but it fails to import numpy however:

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/usr/local/lib/python3.4/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/local/lib/python3.4/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/local/lib/python3.4/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/local/lib/python3.4/dist-packages/numpy/core/__init__.py", line 14, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

it seems its fine with most of the other packages except numpy. Possibly when I pip installed numpy it automatically went for the python 3.4 version or something?


As an extra, if one can make their answer work in a Dockerfile, that would incredibly helpful!

1条回答
虎瘦雄心在
2楼-- · 2019-07-29 11:57

You cannot mix and match between Python versions. Every version needs its own copy of NumPy. This is because Python does not provide a cross-version binary compatibility. For pure Python packages (that have no compiled code, as it is the case for NumPy), it could work in principle but the environment is hard to manage. Some distributions share the .py files with symlinks.

The first thing is to remove the wrong install. I will focus only on getting the python 3.5 install to work

cd /usr/local/lib/python3.5/dist-packages

Warning before continuing using the rm command should be done with caution, even more so as you need root privilege to operate in /usr.

rm -r numpy

(as root).

Then, you need pip. You can install it with a file from the pypi webpages: https://pip.pypa.io/en/latest/installing/

cd
wget https://bootstrap.pypa.io/get-pip.py

and install with

python3.5 get-pip.py

You can do this for the whole computer or just the current user (with the --user option). Once pip is installed,

python3.5 -m pip install -U numpy

should do.

If there is no binary package for you version of Python and pip starts to compile things and fails to do do, install the package python3.5-dev.

查看更多
登录 后发表回答