Question

I can simply reload module when normally imported (imp.reload(module or alias)). But is it possible to reload everything after imported into current namespace (from module import *) ?

imp.reload(module) doesn't work saying that "name: module is not defined"

Was it helpful?

Solution

When you do from module import * everything from that module is fetched into the current namespace and at the end the reference to module is removed. But, due to module caching the module object can still be accessed from sys.modules, so that in case you do some more import later one than it doesn't have to fetch the module again.

That said, one way to do what you're expecting is:

import sys
from foo import *
print A, B        #prints 1, 2
A, B = 100, 200
mod = reload(sys.modules['foo'])#use imp.reload for Python 3  
vars().update(mod.__dict__)     #update the global namespace
print A, B        #prints 1, 2

As a side note, using import * is usually frowned upon:

Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top