https://bugzilla.wikimedia.org/show_bug.cgi?id=72942
--- Comment #4 from Merlijn van Deen valhallasw@arctus.nl --- Seems to be a scoping issue -- replace.py (and thus most of the things inside it, including the pywikibot import) is unloaded when replace.py ends... which is before the function is called. Two options to solve this:
1) Explicitly call pywikibot.stopme() at the end of replace.py. This will make sure the callback is triggered before replace.py is unloaded ...and then you can also juts do the output directly, which is *much* cleaner. I.e. instead of
def display_edit_counter(bot): pywikibot.output(u'\n%s pages changed.' % bot.changed_pages)
# Queue last request to display number of changed pages. pywikibot.async_request(display_edit_counter, bot)
we'd have
pywikibot.stopme() pywikibot.output(u'\n%s pages changed.' % bot.changed_pages)
2) explicitly pass pywikibot so it's included in the scope, i.e.
def display_edit_counter(pywikibot, bot): pywikibot.output(u'\n%s pages changed.' % bot.changed_pages)
# Queue last request to display number of changed pages. pywikibot.async_request(display_edit_counter, pywikibot, bot)