未定义全局名称"re" [英] global name 're' is not defined

查看:121
本文介绍了未定义全局名称"re"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,在地图上工作可以减少百果馅的问题.运行百果馅脚本时出现以下错误.

I am new to python and working on a map reduce problem with mincemeat. I am getting the following error while running the mincemeat script.

$python mincemeat.py -p changeme localhost
error: uncaptured python exception, closing channel <__main__.Client connected at 0x923fdcc> 
(<type 'exceptions.NameError'>:global name 're' is not defined
 [/usr/lib/python2.7/asyncore.py|read|79]
 [/usr/lib/python2.7/asyncore.py|handle_read_event|438] 
 [/usr/lib/python2.7/asynchat.py|handle_read|140]
 [mincemeat.py|found_terminator|96]
 [mincemeat.py|process_command|194]
 [mincemeat.py|call_mapfn|170]
 [raw1.py|mapfn|43])

我的代码位于raw1.py脚本中,该脚本在上面的堆栈跟踪中以[raw1.py|mapfn|43]给出.

My code rests in raw1.py script which is given in the above stacktrace as [raw1.py|mapfn|43].

import re
import mincemeat

# ...

allStopWords = {'about':1, 'above':1, 'after':1, 'again':1}

def mapfn(fname, fcont):
    # ...
    for item in tList[1].split():
        word = re.sub(r'[^\w]', ' ', item).lower().strip()        # ERROR
        if (word not in allStopWords) and (len(word) > 1):
            # ....

我已经在raw1.py中导入了re.如果我在mincemeat.py中导入re,则不会出现该错误.

I have already imported re in raw1.py. The error doesn't appear if I import re in mincemeat.py.

推荐答案

您需要在mapfn本身中包含import语句. mapfn在另一个python进程中执行,因此它无权访问声明它的原始上下文(包括导入).

You need to have the import statement in mapfn itself. mapfn gets executed in a different python process, so it doesn't have access to the original context (including imports) in which it was declared.

这篇关于未定义全局名称"re"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