How to Re Import an Updated Package While in Python Interpreter

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 do I reload a module after changing it?

Get it to work!

Instead of from Module import function, I should import the whole module import Module, then call the function by Module.function(). This is because

from ReloadTest import reloadtest

and

importlib.reload(ReloadTest)

can't go together.

>>> import ReloadTest
>>> ReloadTest.reloadtest(1)
Version A: 1

After making changes:

>>> importlib.reload(ReloadTest)
<module 'ReloadTest' from 'C:\\...\\ReloadTest.py'>
>>> ReloadTest.reloadtest(2)
Version B: 2

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.

Python does not show code changes from imported file

You need to explicitly reload the module, as in:

import lib # first import
# later ....
import imp
imp.reload(lib) # lib being the module which was imported before

note that imp module is pending depreciation in favor of importlib and in python 3.4 one should use: importlib.reload.

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 reload a Class in python shell?

I finally found the answer:

import MyPak
from MyPak import MyMod

after editing MyPak/MyMod.py file, to reload the class MyMod in the file MyMod.py, one needs to

import sys
del sys.modules['MyPak.MyMod']
reload(MyPak)
from MyPak import MyMod

Caveats:

  1. Executing del MyPak or del MyMod or del MyPak.MyMod does not solve the problem since it simply removes the name binding. Python only searches sys.modules to see whether the modules had already been imported. Check out the discussion in the post module name in sys.modules and globals().

  2. When reloading MyPak, python tries to execute the line from MyMod import MyMod in MyPak/__init__.py. However, it finds MyPak.MyMod in sys.modules, thus it will NOT
    reload MyMod although MyPak/MyMod.py has been updated. And you will find that no new MyPak/MyMod.pyc is generated.

Reloading submodules in IPython

IPython comes with some automatic reloading magic:

%load_ext autoreload
%autoreload 2

It will reload all changed modules every time before executing a new line. The way this works is slightly different than dreload. Some caveats apply, type %autoreload? to see what can go wrong.


If you want to always enable this settings, modify your IPython configuration file ~/.ipython/profile_default/ipython_config.py[1] and appending:

c.InteractiveShellApp.extensions = ['autoreload']     
c.InteractiveShellApp.exec_lines = ['%autoreload 2']

Credit to @Kos via a comment below.

[1]
If you don't have the file ~/.ipython/profile_default/ipython_config.py, you need to call ipython profile create first. Or the file may be located at $IPYTHONDIR.

Do I have to reload a module in python to capture changes?

This is a feature in the IPython interpreter name autoreload. It has the magic command %autoreload which allows for activating or deactivating this feature. It seems to be on by default, but I was not able to find something proving that.



Related Topics



Leave a reply



Submit