在jupyter中没有名为tensorflow的模块

Sko*_*ler 24 python tensorflow jupyter-notebook

我的jupyter笔记本中有一些导入,其中有tensorflow:

ImportError                               Traceback (most recent call last)
<ipython-input-2-482704985f85> in <module>()
      4 import numpy as np
      5 import six.moves.copyreg as copyreg
----> 6 import tensorflow as tf
      7 from six.moves import cPickle as pickle
      8 from six.moves import range

ImportError: No module named tensorflow
Run Code Online (Sandbox Code Playgroud)

我把它放在我的电脑上,在一个特殊的环境和所有连接的东西中:

Requirement already satisfied (use --upgrade to upgrade): tensorflow in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): six>=1.10.0 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): protobuf==3.0.0b2 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.10.1 in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): wheel in /Users/mac/anaconda/envs/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./setuptools-23.0.0-py2.7.egg (from protobuf==3.0.0b2->tensorflow)
Run Code Online (Sandbox Code Playgroud)

我可以在我的电脑上导入tensorflow:

>>> import tensorflow as tf
>>> 
Run Code Online (Sandbox Code Playgroud)

所以我很困惑为什么这是笔记本中的另一种情况?

Ide*_*kiy 48

如果您按官方文档中所述安装了TensorFlow:https://www.tensorflow.org/versions/r0.10/get_started/os_setup.html#overview

我的意思是创建一个名为tensorflow的环境并在python中测试你的安装,但是TensorFlow不能在jupyter中导入,你必须在tensorflow环境中安装jupyter:

conda install jupyter notebook
Run Code Online (Sandbox Code Playgroud)

之后我运行了一个jupyter,它也可以导入TensorFlow:

jupyter notebook
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢这个解决方案,因为它不需要任何手动摆弄。看来,通过在环境中另外安装 jupyter,“全局”jupyter 会被遮蔽,并且所有内容都设置正确。(注意:安装后我必须启动一个新的控制台会话) (2认同)

小智 15

Jupyter在conda环境下运行,因为你的tensorflow安装在conda之外.要在conda虚拟环境下安装tensorflow,请在终端中运行以下命令:

 conda install -c conda-forge tensorflow 
Run Code Online (Sandbox Code Playgroud)


alp*_*nis 14

我遇到了同样的问题,并通过查看输出来解决它:

jupyter kernelspec list

输出内核信息:

python2 /Users/Username/Library/Jupyter/kernels/python2 
python3 /Users/Username/Library/Jupyter/kernels/python3
Run Code Online (Sandbox Code Playgroud)

请注意,该路径指向用户的Jupyter内核.要在Anaconda环境中使用它,它需要指向你正在使用的conda env,看起来像Anaconda3\envs\Env_Name\share\jupyter\kernels\python3.

因此,要删除Jupyter kernelspec,只需使用:

jupyter kernelspec remove python3

或者jupyter kernelspec remove python2如果你使用的是python 2

现在,输出jupyter kernelspec list应该指向正确的内核.

有关此内容的详细信息,请参阅https://github.com/jupyter/notebook/issues/397.


Dee*_*mal 6

Conda 环境从主系统站点包中获取 tensorflow 包。

第 1 步:只需停用 conda 环境

conda deactivate  

pip install tensorflow 
Run Code Online (Sandbox Code Playgroud)

第 2 步:切换回 conda 环境

conda activate YOUR_ENV_NAME

jupyter notebook
Run Code Online (Sandbox Code Playgroud)

第 3 步:运行单元格,import tensorflow您应该可以导入。

谢谢