我已经在虚拟环境中安装了tensorflow,并且可以在jupyter笔记本或终端中导入Tensorflow。我现在正在学习使用VScode,所以我尝试在VScode内的jupyter笔记本中启动它,但无法导入Tensorflow。
它显示ModuleNotFoundError:没有名为“tensorflow”的模块,但我已经安装了它,并且可以在终端或jupyternotebook中使用它。
我也尝试过定义python解释器的路径,但是没有成功。有人可以帮我吗?非常感谢。
尝试:
这对我有用。
pip --version
python -m pip install --upgrade pip
pip3 install tensorflow==2.0.0
我正在使用 python 3.11.x 和 Win 11 然后当我这样导入时工作正常:
import tensorflow as tf
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.layers.recurrent import LSTM as LSTM_keras
from tensorflow.python.keras.models import Sequential
再见
Windows 10、python 3.10、tensorflow-gpu 2.10 安装在 ./V310_TF 虚拟环境中。
我特地挑选了VsCode中好的解释器
CTRL+SHIFT+P > Python: clear workspace interpreter settings
(CTRL+SHIFT+P) > Select interpreter ...
。底部的状态栏相应更新指向V310_TF。
但是,VsCode 仍然指向我当天使用的另一个虚拟环境中的另一个解释器。我发现它使用
import sys
sys.executable
>>...\\v311_ANOTHER_ENV\\Scripts\\python.exe'
因此 VsCode 中的 powershell 实例指向了它的预期位置。
我重新启动了 VsCode,问题就解决了。