VSCode 中的 Tensorflow 导入失败

问题描述

我无法在我的 Python 应用程序中导入 TensorFlow,但是只是

  • 如果我在 VSCode 中运行我的应用程序(通过调试器)OR
  • 如果我从 VSCode 中的终端运行它。 如果我从 VSCode 之外的终端运行应用程序,一切正常。

我在 macOS Big Sur 版本 11.1(M1 芯片组)上运行 VSCode。 我在虚拟环境中安装了 python 3.8.2 和 TensorFlow。

这是重现错误的步骤。从 VSCode 之外的终端我运行

  1. source env/bin/activate 激活虚拟环境

  2. python 启动 python。输出到终端(如预期):Python 3.8.2(默认,2020 年 11 月 4 日,21:23:28)[...]

  3. import tensorflow as tf

  4. print(tf.__version__) 这会将“2.4.0-rc0”打印到终端(如预期)。

现在,如果我在内置的 VSCode 终端中重复完全相同的步骤 1 和 2,我会在 2 中得到完全相同的输出。但是,如果我运行命令 3 并尝试导入 tensorflow,则会出现以下错误消息:

Traceback (most recent call last):
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py",line 64,in <module>
    from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so,6): no suitable image found.  Did find:
        /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o,but wrong architecture
        /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o,but wrong architecture

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/__init__.py",line 41,in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/__init__.py",line 39,in <module>
    from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py",line 83,in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py",but wrong architecture


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

似乎 VSCode 内的终端与 VSCode 外的终端使用的站点包不同,但是,运行 print(sys.path) 给出了相同的结果。

如果我尝试在 VSCode 中运行我的应用程序,则会发生同样的问题,而如果我从终端运行它,则它可以正常工作。

非常感谢任何建议。

解决方法

在VS Code中,打开一个集成终端,激活环境后,运行

pip show tensorflow

检查当前环境中是否存在该模块。如果没有,请重新安装。

,

我仍然不知道为什么这个问题首先存在,但现在我通过 mini-conda 的 ARM 版本安装 python 3.8 解决了这个问题。

这是步骤。

  1. 从此处下载 mini-conda https://conda-forge.org/blog/posts/2020-10-29-macos-arm64/ 并安装。
  2. 安装后,创建一个新的 Conda 环境 conda create --name python38 python=3.8。这将安装 ARM 版本的 python 3.8
  3. 激活新环境 conda activate python38
  4. 为您的项目创建一个新的虚拟环境。 python -m venv myEnv
  5. 下载并解压 ARM 版本的 tensorflow https://github.com/apple/tensorflow_macos/releases
  6. 运行包含的脚本来安装 tensorflow。 /Volumes/SSD/Jan/Downloads/tensorflow_macos/install_venv.sh --prompt 并将其指向您在步骤 4 中新创建的虚拟环境。

通过这些步骤,可以正确导入 tensorflow。

一些更多的参考,它指向我这个解决方案: https://github.com/apple/tensorflow_macos/issues/8 https://github.com/apple/tensorflow_macos/issues/3

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...