简体   繁体   English

无法使用 python3 在 numpy 中导入名称“multiarray”

[英]cannot import name 'multiarray' in numpy with python3

I usually work with python 2.7 but this time i have to test a script in python3.我通常使用 python 2.7,但这次我必须在 python3 中测试脚本。

It is already installed on my computer, however when i start "python3", then go "import numpy", it show me "cannot import name 'multiarray'.它已经安装在我的计算机上,但是当我启动“python3”,然后转到“import numpy”时,它显示“无法导入名称‘multiarray’。

I even installed anaconda3 to test, but nothing happens我什至安装了anaconda3进行测试,但没有任何反应

myName:~/anaconda3/bin$ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 180, in     <module>
    from . import add_newdocs
  File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 14, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

I saw that it is looking in the lib/python2.7, but i cannot find what to do to get him search in the python3 library.我看到它在 lib/python2.7 中查找,但我找不到让他在 python3 库中搜索的方法。

I already tried python -m pip install numpy , and tried to create a virtualenv in python3 but i still get the same error.我已经尝试过python -m pip install numpy ,并尝试在 python3 中创建一个 virtualenv 但我仍然遇到相同的错误。

I cannot figure what to do.我想不通该怎么办。 Can someone help me ?有人能帮我吗 ?

I would like to add, i cannot start command with 'sudo' as i'm working on a client machine.我想补充一点,我在客户端机器上工作时无法使用“sudo”启动命令。

edit:编辑:

i tried @gehbiszumeis answer and got this:我尝试了@gehbiszumeis 的回答并得到了这个:

myName:~ $ cd anaconda3/bin/
myName:~/anaconda3/bin $ source activate /home/myName/anaconda3
(base) myName:~/anaconda3/bin $ conda list numpy
# packages in environment at /home/myName/anaconda3:
#
# Name                    Version                   Build  Channel
numpy                     1.14.3           py36hcd700cb_1  
numpy-base                1.14.3           py36h9be14a7_1  
numpydoc                  0.8.0                    py36_0  
(base) myName:~/anaconda3/bin $ python3
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 180, in <module>
    from . import add_newdocs
  File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 14, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

I see there is numpy 36 installed when i type conda list numpy, but it seems not to work.. Did i miss something ?我看到当我输入 conda list numpy 时安装了 numpy 36,但它似乎不起作用..我错过了什么吗?

edit2: After @Pal Szabo method, i tested command python3 -m pip install --upgrade pip and got this error :编辑2:在@Pal Szabo 方法之后,我测试了命令python3 -m pip install --upgrade pip并得到这个错误:

(env) (base) myName:~/anaconda3/bin $ python3 -m pip install --upgrade pip
Traceback (most recent call last):
  File "/home/myName/anaconda3/lib/python3.6/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/home/myName/anaconda3/lib/python3.6/runpy.py", line 142, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/home/myName/anaconda3/lib/python3.6/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 4, in <module>
    import locale
  File "/home/myName/anaconda3/bin/env/lib/python3.6/locale.py", line 16, in <module>
    import re
  File "/home/myName/anaconda3/bin/env/lib/python3.6/re.py", line 142, in <module>
    class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'`

It is a crazy mix between python3, python2.7 then again python3.这是python3、python2.7和python3之间的疯狂组合。 I'm lost \\o/我迷路了\\o/

edit3:编辑3:

I finally found my error.我终于找到了我的错误。 It was a probleme with my PYTHONPATH, which was pointing somewhere where a .pth file was defined, with some hard link to python 2.7 libraries.这是我的 PYTHONPATH 的一个问题,它指向定义了 .pth 文件的某个地方,并带有一些指向 python 2.7 库的硬链接。 with a simple "unset PYTHONPATH" it works fine.使用简单的“未设置 PYTHONPATH”就可以正常工作。 Thanks you all谢谢大家

I had the same problem, took me several hours to figure it out.我遇到了同样的问题,花了我几个小时才弄明白。

In my case, the PYTHONPATH was set to /usr/lib/python2.6/dist-packages/ changing it to /Users/xxx/miniconda3/lib/python3.7/site-packages/ resolved the issue.就我而言, PYTHONPATH设置为/usr/lib/python2.6/dist-packages/将其更改为/Users/xxx/miniconda3/lib/python3.7/site-packages/解决了该问题。 Good luck.祝你好运。

Based what I can see from your code listing, it seems that the anaconda environment is not activated (this is usually indicated by the environments name in surrounding brackets () before the prompt).根据我从您的代码清单中看到的内容,似乎 anaconda 环境未激活(这通常由提示前括号 () 中的环境名称表示)。 Because of that python3 tries to look for numpy and only finds the one you used for python 2.7 previously, which causes the error (see the python2.7 error messages)因为 python3 试图寻找 numpy 并且只找到你以前用于 python 2.7 的那个,这会导致错误(请参阅python2.7错误消息)

Try尝试

source activate <your-anaconda-environment>

Then your prompt should look like this那么你的提示应该是这样的

(<your-anaconda-environment>) myName:~/anaconda3/bin$ 

Numpy should be contained in the standard anaconda environment. Numpy 应该包含在标准的 anaconda 环境中。 Try then to check if numpy is installed within your environment with然后尝试检查您的环境中是否安装了 numpy

conda list numpy

If there is no numpy listed, try to install it with pip as you wrote before.如果没有列出 numpy,请尝试使用之前写的 pip 安装它。

If want to use python3, use pip3 instead of pip:如果要使用python3,请使用pip3而不是pip:

 pip3 install numpy

You can try these too in terminal:您也可以在终端中尝试这些:

rm -Rf env
virtualenv -p python3 env
source env/bin/activate
python3 -m pip install --upgrade pip
pip3 install numpy

Also make sure that the first line of your script is还要确保脚本的第一行是

#!/usr/bin/python3

Don't write anything, even comments, before this.在此之前不要写任何东西,甚至是评论。

I solved this problem changing the Python's version on VS Code.我通过更改 VS Code 上的 Python 版本解决了这个问题。 I was working with Python 3.7.4, now with the 3.7.8 one.我使用的是 Python 3.7.4,现在使用的是 3.7.8。

When I installed Numpy, it was stored in the folder of Python3.6.我安装Numpy的时候,是存放在Python3.6的文件夹下的。 So I think that u could try to change the path or just change the Python's version所以我认为你可以尝试改变路径或者只是改变 Python 的版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM