Forum Archive

Scripts not updating on import?

mwerevu

I discovered this in a more complex set of code, but I've boiled the issue down to two very simple scripts:

mainprog.py

from helper import  helpfunc
print helpfunc(6)

and

helper.py

def helpfunc(val):
    return 2*val

All goes fine the first time I run mainprog.py and the output is 12, but if I change the "2" to a "3" in the helper.py script, then when I run the mainprog.py script, the output is still 12.

The only way I can get mainprog.py to pick up the changes in helper.py is to force quit pythonista.

Am I missing something?

Stefano

Try reload( helpfunc ) at the end of mainprog.py, it should do the math.

mwerevu

Got a TypeError when I tried that. reload wanted a module.

omz

Try adding import helper; reload(helper) before the from-import.

Stefano

Right, reload works with module objects — successfully imported before — only …

mwerevu

Yup. That worked. Thanks!

I'm not a python expert, but is this something unique to pythonista? I've not encountered this before in other IDEs.

omz

It's kind of unique to Pythonista – IDEs on other platforms usually run scripts in a separate process, this is technically impossible to do on iOS (at least in an App Store app), so Pythonista runs a single interpreter and imports are cached (which is just how Python works, it's the same on any platform when you use the command-line Python).