我无法在我的 python 应用程序中导入 TensorFlow,但仅限
- 如果我在 VSCode 中运行我的应用程序(通过调试器)或者
- 如果我从 VSCode 内的终端运行它。如果我从 VSCode 之外的终端运行应用程序,一切正常。
我在 macOS Big Sur 版本 11.1(M1 芯片组)上运行 VSCode。我在虚拟环境中安装了 python 3.8.2 和 TensorFlow。
这是重现错误的步骤。从我运行的 VSCode 之外的终端
source env/bin/activate
激活虚拟环境python
启动 python。输出到终端(如预期):Python 3.8.2(默认,2020 年 11 月 4 日,21:23:28)[...]import tensorflow as tf
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 中运行我的应用程序,而如果我从终端运行它,它正在工作,也会发生同样的问题。
任何意见是极大的赞赏。