VSCode中的Tensorflow导入失败

煎饼

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

  • 如果我在VSCode中运行应用程序(通过调试器),或者
  • 如果我是从Terminal WITHIN 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中运行我的应用程序,则会发生相同的问题,而如果我从终端运行它,则它会正常工作。

任何意见是极大的赞赏。

煎饼

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档