jupyter notebook中No module named 'tensorflow'

旧时模样 提交于 2019-12-23 01:02:57

当我们在jupyter notebook中运行时可能会遇见没有某个包的情况,如下:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-24005895b065> in <module>
      2 import h5py
      3 import matplotlib.pyplot as plt
----> 4 import tensorflow as tf
      5 from tensorflow.python.framework import ops
      6 import tf_utils

ModuleNotFoundError: No module named 'tensorflow'

首先我的jupyter notebook是在本地设置的python3的环境deeplearning下运行的:

userdeMacBook-Pro:~ user$ conda activate deeplearning
(deeplearning) userdeMacBook-Pro:~ user$ jupyter notebook

但是很奇怪的是我python3的环境下已经安装好了tensorflow,但是jupyter中还是显示没有

(deeplearning) userdeMBP:~ user$ python
Python 3.7.2 (default, Dec 29 2018, 00:00:04) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> tensorflow.__version__
'1.13.1'
>>> 

可见安装了1.13.1版本的tensorflow

 

解决方法有:

打开你的anaconda navigator,按下图选择,之后apply安装就行:

 报了个错:

UnsatisfiableError: The following specifications were found to be in conflic
pytorch
tensorflow == 1.11.0
use conda info <package> to check dependencies

使用conda info tensorflow查看依赖后,发现tensorflow <= 1.12.0版本不支持python3.7,而anaconda navigator中提供的tensorflow的最高版本是1.12.0,所以我就将其降到了3.6版本,即在指定的环境deeplearning下运行:

(deeplearning) userdeMBP:~ user$ conda install python=3.6

 当然这样可能会导致一些之前安装的包的丢失,因为再import tensorflow时可见之前安装的tensorflow没了:

(deeplearning) userdeMBP:~ user$ python
Python 3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46) 
[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
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tensorflow'

 

然后再安装就可以看见安装成功:

然后再查看:

(deeplearning) userdeMBP:~ user$ python
Python 3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46) 
[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
>>> tensorflow.__version__
'1.12.0'
>>> 

可见相应的环境deeplearning中成功安装了tensorflow == 1.12.0版本

 

⚠️

如果在过程中还是有UnsatisfiableError的错,除了依赖的问题,还可以试试:

userdeMBP:~ user$ conda update conda

更新conda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!