简体   繁体   English

无法在 Python 3.7 或 Python 3.8 中导入 numpy - Ubuntu 18.04

[英]Cannot import numpy in Python 3.7 or Python 3.8 - Ubuntu 18.04

I manage a group of users so I'm trying to figure out how I can install numpy for all of them to use without having everyone install the package themselves.我管理一组用户,所以我试图弄清楚如何安装numpy供他们所有人使用,而无需每个人都自己安装软件包。

I have Python 2.7.17 and Python 3.6.9 (installed by default through Ubuntu 18.04).我有 Python 2.7.17 和 Python 3.6.9(默认通过 Ubuntu 18.04 安装)。 Additionally, I have installed Python 3.7.5, Python 3.8.0, and numpy using the following.此外,我已经使用以下内容安装了 Python 3.7.5、Python 3.8.0 和numpy

sudo apt install python3.7
sudo apt install python3.8
sudo apt install python3-numpy

If I run python or python3 or python3.6 which (invokes python 2.7.17 or python 3.6.9), I can run the following command just fine.如果我运行pythonpython3python3.6 (调用 python 2.7.17 或 python 3.6.9),我可以运行以下命令就好了。

import numpy

However, if I try to run the above after running python3.7 or python3.8 I get the following error message:但是,如果我在运行python3.7python3.8后尝试运行上述python3.7python3.8收到以下错误消息:

ImportError: cannot import name 'multiarray' from 'numpy.core' (/usr/lib/python3/dist-packages/numpy/core/__init__.py)

