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

标签 python python-3.x numpy visual-studio-code macos-big-sur

(我在 StackOverflow 上发现的类似问题针对不同的操作系统进行了回答)

我在我的新 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

最佳答案

我找到了解决方法。

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

所以我首先完全删除了numpy

pip3 uninstall numpy

添加路径

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

然后重新安装了numpy

pip3 install numpy

我不确定这是否是要走的路,但它现在解决了这个 ImportError。

关于python - 无法从 VSCode 外部在我的 macbook pro 中使用 Python3 numpy(或 matplotlib 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65679502/

相关文章:

python - 使用python从邮件下载附件

Python Dataframe - 获取特定数字与列值之间的最大值

python - 创建一个 DataFrame,其值作为连接的索引和列名

python - Numpy 网格数据插值到一定半径

python - Pandas:将索引值列表应用于数据框

python - 使用查询参数过滤基于类的 View Django 中的对象?

python - 具有反向引用的 re.sub

python - 如何将列表中元组中的整数与字典中另一个列表中另一个元组中的另一个整数进行比较?

python - Linux开发/最小smtp和pop3服务器

python - 用python从地址中分割城市和县