无法在我的 macbook pro 中从外部使用 Python3 numpy(和 matplotlib 等)VSCode

Cannot use Python3 numpy (or matplotlib etc.) in my mcbook pro from outside VSCode

(我在 Whosebug 上发现的类似问题的答案不同 OS)

我在我的新 macbook (os Big Sur) 中使用 numpy 时遇到了一些问题。

我已经使用 pip3 安装了 numpy 等,但它似乎只能在 VSCode.

中运行
pip3 install numpy 

在 VSCode 里面一切都 运行 很顺利,甚至是 ipynbs。

但每当我尝试使用终端或 Jupyter notebook 运行 numpy

import numpy as np

我收到以下错误:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~/Library/Python/3.8/lib/python/site-packages/numpy/core/__init__.py in <module>
     21 try:
---> 22     from . import multiarray
     23 except ImportError as exc:

~/Library/Python/3.8/lib/python/site-packages/numpy/core/multiarray.py in <module>
     11 
---> 12 from . import overrides
     13 from . import _multiarray_umath

~/Library/Python/3.8/lib/python/site-packages/numpy/core/overrides.py in <module>
      6 
----> 7 from numpy.core._multiarray_umath import (
      8     add_docstring, implement_array_function, _get_implementing_args)

ImportError: dlopen(/Users/ahsantarique/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 2): no suitable image found.  Did find:
    /Users/ahsantarique/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so: mach-o, but wrong architecture
    /Users/ahsantarique/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so: mach-o, but wrong architecture

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-2-0aa0b027fcb6> in <module>
----> 1 import numpy as np

~/Library/Python/3.8/lib/python/site-packages/numpy/__init__.py in <module>
    138     from . import _distributor_init
    139 
--> 140     from . import core
    141     from .core import *
    142     from . import compat

~/Library/Python/3.8/lib/python/site-packages/numpy/core/__init__.py in <module>
     46 """ % (sys.version_info[0], sys.version_info[1], sys.executable,
     47         __version__, exc)
---> 48     raise ImportError(msg)
     49 finally:
     50     for envkey in env_added:

ImportError: 

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

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/Library/Developer/CommandLineTools/usr/bin/python3"
  * The NumPy version is: "1.19.4"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: dlopen(/Users/**/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so, 2): no suitable image found.  Did find:
    /Users/**/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so: mach-o, but wrong architecture
    /Users/**/Library/Python/3.8/lib/python/site-packages/numpy/core/_multiarray_umath.cpython-38-darwin.so: mach-o, but wrong architecture

VSCode 让您选择您正在使用的解释器,这很可能是您的问题,因为 VSCode 使用的是与您的系统不同的解释器,具有不同的包。

第一步是:

首先你需要知道你的系统使用的是哪个版本的python,要知道你的系统使用的是哪个版本的python在终端python3 --version中输入

第二步是:

然后你需要让VSCode使用与你的系统相同版本的python,为此输入ctrl+shift+p打开VSCode中的命令选项板然后在命令选项板中键入或选择 “Python: Select Interpreter,然后选择 select 您的系统正在使用的相同版本。

最后一步是:

由于 python 的系统安装没有安装 numpy,您需要通过键入 pip3 install numpy

来安装它

我找到了解决方法。

似乎问题是我在路径变量中没有 python bin

所以我首先完全删除了 numpy

pip3 uninstall numpy

添加路径

PATH=$PATH:the/python/bin/directory/that/was/showing

然后重新安装了numpy

pip3 install numpy

我不确定这是否可行,但它暂时解决了这个 ImportError。