Share
Go down
avatar
Admin
Posts : 155
Join date : 2017-11-11
Age : 27
View user profilehttp://net-media.yoo7.com

How do I unload (reload) a Python module?

on Thu Dec 14, 2017 12:09 pm
I have a long-running Python server and would like to be able to upgrade a service without restarting the server. What's the best way do do this?
Code:
if foo.py has changed:
    unimport foo  <-- How do I do this?
    import foo
    myfoo = foo.Foo()

Answers
You can reload a module when it has already been imported by using the reload builtin function in Python 2:
Code:
import foo

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

You can reload a module when it has already been imported by using the reload builtin function in Python 2:

import foo

while True:
# Do some things.
if is_changed(foo):
foo = reload(foo)
In Python 3, reload was moved to the imp module. In 3.4, imp was deprecated in favor of importlib, and reload was added to the latter. 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 modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. 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.
Back to top
Permissions in this forum:
You can reply to topics in this forum