jupyter模式下寫代碼時,通過pip install package命令行安裝package完成之後,無法在jupyter模式下import ,這是個通用的問題,我這裏遇到的是import jieba,可能import 別的package也會出現,記錄下,也花了點時間排查。。。
環境
- windows10
- anaconda3
- jieba
- python3
問題
python3版本,已經安裝好了jieba模塊,並且已經本地寫個.py的測試jieba分詞的python文件可以運行
但是就是在jupyter notebook中無法import。類似其他module也會有類似情況
如下
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-6bd034f81497> in <module>
5 import pandas as pd
6 import numpy as np
----> 7 import jieba
8 import codecs
9 import re
ModuleNotFoundError: No module named 'jieba'
解決
-
乍一看就是jieba模塊不存在,但是實際上python環境已經安裝過了,如下檢查
C:\Users\明柯>pip install jieba Requirement already satisfied: jieba in e:\study\python3\lib\site-packages (0.39)
說明跟有無安裝jieba這個東西無關,而是jupyter notebook認爲沒安裝,沒找到這個。
-
檢查當前系統下的module腳本安裝路徑(也可以理解爲編譯路徑)
C:\Users\明柯>python Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path ['', 'E:\\study\\Python3\\python37.zip', 'E:\\study\\Python3\\DLLs', 'E:\\study\\Python3\\lib', 'E:\\study\\Python3', 'E:\\study\\Python3\\lib\\site-packages'] >>>
-
檢查jupyter notebook運行環境下的腳本安裝路徑(編譯路徑),我的notebook是在anoconda3裏的,如下
如下是在jupyter notebook瀏覽器裏打開的ipython3文件裏操作
In [1]: import sys In [2]: sys.path Out[2]: ['D:\\GitTest\\badou\\QAboard\\work\\class1\\45', 'E:\\study\\anacoda3\\python37.zip', 'E:\\study\\anacoda3\\DLLs', 'E:\\study\\anacoda3\\lib', 'E:\\study\\anacoda3', '', 'E:\\study\\anacoda3\\lib\\site-packages', 'E:\\study\\anacoda3\\lib\\site-packages\\win32', 'E:\\study\\anacoda3\\lib\\site-packages\\win32\\lib', 'E:\\study\\anacoda3\\lib\\site-packages\\Pythonwin', 'E:\\study\\anacoda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\明柯\\.ipython']
-
如上可知,系統的路徑與anaconda3的jupyter notebook理解的路徑不一樣,所以有這個問題。因爲anaconda3自己會安裝需要的module,且不會和系統混合公用,所以他名下的模塊安裝路徑(編譯路徑)肯定和系統的python模塊路徑不一樣。
-
解決:進入到E:\study\anacoda3這個路徑下有個Scripts目錄,在這裏面文件路徑欄裏直接輸入cmd(或者cmd 然後cd到E:\study\anacoda3\Scripts路徑下),進行安裝jieba
注意,若需要指定版本則pip install jieba=0.39這樣
E:\study\anacoda3\Scripts>pip install jieba Collecting jieba Downloading jieba-0.42.1.tar.gz (19.2 MB) |████████████████████████████████| 19.2 MB 939 kB/s Building wheels for collected packages: jieba Building wheel for jieba (setup.py) ... done Created wheel for jieba: filename=jieba-0.42.1-py3-none-any.whl size=19314481 sha256=ac593ad59de55018cc6750101fe6cab7b9e1a3695fbb26044dd3c3ae0a10a0ca Stored in directory: c:\users\明柯\appdata\local\pip\cache\wheels\24\aa\17\5bc7c72e9a37990a9620cc3aad0acad1564dcff6dbc2359de3 Successfully built jieba Installing collected packages: jieba Successfully installed jieba-0.42.1 E:\study\anacoda3\Scripts>
-
驗證
安裝完畢後,再次去jupyter notebook裏試下import jieba
In [3]: import jieba In [4]: # 全模式 seg_list = jieba.cut("南京市長江大橋",cut_all=True) print(",".join(seg_list)) Building prefix dict from the default dictionary ... Dumping model to file cache C:\Users\明柯\AppData\Local\Temp\jieba.cache Loading model cost 1.001 seconds. Prefix dict has been built successfully. 南京,南京市,京市,市長,長江,長江大橋,大橋
如有更簡便的解決方案歡迎討論