jupyter notebook中No module named 'tensorflow'

当我们在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,当要注意,你这么做会导致你之前安装的应用都失效,要重新安装,如下图所示,状态都从launch变成了install:

在deeplearning中再运行jupyter notebook就出现了错误:

(deeplearning) userdeMBP:~ user$  jupyter notebook
-bash: jupyter: command not found

重新安装即可

猜你喜欢

转载自www.cnblogs.com/wanghui-garcia/p/10606126.html
今日推荐