1

我无法在我的 python 应用程序中导入 TensorFlow,仅限

  • 如果我在 VSCode 中运行我的应用程序(通过调试器)或者
  • 如果我从 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", 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


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 中运行我的应用程序,而如果我从终端运行它,它正在工作,也会发生同样的问题。

任何意见是极大的赞赏。

4

4 回答 4

0

试试 Python 3.7。许多人抱怨 tensorflow 不在 3.8 上工作,而是在 3.7 上工作。另外,尝试从 google tensorflow 网页下载它。

于 2020-12-27T21:18:10.223 回答
0

在上面的评论中捎带 Jay Mody。我会首先检查您在 VS 代码中使用的 shell 是否与您在非 VS 代码终端中使用的 shell 相同。

尝试以下 shell 命令:

echo $SHELL

如果你得到相同的输出,那么我建议确保使用完全相同的 python 可执行文件。尝试在两个 shell 中输入:

which python
于 2020-12-27T22:31:58.530 回答
0

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

pip show tensorflow

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

于 2020-12-28T05:40:05.367 回答
0

我仍然不知道为什么这个问题首先存在,但我现在通过 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

于 2020-12-29T20:20:52.290 回答