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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM