Skip to content Skip to sidebar Skip to footer

Why Can't You Re-import In Python?

There are plenty of questions and answers regarding re-imports on SO, but it all seems very counter-intuitive without knowing the mechanisms behind it. If you import a module, chan

Solution 1:

When you import a module, it is cached in sys.modules. Any attempt to import the same module again within the same session simply returns the already existing module contained there. This speeds up the overall experience when a module is imported from multiple places. It also allows a module to have its own objects shared between all of the imports, since the same module is returned each time.

As mentioned you can use reload to re-import a whole module. Check the documentation for the caveats, because even this is not fool-proof.

When you import specific items from a module, the whole module is imported as above and then the objects you requested are placed in your namespace. reload doesn't work because these objects aren't modules, and you never received a reference to the module itself. The work-around is to get a reference to the module, reload it, then re-import:

>>> from foo import baz
>>> print baz
original
>>> # change foo.py from baz = 'original' to baz = 'changed'
>>> import foo
>>> reload(foo)
>>> from foo import baz
>>> print baz
changed

Solution 2:

The simple explanation for "why" is that import statements involve two operations: loading the module (i.e., running the code inside it), and importing names into the namespace of the importing module. Re-importing only redoes the second operation. The reason is that the first operation may be expensive in terms of computation and memory resources.

reload is provided as a way to redo the first operation, but, as you saw, it requires the module name. Don't be fooled, however, into thinking that from foo import bar doesn't load the entire module. It does. All the module's code is run, it's just that you only get to refer to the names (e.g., bar) that you explicitly import. For this reason, there is little real difference between importing the module and not importing it; the only differences are namespace issues. So if you think you might want to reload the module foo, you should go ahead and do import foo. If you want, you can do this to refresh your imported names:

import foo
from foo import bar
# later...
reload(foo)
from foo import bar

There's another reason you can't reimport individual names, which is that Python has no way of changing the objects referred to; it can only rebind the names. Suppose you do this:

from foo import bar
newBar = bar
from foo import bar

Even if the second import really did refresh the value of bar, it could never refresh the value of newBar, because neither foo nor bar has any way of knowing that you created an extra name to refer to bar. Avoiding the rebinding of individual names prevents you from falling into this trap; if people thought that reimporting would refresh the names, it would be easy to forget that it still wouldn't refresh the objects.


Post a Comment for "Why Can't You Re-import In Python?"