Forum Archive

(bug) When running a script that makes use of another script...

eric

When running a script a.py that import b.py, and I modify b.py once a.py as already been launched, I have to quit Pythonista for the a.py to reload and take into account modifications made in b.py ...
Is that a bug or some sort of optimisation that avoid recompiling the byte code that I can disable in the settings?

Regards,
Eric T.

dgelessus

It's an optimization in core Python. When a module is imported for the first time, it is stored in the dictionary sys.modules. If a module is already listed there, the existing instance is reused and the file is not reloaded from disk. This is especially noticeable in Pythonista because the entire app internally uses a single Python interpreter instance for the interactive console, user scripts, editor actions, etc. On a normal computer you'd run your program in its own Python process, e. g. by typing python helloworld.py in the shell. That process exits when the program is finished, and sys.modules is discarded. In Pythonista this only happens when the app is removed from the background, either because iOS needs RAM or because you manually quit it.

eric

Ok, so this is kind of a known issue that you have to quit Pythonista for sub modules to be properly reloaded by scripts using it, once edited?

omz

You can use the built-in reload() function to reload a module without having to quit Pythonista. It's likely that this won't be necessary anymore in the next update though.

eric

thx that's a good workaround while working on a sub module