更新
TensorFlow website 支持五种安装。
据我了解,直接使用Pip installation 可以在 Jupyter Notebook 中导入 TensorFlow(只要安装了 Jupyter Notebook 并且没有其他问题)b/z 它没有创建任何虚拟环境。
使用 virtualenv install 和 conda install 需要将 jupyter 安装到新创建的 TensorFlow 环境中,以允许 TensorFlow 在 Jupyter Notebook 中工作(有关更多详细信息,请参阅以下原始帖子部分)。
我相信docker install 可能需要在 VirtualBox 中设置一些端口才能使 TensorFlow 在 Jupyter Notebook 中工作 (see this post)。
对于installing from sources,还取决于源代码构建和安装到哪个环境。如果它安装在新创建的虚拟环境中或未安装 Jupyter Notebook 的虚拟环境中,则还需要将 Jupyter Notebook 安装到虚拟环境中才能在 Jupyter Notebook 中使用 Tensorflow。
原帖
要在 Ipython 和/或 Jupyter(Ipython) Notebook 中使用 tensorflow,您需要在 tensorflow 激活环境下安装 Ipython 和 Jupyter(安装 tensorflow 后)。
在tensorflow环境下安装Ipython和Jupyter之前,如果你在终端执行以下命令:
username$ source activate tensorflow
(tensorflow)username$ which ipython
(tensorflow)username$ /Users/username/anaconda/bin/ipython
(tensorflow)username$ which jupyter
(tensorflow)username$ /Users/username/anaconda/bin/jupyter
(tensorflow)username$ which python
(tensorflow)username$ /User/username//anaconda/envs/tensorflow/bin/python
这告诉您,当您从终端打开 python 时,它使用的是安装在安装 tensorflow 的“环境”中的那个。因此,您实际上可以成功导入 tensorflow。但是,如果您尝试运行 ipython 和/或 jupyter 笔记本,它们不会安装在配备 tensorflow 的“环境”下,因此它必须返回使用没有 tensorflow 模块的常规环境,因此您会得到一个导入错误。
您可以通过列出 envs/tensorflow/bin 目录下的项目来验证这一点:
(tensorflow) username$ ls /User/username/anaconda/envs/tensorflow/bin/
您会看到没有列出“ipython”和/或“jupyer”。
要将 tensorflow 与 Ipython 和/或 Jupyter notebook 一起使用,只需将它们安装到 tensorflow 环境中:
(tensorflow) username$ conda install ipython
(tensorflow) username$ pip install jupyter #(use pip3 for python3)
安装它们后,在 envs/tensorflow/bin/ 目录中应该会出现一个“jupyer”和一个“ipython”。
注意事项:
在尝试在 jupyter notebook 中导入 tensorflow 模块之前,请尝试关闭 notebook。并且首先“source deactivate tensorflow”,然后重新激活它(“source activate tensorflow”)以确保事情“在同一页面上”。然后重新打开笔记本并尝试导入 tensorflow。它应该成功导入(至少在我的工作)。