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

时间:2019-04-26 12:04:35

标签: python numpy msys2

我在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中显式添加“站点包”,以便numpy被Python找到-并且即使这样,还是出了问题:

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和MSYS2 numpy shell中使用bash

1 个答案:

答案 0 :(得分:0)

事实证明,在MINGW64中实际上使用了两个不同的python3

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

... vs 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)