【問題描述】
在 jupyter notebook 中 import tensorflow 時報錯:提示沒有 tensorflow 模塊傅瞻。
【原因】
我用 virtualen 裝了一個深度學(xué)習(xí)虛擬環(huán)境,名字就叫做 (tensorflow)拭宁。裝 ipython 和 jupyter 時掌唾,沒有裝在 (tensorflow) 環(huán)境下今野,而是裝在了系統(tǒng)全局環(huán)境下照藻。而我的 tensorflow 是裝在 (tensorflow) 虛擬環(huán)境下的免钻。雖然我又在我的 (tensorflow) 虛擬環(huán)境中重新裝了 ipython 和 jupyter 彼水。但是 jupyter 貌似又到全局系統(tǒng)環(huán)境去找 tensorflow , 全局系統(tǒng)環(huán)境中是沒有裝 tensorflow 的,所以報錯:找不到 tensorflow 模塊极舔。
【解決方案】
stackoverflow上關(guān)于這個問題有一個高票解答凤覆,感興趣的讀者請移步這里。
第一步
要在Ipython 或者 Jupyter(Ipython) Notebook 中使用 tensorflow, 你需要在激活了的(tensorflow) (這個環(huán)境中安裝了tensorflow)環(huán)境中安裝Ipython 和 Jupyter 拆魏。在安裝Ipython和Jupyter 之前盯桦,如果在終端執(zhí)行以下命令:
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
以上代碼只是示例,具體根據(jù)自己電腦配置靈活運(yùn)用
以上代碼運(yùn)行結(jié)果表示 當(dāng)你在(tensorflow)虛擬環(huán)境中打開python 時渤刃,這個python是你安裝在這個虛擬環(huán)境中的python 版本拥峦,而不是全局系統(tǒng)環(huán)境中的python版本。 圖1是我解決了這個問題后運(yùn)行的代碼卖子,可以看出jupyter 和 ipython 都在(tensorflow)中略号,這樣的話在jupyter中import tensorflow 就不會報錯了。如果 jupyter 和 ipython 不在這個虛擬環(huán)境中揪胃,就要重新安裝這兩個東西璃哟。
你可以查看 ~/tensorflow/bin 路徑下的文件,看看是否有 “ipython” 和 “jupyter” 相關(guān)文件喊递。沒有的話随闪,就需要在(tensorflow)中安裝這兩個東西。
【注意】 如果在系統(tǒng)全局環(huán)境中安裝了 ipython 和 jupyter骚勘,必須卸載掉全局環(huán)境中的ipython和jupyther铐伴,才能在(tensorflow)虛擬環(huán)境中成功安裝 ipthon 和jupyter 。如果不卸載全局環(huán)境中的ipython和jupyter 俏讹,而直接在(tensorflow)中安裝当宴,即使終端顯示安裝成功,在~/tensorflow/bin 路徑下也找到不到 “ipython” 和 “jupyter” 相關(guān)文件泽疆。執(zhí)行以下語句, 顯示的結(jié)果還是全局路徑户矢,如圖2.
(tensorflow)username$ which ipython
(tensorflow)username$ which jupyter
所以,下一步很關(guān)鍵殉疼,卸載安裝在全局環(huán)境中的 ipython 和 jupyter梯浪。
第二步
卸載 ipython 很簡單 我是用pip安裝的 ipython , 直接用 pip 來卸載(注意此時要退出(tensorflow)環(huán)境):
#卸載python2.7版本中的ipython
username$ pip uninstall ipython # 如果有權(quán)限問題捌年,前面加 sudo
#卸載python3.5版本中的ipython
username$ pip3 uninstall ipython # 如果有權(quán)限問題,前面加 sudo
卸載 jupyter 已經(jīng)花光了我所有的力氣挂洛,這玩意兒真是太難卸載了礼预。
各種方法都卸載不了。先來感受一下下
嘗試用 pip 卸載
聽說 pip-autoremove 很厲害虏劲,試一下
奈何我的 jupyter 裝在 python3.5下托酸,pip-autoremove 貌似只能用在python2.7中,還是卸不掉柒巫。
又是在 stackoverflow 找到了答案励堡,想看原文的讀者請移步這里
安裝 jupyter 時,會安裝很多依賴吻育,具體如下面:
- notebook
- nbconvert
- nbformat
- ipykernel
- ipywidgets
- qtconsole
- traitlets
- tornado
- anything with jupyter in front of it (jupyter_core, jupyter_client etc.)
- ipython_genutils
- jinja2
一個一個都卸載掉念秧,其中,anything with jupyter in front of it (jupyter_core, jupyter_client etc.) 這一項(xiàng)可以用下面的命令找到
field@field-ThinkCentre:~$ pip3 freeze | grep jupyter
jupyter-client==5.2.1
jupyter-console==5.2.0
jupyter-core==4.4.0
再輸入以下指令
field@field-ThinkCentre:~$ sudo pip3 uninstall notebook nbconvert nbformat ipykernel ipywidgets qtconsole traitlets tornado jupyter-client==5.2.1 jupyter-console==5.2.0 jupyter-core==4.4.0 ipython_genutils jinja2 -y
結(jié)果如下圖
驗(yàn)證一下布疼,終于卸載掉了。
【第三步】
進(jìn)入(tensorflow) 環(huán)境中币狠,安裝 ipython 和 jupyter
field@field-ThinkCentre:~$ source tensorflow/bin/activate
(tensorflow) field@field-ThinkCentre:~$ pip3 install ipython
(tensorflow) field@field-ThinkCentre:~$ pip3 install jupyter
安裝完成即可游两。應(yīng)該沒什么問題
完成后,再檢查 ipython 和 jupyter 的路徑
(tensorflow) field@field-ThinkCentre:~$ which jupyter
/home/field/tensorflow/bin/jupyter
(tensorflow) field@field-ThinkCentre:~$ which ipython
/home/field/tensorflow/bin/ipython
結(jié)果如下漩绵,ipython 和 jupyter 的路徑都在(tensorflow)下贱案。
最后,啟動 jupyter notebook, 驗(yàn)證問題是否解決
成功解決問題止吐!