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])
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):
# ....
I have already imported re
in raw1.py. The error doesn't appear if I import re
in mincemeat.py.
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.
"Global" variables in python are actually scoped to the module/file they're bound in; you do need to import them in every file that uses them.
A module name is just a variable like anything else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With