无法在Mrjob中导入模块
我尝试用mrjob来修改单词计数的例子。我的项目结构是:
├── input_text.txt
├── store_xml_dir
│ ├── xml_file.xml
│ └── xml_parse.py
└── wordcount.py
而wordcount.py的内容是:
import os
import sys
cwdir = os.path.dirname(__file__)
sys.path.append(cwdir)
sys.path.append(os.path.join(cwdir, "store_xml_dir"))
import xml_parse
# print dir(xml_parse) <- it works here if i'd comment the rest code
from mrjob.job import MRJob
class MRWordFrequencyCount(MRJob):
def mapper(self, _, line):
getxml = xml_parse.GetXML()
print '>>>', getxml.get_strings()
yield "chars", len(line)
yield "words", len(line.split())
yield "lines", 1
def reducer(self, key, values):
yield key, sum(values)
if __name__ == '__main__':
MRWordFrequencyCount.run()
当我运行的时候,出现了错误:ImportError: No module named xml_parse
。为什么在这种情况下,Python无法导入xml_parse
呢?
2 个回答
0
我发现把模块导入放在成员函数里面,可以解决我的问题。
def mapper(self, _, line):
import xml_parse
...
0
mrjob 不会 自动导出你的 非mapreduce 代码,你需要自己导出。 但是你需要在配置中明确告诉它这样做。(可以查看他们的 文档)
你可以通过添加这些配置参数来运行你的代码:
--python-archive=store_xml_dir