Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unload (reload) a Python module?

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?

if foo.py has changed:     unimport foo  <-- How do I do this?     import foo     myfoo = foo.Foo() 
like image 353
Mark Harrison Avatar asked Jan 13 '09 00:01

Mark Harrison


People also ask

How do you reload a Python module?

The reload() - reloads a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have mades changes to the code.

Can you Unimport something in Python?

You can use https://pypi.org/project/unimport/, it can find and remove unused imports for you.

What is reload function in Python?

The function reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "import moduleName". It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again.


1 Answers

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.

like image 101
9 revs, 8 users 53% Avatar answered Sep 19 '22 13:09

9 revs, 8 users 53%