2017-04-22 53 views
0

我剛剛安裝NLTK通過PIP使用命令:無法導入nltk。說「導入錯誤:沒有模塊名爲搭配」

sudo pip install -U nltk 

我還安裝numpy的立即類似的方式,我試圖導入NLTK測試和類型鍵入終端「巨蟒」後「進口NLTK」,然後我得到這個:

>>> import nltk 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "nltk.py", line 3, in <module> 
    from nltk.collocations import * 
ImportError: No module named collocations 
>>> 

我試圖在網上找到的解決方案,並發現此鏈接Importing Libraries Issue - "ImportError: No Module named ____", 所以我想這個命令:export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages,但什麼都沒有改變,也許是因爲那裏沒有找到模塊本身。我也嘗試使用命令

sudo pip install -U collocations 

但它說

Collecting collocations 
    Could not find a version that satisfies the requirement collocations (from versions:) 
No matching distribution found for collocations 

當我第一次嘗試也說 您正在使用PIP版本8.1.1,但是9.0.1版本可用。 您應該考慮通過'pip install --upgrade pip'命令進行升級。

我不能直接升級,但是當我用可以升級: 須藤-H PIP安裝--upgrade PIP

而我依然得到了同樣的事情。我對python非常陌生,想知道我是否做錯了什麼。我最近不得不重新安裝Ubuntu 16.04,所以我認爲操作系統工作正常。非常感謝

編輯:所以在我的home文件夾中有一個名爲nltk.py的文件,可能是影響到了這一點,我已經刪除了該文件,但是當我嘗試導入nltk並打印時它仍然以某種方式創建同樣的事情上面回溯.... 文件的內容是:

import sys 
import nltk 
from nltk.collocations import * 
bigram_measures = nltk.collocations.BigramAssocMeasures() 
trigram_measures = nltk.collocations.TrigramAssocMeasures() 

# change this to read in your data 
finder = BigramCollocationFinder.from_words(
    nltk.corpus.genesis.words('annotation/dataset.txt')) 

# only bigrams that appear 3+ times 
finder.apply_freq_filter(3) 

# return the 10 n-grams with the highest PMI 
finder.nbest(bigram_measures.pmi, 10) 
+0

重命名你的文件,不要使用'nltk.py'作爲你的文件名。 – alvas

+0

謝謝@alvas我已經刪除了該文件,但不知何故它又被重新創建了。我不知道如何讓它停止 – user41965

+0

此外,重命名您的文件。沒有任何'collocations.py' – alvas

回答

0

請使用virtualenv,因爲如果你更新或安裝Python包的全球這是不好的,因爲如Ubuntu爲他們自己的軟件使用一些特殊的軟件包版本,如果你改變了一些東西,這可能會產生很多問題。你可以安裝它:

pip install virtualenv 

這裏是一個link具有非常好的介紹如何使用它。

這也可能解決你的問題。我自己也用Ubuntu 16.04測試了它。

+0

非常感謝,我會嘗試它 – user41965

相關問題