在MSYS2 shell中访问Numpy(MINGW64可以正常工作,Windows 10)?

问题描述 投票:0回答:1

我在Windows 10上安装了MSYS2,在其中,我通过pacman为Python3安装了numpy:

$ pacman -Ss numpy | grep installed
mingw64/mingw-w64-x86_64-python3-numpy 1.16.2-1 [installed]

如果我启动MINGW64 bash shell,那么导入numpy就可以了:

user@DESKTOP-XYXYXY MINGW64 /c/temp
$ python3 -c 'import numpy'

user@DESKTOP-XYXYXY MINGW64 /c/temp
$ 

但是,当我从MSYS2 bash shell执行相同操作时,首先我必须向PYTHONPATH明确添加“site-packages”,以便Python找到numpy - 即使这样,也会出现问题:

user@DESKTOP-XYXYXY MSYS /c/temp
$ python3 -c 'import numpy'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

user@DESKTOP-XYXYXY MSYS /c/temp
$ PYTHONPATH='/mingw64/lib/python3.7/site-packages' python -c 'import numpy'
Traceback (most recent call last):
  File "/mingw64/lib/python3.7/site-packages/numpy/core/__init__.py", line 40, in <module>
    from . import multiarray
  File "/mingw64/lib/python3.7/site-packages/numpy/core/multiarray.py", line 12, in <module>
    from . import overrides
  File "/mingw64/lib/python3.7/site-packages/numpy/core/overrides.py", line 6, in <module>
    from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/mingw64/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/mingw64/lib/python3.7/site-packages/numpy/core/__init__.py", line 71, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
  your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
  1. Check that you are using the Python you expect (you're using /usr/bin/python.exe),
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy versions you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

     Note: this error has many possible causes, so please don't comment on
     an existing issue about this - open a new one instead.

Original error was: No module named 'numpy.core._multiarray_umath'

事实证明,这个_multiarray_umath是一个DLL:

$ find / -name '*multiarray_umath*'
/mingw64/lib/python3.7/site-packages/numpy/core/_multiarray_umath-cpython-37m.dll

...所以我想,也许我也应该“破解”$ PATH,但没有骰子:

user@DESKTOP-XYXYXY MSYS /c/temp
$ PATH="$PATH:/mingw64/lib/python3.7/site-packages" PYTHONPATH='/mingw64/lib/python3.7/site-packages' python -c 'import numpy'
Traceback (most recent call last):
  File "/mingw64/lib/python3.7/site-packages/numpy/core/__init__.py", line 40, in <module>
    from . import multiarray
  File "/mingw64/lib/python3.7/site-packages/numpy/core/multiarray.py", line 12, in <module>
    from . import overrides
  File "/mingw64/lib/python3.7/site-packages/numpy/core/overrides.py", line 6, in <module>
    from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/mingw64/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/mingw64/lib/python3.7/site-packages/numpy/core/__init__.py", line 71, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
  your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
  1. Check that you are using the Python you expect (you're using /usr/bin/python.exe),
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy versions you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

     Note: this error has many possible causes, so please don't comment on
     an existing issue about this - open a new one instead.

Original error was: No module named 'numpy.core._multiarray_umath'

是否有可能以某种方式通过环境变量进行设置,以便我可以在Python3中使用numpy,也可以在MSYS2 bash shell中使用?

python numpy msys2
1个回答
0
投票

嗯,事实证明,实际上有两种不同的python3s在MINGW64中使用:

user@DESKTOP-XYXYXY MINGW64 /c/temp
$ ls -la `which python3`
-rwxr-xr-x 1 user None 99328 Apr 23 10:20 /mingw64/bin/python3

...对MSYS2 bash炮弹:

user@DESKTOP-XYXYXY MSYS /c/temp
$ ls -la `which python3`                                                       
-rwxr-xr-x 1 user None 9039 Apr 25 08:50 /usr/bin/python3

所以,诀窍不仅仅是设置一个像PYTHONPATH这样的环境变量 - 而是调用正确的python;有了它,现在我在MSYS2下获得了正确的响应:

user@DESKTOP-XYXYXY MSYS /c/temp
$ /mingw64/bin/python3 -c 'import sys; print(sys.path); import numpy'
['', 'C:/msys64/mingw64/lib/python37.zip', 'C:/msys64/mingw64/lib/python3.7', 'C:/msys64/mingw64/bin', 'C:/msys64/mingw64/lib/python3.7/lib-dynload', 'C:/msys64/mingw64/lib/python3.7/site-packages']

user@DESKTOP-XYXYXY MSYS /c/temp
$ 

您甚至可以使用路径“作弊”,以便在不设置显式绝对路径的情况下获得此行为:

# (this shows still the "old" msys python3)
user@DESKTOP-XYXYXY MSYS /c/temp
$ PATH="/mingw64/bin:$PATH" ls -la `which python3`
-rwxr-xr-x 1 user None 9039 Apr 25 08:50 /usr/bin/python3

# (like this, with subshell, it seems to work)
user@DESKTOP-XYXYXY MSYS /c/temp
$ PATH="/mingw64/bin:$PATH" bash -c 'ls -la `which python3`'
-rwxr-xr-x 1 user None 99328 Apr 23 10:20 /mingw64/bin/python3

# (but can seemingly call python3 without subshell)
user@DESKTOP-XYXYXY MSYS /c/temp
$ PATH="/mingw64/bin:$PATH" python3 -c 'import sys; print(sys.path); import numpy'
['', 'C:/msys64/mingw64/lib/python37.zip', 'C:/msys64/mingw64/lib/python3.7', 'C:/msys64/mingw64/bin', 'C:/msys64/mingw64/lib/python3.7/lib-dynload', 'C:/msys64/mingw64/lib/python3.7/site-packages']

# (program exits without a problem)
© www.soinside.com 2019 - 2024. All rights reserved.