Python VSCode中的Tensorflow导入失败

Python VSCode中的Tensorflow导入失败,python,python-3.x,tensorflow,visual-studio-code,apple-m1,Python,Python 3.x,Tensorflow,Visual Studio Code,Apple M1,我在python应用程序中导入TensorFlow时遇到问题,但仅限于 如果我在VSCode中运行应用程序(通过调试器)或 如果我在VSCode中从终端运行它。 如果我在VSCode之外的终端上运行应用程序,一切都正常 我在macOS Big Sur 11.1版(M1芯片组)上运行VSCode。 我在虚拟环境中安装了python 3.8.2和TensorFlow 下面是重现错误的步骤。从VSCode外部的终端运行 源环境/bin/activate激活虚拟环境 python启动python。

我在python应用程序中导入TensorFlow时遇到问题,但仅限于

  • 如果我在VSCode中运行应用程序(通过调试器)
  • 如果我在VSCode中从终端运行它。 如果我在VSCode之外的终端上运行应用程序,一切都正常
我在macOS Big Sur 11.1版(M1芯片组)上运行VSCode。 我在虚拟环境中安装了python 3.8.2和TensorFlow

下面是重现错误的步骤。从VSCode外部的终端运行

  • 源环境/bin/activate
    激活虚拟环境

  • python
    启动python。输出到终端(如预期的那样):Python 3.8.2(默认值,2020年11月4日,21:23:28)[…]

  • 将tensorflow导入为tf

  • 打印(tf.\uuuuu版本)
    这会将“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.
    
    回溯(最近一次呼叫最后一次):
    文件“/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site packages/tensorflow/python/pywrap_tensorflow.py”,第64行
    从tensorflow.python.\u pywrap\u tensorflow\u内部导入*
    导入错误:dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python//u pywrap\u tensorflow\u internal.so,6):未找到合适的映像。确实发现:
    /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python//u pywrap\u tensorflow\u internal.so:mach-o,但架构错误
    /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python//u pywrap\u tensorflow\u internal.so:mach-o,但架构错误
    在处理上述异常期间,发生了另一个异常:
    回溯(最近一次呼叫最后一次):
    文件“”,第1行,在
    文件“/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site packages/tensorflow/_init__.py”,第41行,in
    从tensorflow.python.tools将模块_util作为_模块_util导入
    文件“/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site packages/tensorflow/python/__init__.py”,第39行
    从tensorflow.python导入pywrap\u tensorflow作为_pywrap\u tensorflow
    文件“/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site packages/tensorflow/python/pywrap_tensorflow.py”,第83行
    提高效率(msg)
    ImportError:回溯(最近一次呼叫上次):
    文件“/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site packages/tensorflow/python/pywrap_tensorflow.py”,第64行
    从tensorflow.python.\u pywrap\u tensorflow\u内部导入*
    导入错误:dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python//u pywrap\u tensorflow\u internal.so,6):未找到合适的映像。确实发现:
    /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python//u pywrap\u tensorflow\u internal.so:mach-o,但架构错误
    /Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python//u pywrap\u tensorflow\u internal.so:mach-o,但架构错误
    未能加载本机TensorFlow运行时。
    看见https://www.tensorflow.org/install/errors
    因为一些常见的原因和解决方案。包括整个堆栈跟踪
    以上是请求帮助时的错误消息。
    
    看起来VSCode内的终端不像VSCode外的终端那样使用相同的站点包,但是运行
    print(sys.path)
    会得到相同的结果

    如果我试图在VSCode中运行我的应用程序,那么同样的问题也会发生,而如果我在终端上运行它,那么它就会工作


    非常感谢您的建议。

    试试Python 3.7。许多人抱怨tensorflow没有使用3.8,而是使用3.7。另外,尝试从google tensorflow网页下载它。

    尝试Python 3.7。许多人抱怨tensorflow没有使用3.8,而是使用3.7。另外,试着从谷歌tensorflow网页下载它。

    在上面的评论中,Piggy放弃了Jay Mody。我首先检查您在VS代码中使用的shell是否与您在非VS代码终端中使用的shell相同

    请尝试以下shell命令:

    echo $SHELL
    
    如果您得到相同的输出,那么我建议确保使用完全相同的python可执行文件。尝试在两个shell中键入以下内容:

    which python
    

    在上面的评论中,小猪对杰伊·莫迪的支持。我首先检查您在VS代码中使用的shell是否与您在非VS代码终端中使用的shell相同

    请尝试以下shell命令:

    echo $SHELL
    
    如果您得到相同的输出,那么我建议确保使用完全相同的python可执行文件。尝试在两个shell中键入以下内容:

    which python
    

    在VS代码中,打开集成终端,激活环境后,运行

    pip show tensorflow
    

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

    在VS代码中,打开集成终端,激活环境后,运行

    pip show tensorflow
    

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

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

    以下是步骤

  • 从这里下载mini conda并安装它
  • 安装后,创建一个新的Conda环境
    Conda create--name python38 python=3.8
    。这将安装python 3.8的ARM版本
  • 激活新环境
    conda激活python38
  • 创建一个新的虚拟环境f