Sub modules not updated automatically
In my app distributed over several module files I have come cross a strange behavior. It seems to me as though sub modules which are used by main modules are not updated automatically. Instead the old public interface remains visible to the main modules until the Pythonista app is closed and reopened. Is there a way to avoid this since this is a little annoying? I often start looking for declaration errors which are due to the outdated sub modules.
The quickest way to reproduce this behavior is using modules "a" and "b":
Start b for the first time. It will work and print "a". Then change modules "a" and "b" to:
Start module "b" again. It will say that the name "a1" is not known. Then close the app, reopen and restart module "b".
Call reload() on modules that have changed. You can search the forum for 'reload' for other discussions of this issue.
omz last edited by
In version 1.5, you need to use the
reload()function manually, as suggested by @ccc. This shouldn't be necessary anymore in the next version.
Unfortunately, reload() does not seem to work with "from <module> import *" which I'm using a lot to keep identifiers short.
dgelessus last edited by
If you use
from-imports, then you need to reload the scripts in order and/or more than once, because you're assigning module attributes to new names that are no longer linked to the source module. If you access attributes over the module instead of
from-importing them, they are always looked up through the module object, which always has the up-to-date references.
In many cases it's a bad idea to use
from module import *at all, as it can cause a few obscure bugs. (For example
from os import *shadows the built-in
openfunction, meaning that you cannot open files easily anymore. Your program might also break in the future if a module is updated and gets new attributes, which are automatically star-imported into your module.) A few packages, usually large libraries and frameworks like
tkinter, are specifically designed to be star-import-safe and are often used that way.
If you only use one or two things from a module, a
fromimport can be useful to show that, but if you use a module a lot then a normal
importis cleaner, because you don't need to remember which global comes from which module. (This is another reason why star-imports are problematic - it's impossible to see where a global comes from without checking every imported module.)
This syntax will work:
import my_module reload(my_module) from my_module import my_function
ccc last edited by ccc
Talk about why wildcard imports (from <module> import *) are frowned upon.
I did not know that I could use both import variants at the same time. I will definitely try that. Also I will try to eliminate the "from m import *" variants wherever possible.
Thanks for your advice!
Changing all the imports as @ccc had suggested took care of the problem. Syntax errors are always "real" errors now. :-) Thanks a lot!