2017-04-04 119 views
0

我從book運行以下簡單的腳本,並收到以下錯誤Python的錯誤:沒有模塊名爲mrjob.job

from mrjob.job import MRJob 

class MRWordCount(MRJob): 
def mapper(self, _, line): 
    for word in line.split(): 
    yield(word, 1) 
def reducer(self, word, counts): 
    yield(word, sum(counts)) 

if __name__ == '__main__': 
MRWordCount.run() 

使用Windows 10 cygwin64它返回以下錯誤:

[email protected] /cygdrive/c/Users/User001/PycharmProjects/TestProject 
$ python preparation.py input.txt 
Traceback (most recent call last): 
    File "preparation.py", line 1, in <module> 
    from mrjob.job import MRJob 
ImportError: No module named mrjob.job 

這裏是我做過什麼:

  1. 我使用安裝mrjob併成功安裝。

  2. 我檢查過site-packages/mrjob中存在的文件,並且job.py文件也存在,我可以打開該文件並查看該文件中的方法。

  3. 我使用的是Pycharm,所以當我嘗試導入mrjob時,它也給了我pycharm識別文件的語法完成。

現在我不明白爲什麼它無法得到這個模塊。任何人都可以幫忙嗎?謝謝

+2

檢查你是否安裝了多個python版本 –

+0

檢查你是否有一個帶有__init __。py'文件的本地'mrjob.py'文件或'mrjob'目錄,掩蓋了這個包。 –

+0

我最近安裝了windows,所以沒有其他的python版本。我在包 – muazfaiz

回答

0

The Module Search Path

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path . sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

這意味着你應該避免使用相同的名稱作爲標準庫或命名你的模塊內置模塊名稱。所以你最好重命名你的包名或腳本文件名,而不是mrjob.py

相關問題