Reimport a Module While Interactive

Reimport a module while interactive

For Python 3.4+:

import importlib
importlib.reload(nameOfModule)

For Python < 3.4:

reload(my.module)

From the Python docs

Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter.

Don't forget the caveats of using this method:

  • When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem, but if the new version of a module does not define a name that was defined by the old version, the old definition is not removed.

  • If a module imports objects from another module using from ... import ..., calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.*name*) instead.

  • If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes.

how to reimport module to python then code be changed after import

For Python 2.x

reload(foo)

For Python 3.x

import importlib
import foo #import the module here, so that it can be reloaded.
importlib.reload(foo)

How to easily reload/reimport a module while working in a console

This will reset globals here is an example with itertools.

import itertools
from itertools import *
from importlib import reload

itools = reload(itertools)

for k, v in itools.__dict__.items():
if k in globals():
globals()[k] = v

Do I need to reimport modules in other modules

Yes, you have to import everything you need in every module. If in module_a you use the function B defined in module_b then you must import module_b inside module_a, or at least import the B function from module_b.

Explanation:

In Python modules are objects! When you import a module its code is executed and everything that gets defined there is attached to the module object's __dict__:

$ echo 'a=1' > testing.py
$ python
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import testing
>>> 'a' in testing.__dict__
True

The module's __dict__ contains also the usual global built-ins.
Anything that is defined inside a module uses the module's __dict__ as global scope.
In python there is no such a thing as a "global variable" meaning a variable accessible from every module/class/function. Globals variables are actually just a module's instant variables.

If you want to import some items from a module into an other module's namespace you can use the from syntax:

from module_a import functionA, functionB, classA, CONSTANT

You can import everything using the *:

from module_a import *

But avoid using the from ... import * syntax! You'll get namespacing clashes like in C includes. Only do this if a module states in its documentation that it is *-import safe.
To make a module *-import safe you can define the __all__ global, which should be a sequence of strings representing the identifiers that should be exported when a *-import is executed.

For example:

#module_a
A = 1
B = 2
__all__ = ['A']

#module_b
from module_a import *
print(A) #okay
print(B) #NameError, B was not exported!

How to reimport module from a different folder in the python code?

The usual way to go is:

try:
from better_config import worker
except ImportError:
from config import worker

Reloading is intended to refresh the same module after code update and not to import a different one.

How do I unload (reload) a Python module?

You can reload a module when it has already been imported by using importlib.reload():

from importlib import reload  # Python 3.4+
import foo

while True:
# Do some things.
if is_changed(foo):
foo = reload(foo)

In Python 2, reload was a builtin. In Python 3, it was moved to the imp module. In 3.4, imp was deprecated in favor of importlib. When targeting 3 or later, either reference the appropriate module when calling reload or import it.

I think that this is what you want. Web servers like Django's development server use this so that you can see the effects of your code changes without restarting the server process itself.

To quote from the docs:

  • Python module’s code is recompiled and the module-level code re-executed, defining a new set of objects which are bound to names in the module’s dictionary by reusing the loader which originally loaded the module. The init function of extension modules is not called a second time.
  • As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero.
  • The names in the module namespace are updated to point to any new or changed objects.
  • Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.

As you noted in your question, you'll have to reconstruct Foo objects if the Foo class resides in the foo module.



Related Topics



Leave a reply



Submit