How to reload python module imported using `from module import *`

python-import
python reload(module)
python reload all modules
python importlib reload
reload() argument must be a module
python reload(sys)
python reload file
python3 reload module

I saw in this useful Q&A that one can use reload(whatever_module) or, in Python 3, imp.reload(whatever_module).

My question is, what if I had said from whatever_module import * to import? Then I have no whatever_module to refer to when I use reload(). Are you guys gonna yell at me for throwing a whole module into the global namespace? :)

I agree with the "don't do this generally" consensus, but...

The correct answer is:

import X
reload(X)
from X import Y  # or * for that matter

Automatically Reload Modules with %autoreload, How does importing in Python work? Turns out that, for efficiency reasons, when you import a module in an interactive Python session, Python  The import statements don’t promote the visibility of the attributes, and the imported file can’t see the attributes in the importing file. Reloading Modules. If you try to re-import the module after importing it on the top of your code, Python will fetch the already loaded module again.

Never use import *; it destroys readability.

Also, be aware that reloading modules is almost never useful. You can't predict what state your program will end up in after reloading a module, so it's a great way to get incomprehensible, unreproduceable bugs.

31.5. importlib – The implementation of import, In many cases extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded. If a module imports objects from  Reloading modules in Python. reload() reloads a previously imported module. 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. The return value is the module object. Note: The argument should be a module which has been successfully imported.

A

from module import *

takes all "exported" objects from module and binds them to module-level (or whatever-your-scope-was-level) names. You can reload the module as:

reload(sys.modules['module'])

but that won't do you any good: the whatever-your-scope-was-level names still point at the old objects.

imp — Access the import internals, In many cases, however, extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded. If a module imports  When you import a module, Python looks if this module is in sys.modules dictionary. If not, it runs the module, and puts the resulting module object in sys.modules. So, to unimport a module, one might use. import sys sys.modules.pop('your_module_name_here') The next time it is imported, python will re-execute the module.

A cleaner answer is a mix of Catskul's good answer and Ohad Cohen's use of sys.module and direct redefinition:

import sys
Y = reload(sys.module["X"]).Y  # reload() returns the new module

In fact, doing import X creates a new symbol (X) that might be redefined in the code that follows, which is unnecessary (whereas sys is a common module, so this should not happen).

The interesting point here is that from X import Y does not add X to the namespace, but adds module X to the list of known modules (sys.modules), which allows the module to be reloaded (and its new contents accessed).

More generally, if multiple imported symbols need to be updated, it is then more convenient to import them like this:

import sys
reload(sys.module["X"])  # No X symbol created!
from X import Y, Z, T

5. The import system, When a module is first imported, Python searches for the module and if found, it creates a The import system passes in a target module only during reload. reload(module) 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.

I've found another way to deal with reloading a module when importing like:

from directory.module import my_func

It's nice to know how do modules are being imported generally. The module is searched in sys.modules dictionary. If it already exists in sys.modules - the module will not be imported again.

So if we would like to reload our module, we can just remove it from sys.modules and import again:

import sys
from directory.module import my_func
my_func('spam')
# output: 'spam'

# here I have edited my_func in module.py

my_func('spam') # same result as above
#output: 'spam'


del sys.modules[my_func.__module__]
from directory.module import my_func

my_func('spam') # new result
#output: 'spam spam spam spam spam'

If You would like to get reloaded module when running whole script, you could use exception handler:

try:
    del sys.modules[my_func.__module__]

except NameError as e:
    print("""Can't remove module that haven't been imported.
    Error: {}""".format(e))

from utils.module import my_func

..........
# code of the script here

importlib — The implementation of import, In many cases extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded. If a module imports objects from  importlib.reload (module) ¶ 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.

6. Modules, The imported module names are placed in the importing module's global one module you want to test interactively, use reload() , e.g. reload(modulename) . Python Module Search Path. While importing a module, Python looks at several places. Interpreter first looks for a built-in module. Then(if built-in module not found), Python looks into a list of directories defined in sys.path.

6. Modules, The imported module names are placed in the importing module's global symbol use importlib.reload() , e.g. import importlib; importlib.reload(modulename) . Any file with extension .py can be referred as a module and the functions defined inside the module can be used in another program by simply using the import statement. Suppose we need a function to find factorial in many programs. So, instead of defining a function to find the factorial in each program,

Reloading modules in Python?, The argument passed to the reload() must be a module object which is successfully imported before. Syntax. import importlib importlib.reload(sys)  So just importing it multiple times wouldn't cause a reload. Thanks! So I just found this option in spyder which says: "User Module Reloader (UMR): UMR forces Python to reload modules which were imported when executing a file in a Python or IPython console with the runfile function." And I deactivated it and now it works perfectly!

Comments
  • With regard to your final question: Yes.
  • (1) Given that this question was asked on April 1st (even if two years ago), I would have expected a bit more humour in the answers. (2) The Python tutorial says "However [importing * from a module] is okay to save typing in an interactive session; it would seem to me that this is exactly the situation in which reloading is called for (you've just repaired an error in a function you are testing interactively, and wish to not leave the interpreter to keep your other testing data). (3) Catskul did indeed provide the unique correct answer to the question as posed; please accept and vote up!
  • Catskul's answer is good but is not the "unique correct" one: it unnecessarily creates a new symbol X that is generally not in the original code (see Ohad Cohen's or my answer for how to avoid this side effect).
  • This typically adds symbol X to the namespace, so it is a bit cleaner to respect X not being in the namespace, by doing instead reload(sys.modules["X"]). Granted, this may required adding sys to the namespace, but it is such a common module that this should not shadow anything (whereas X might be shadowed by the added import X).
  • In case you import a module with an alias, use the alias in reload. For example for import XX as X use reload(X)
  • it's useful in interactive mode though, i am always updating my module and then having to get out and back into python.
  • that's also why i use import * from ___, is just for interactive mode so i don't have to type so much
  • if you use python -i foo.py, you can have python run all your setup code before it gives you a prompt. (and instead of from somepackage.foo import *, why not use from somepackage import foo as f then refer to f.yourObjects, etc?
  • ahh, but i still can't do from somepackage import * as whatever (at least in my python 3)...i know none of u want me to be able to do this anyway! ;)
  • @JarethHolt, you've probably figured this out by now, but just do reload(mod)
  • Nice! I like that solution. Thanks for sharing!
  • this comment does not answer the original question