Skip to main content
 首页 » 编程设计

python之无法在 Jupyter Notebook 中导入 tensorflow

2025年05月04日23pander-it

我正在使用 Jupyter Notebook 并尝试导入 tensorflow。这是我得到的错误:

--------------------------------------------------------------------------- 
ModuleNotFoundError                       Traceback (most recent call last) 
<ipython-input-15-64156d691fe5> in <module>() 
----> 1 import tensorflow as tf 
 
ModuleNotFoundError: No module named 'tensorflow' 

我通过在我的虚拟环境中键入 jupyter notebook 来托管笔记本:

(labs) Sahands-MBP:part1 sahandzarrinkoub$ jupyter notebook 

虚拟环境肯定安装了tensorflow:

(labs) Sahands-MBP:part1 sahandzarrinkoub$ python 
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)  
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import tensorflow 
>>>  

为什么找不到呢? jupyter 在哪里寻找包?我什至在我的 virtualenv 之外安装了 tensorflow。

请您参考如下方法:

您似乎正在尝试使用来自基线 Python 的 Jupyter 安装,即在您的 labs 虚拟环境之外。

解决此问题的一种快速简便的方法是在您的虚拟环境中另外安装 Jupyter,即

pip install jupyter 

来自实验室。

一个更通用的方法,也是为了避免多个 Jupyter 安装(在每个虚拟环境中),是使用 Jupyter kernels ;看我的 detailed answer here对于 PySpark 的情况,它很容易适应您的情况。