【问题标题】:Tensorflow import in VSCode failsVSCode 中的 TensorFlow 导入失败
【发布时间】:2021-04-04 17:32:17
【问题描述】:

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

非常感谢任何建议。

【问题讨论】:

  • 再次测试两个终端,但首先运行 zsh 命令打开一个新的 shell 会话。这应该确保您使用相同的 shell 环境和设置运行两个终端,因此结果应该是相同的(理论上)。我很好奇会发生什么。
  • 我按照建议重复测试并运行zsh,然后再执行原始帖子中的步骤。结果没有变化。正如@Ryan 建议的那样,我运行了echo $SHELLwhich python,这两个命令在内部和外部终端中产生相同的输出。 TensorFlow 未通过 pip 安装,因为 M1 兼容版本无法通过 pip 获得。因此,pip show tensorflow 在内部和外部终端 @MollyWang 中都会给出错误消息
  • @pancakeNbacon。然后你可以在集成终端中打开当前激活的环境,检查site-packages文件夹中是否有tensorflow,如果没有,将模块复制到其中。

标签: python python-3.x tensorflow visual-studio-code apple-m1


【解决方案1】:

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

【讨论】:

  • 我必须使用适用于 Apple M1 硅胶的 python / tensorflow 版本。因此,v3.8 目前是我唯一的选择。
【解决方案2】:

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

尝试以下 shell 命令:

echo $SHELL

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

which python

【讨论】:

    【解决方案3】:

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

    pip show tensorflow
    

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

    【讨论】:

      【解决方案4】:

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

      这里是步骤。

      1. 从这里https://conda-forge.org/blog/posts/2020-10-29-macos-arm64/ 下载 mini-conda 并安装它。
      2. 安装后,新建一个Conda环境conda create --name python38 python=3.8。这将安装 ARM 版本的 python 3.8
      3. 激活新环境conda activate python38
      4. 为您的项目创建一个新的虚拟环境。 python -m venv myEnv
      5. 下载解压ARM版tensorflowhttps://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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-17
        • 2020-02-18
        相关资源
        最近更新 更多