EDIT: Full error编辑:完全错误

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 'numpy.core' (/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 'numpy.core' (/usr/lib/python3/dist-packages/numpy/core/__init__.py)

When I install numpy it gets installed to /usr/lib/python3/dist-packages which should be fine since it's on the sys.path() for all versions of Python.当我安装numpy它被安装到/usr/lib/python3/dist-packages这应该没问题,因为它在所有版本的 Python 的sys.path()上。

Could it not be working because the version of numpy being pulled using apt is only for Python 3.6.9?因为使用 apt 拉取的numpy版本仅适用于 Python 3.6.9,所以它无法工作吗? If that's the case, how should I install numpy for all users for ALL versions of Python?如果是这种情况,我应该如何为所有版本的 Python 为所有用户安装numpy

EDIT 2: Contents of /usr/lib/python3/dist-packages/numpy/core编辑 2: /usr/lib/python3/dist-packages/numpy/core

total 3672
-rw-r--r-- 1 root root   29215 Sep 29  2017 arrayprint.py
-rw-r--r-- 1 root root     413 Sep 17  2017 cversions.py
-rw-r--r-- 1 root root   67393 Sep 17  2017 defchararray.py
-rw-r--r-- 1 root root    6208 Dec  5  2017 _dummy.cpython-36m-x86_64-linux-gnu.so
-rw-r--r-- 1 root root   35533 Sep 29  2017 einsumfunc.py
-rw-r--r-- 1 root root   98980 Sep 29  2017 fromnumeric.py
-rw-r--r-- 1 root root   12104 Sep 29  2017 function_base.py
-rw-r--r-- 1 root root    7331 Sep 29  2017 generate_numpy_api.py
-rw-r--r-- 1 root root   18422 Sep 17  2017 getlimits.py
drwxr-xr-x 3 root root    4096 Mar 24 13:20 include
-rw-r--r-- 1 root root    4692 Sep 17  2017 info.py
-rw-r--r-- 1 root root    3039 Sep 29  2017 __init__.py
-rw-r--r-- 1 root root   21375 Sep 29  2017 _internal.py
drwxr-xr-x 3 root root    4096 Mar 24 13:20 lib
-rw-r--r-- 1 root root   10789 Sep 17  2017 machar.py
-rw-r--r-- 1 root root   11432 Sep 17  2017 memmap.py
-rw-r--r-- 1 root root    4704 Sep 17  2017 _methods.py
-rw-r--r-- 1 root root 1582528 Dec  5  2017 multiarray.cpython-36m-x86_64-linux-gnu.so
-rw-r--r-- 1 root root   48624 Dec  5  2017 multiarray_tests.cpython-36m-x86_64-linux-gnu.so
-rw-r--r-- 1 root root   91642 Sep 29  2017 numeric.py
-rw-r--r-- 1 root root   28786 Sep 29  2017 numerictypes.py
-rw-r--r-- 1 root root   10384 Dec  5  2017 operand_flag_tests.cpython-36m-x86_64-linux-gnu.so
drwxr-xr-x 2 root root    4096 Mar 24 13:20 __pycache__
-rw-r--r-- 1 root root   29418 Sep 29  2017 records.py
-rw-r--r-- 1 root root   15345 Dec  5  2017 setup_common.py
-rw-r--r-- 1 root root   40704 Sep 29  2017 setup.py
-rw-r--r-- 1 root root   19081 Sep 29  2017 shape_base.py
-rw-r--r-- 1 root root   10408 Dec  5  2017 struct_ufunc_test.cpython-36m-x86_64-linux-gnu.so
-rw-r--r-- 1 root root   44312 Dec  5  2017 test_rational.cpython-36m-x86_64-linux-gnu.so
drwxr-xr-x 4 root root    4096 Mar 24 13:20 tests
-rw-r--r-- 1 root root 1415520 Dec  5  2017 umath.cpython-36m-x86_64-linux-gnu.so
-rw-r--r-- 1 root root   18856 Dec  5  2017 umath_tests.cpython-36m-x86_64-linux-gnu.so

Results of locate multiarray.py locate multiarray.py结果

/usr/lib/python2.7/dist-packages/numpy/core/tests/test_multiarray.py
/usr/lib/python2.7/dist-packages/numpy/core/tests/test_multiarray.pyc
/usr/lib/python2.7/dist-packages/numpy/matrixlib/tests/test_multiarray.py
/usr/lib/python2.7/dist-packages/numpy/matrixlib/tests/test_multiarray.pyc
/usr/lib/python3/dist-packages/numpy/core/tests/test_multiarray.py
/usr/lib/python3/dist-packages/numpy/matrixlib/tests/test_multiarray.py

The problem is that the deb numpy-python3 only works with python3.6 .问题是 deb numpy-python3仅适用于python3.6 The (appallingly uninformative) error message is cause by a trial import in the __init__.py (令人震惊的无信息)错误消息是由__init__.py的试验导入引起的

try:
    from . import multiarray
except ImportError as exc:
    msg = """
Importing the multiarray numpy extension module failed.  Most
 :

That import fails because you are running python3.7 , which attempts to load the dynamic library multiarray.cpython-37m-x86_64-linux-gnu.so , but numpy-python3 only provides multiarray.cpython-36m-x86_64-linux-gnu.so : this is the minor-version incompatibility that you observe.该导入失败,因为您正在运行python3.7 ,它尝试加载动态库multiarray.cpython-37m-x86_64-linux-gnu.so ,但numpy-python3仅提供multiarray.cpython-36m-x86_64-linux-gnu.so :这是您观察到的次要版本不兼容性。

If you look on the package description for numpy-python3 , this is actually explicit如果您查看numpy-python3的包描述,这实际上是明确的

$ apt-cache show python3-numpy | grep Dep
Depends: python3 (<< 3.7), python3 (>= 3.6~), ...

and python3 provides a compatible version并且python3提供了兼容的版本

$ apt-cache show python3 | grep Vers
Version: 3.6.7-1~18.04

So I'm afraid you're out of luck using the system Numpy with 3.7 or 3.8, you'll need to pip-install it (that should install it into the site-packages in /usr/local , not tested), but don't forget to do this as python3.7 -m pip ... since the system pip3 uses python3 which is python3.6 unless you've futzed around with update-alternatives .所以我担心你在使用 3.7 或 3.8 的系统 Numpy 时不走运,你需要 pip 安装它(应该将它安装到/usr/local的站点包中,未测试),但是不要忘记将其作为python3.7 -m pip ...因为系统pip3使用python3 ,即python3.6除非您对update-alternatives感到困惑。

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

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