2017-04-04 123 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

相关问题