64

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? :)

Community
  • 1
  • 1
murftown
  • 1,287
  • 1
  • 10
  • 13
  • 19
    With regard to your final question: Yes. – JoshAdel Apr 01 '11 at 17:32
  • 9
    (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! – Marc van Leeuwen Apr 08 '13 at 13:39
  • 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). – Eric O. Lebigot Aug 01 '18 at 16:24

8 Answers8

69

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

The correct answer is:

from importlib import reload
import X
reload(X)
from X import Y  # or * for that matter
alan ocallaghan
  • 3,116
  • 17
  • 37
Catskul
  • 17,916
  • 15
  • 84
  • 113
  • 2
    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`). – Eric O. Lebigot Aug 01 '18 at 16:13
  • 2
    In case you import a module with an alias, use the alias in `reload`. For example for `import XX as X` use `reload(X)` – Duccio A Jun 08 '19 at 12:03
  • This does not seem to work when I have imported an object from a module of a package (`from package.module import object`). – nocibambi Jun 10 '21 at 20:21
  • for Python 3, and in particular 3.4+, you need to add `from importlib import reload`. Unfortunately, editing this answer yields the error "Suggested edit queue is full". – Pierre H. Jul 20 '22 at 08:59
8

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.

Allen
  • 5,034
  • 22
  • 30
  • 24
    it's useful in interactive mode though, i am always updating my module and then having to get out and back into python. – murftown Apr 01 '11 at 20:06
  • that's also why i use import * from ___, is just for interactive mode so i don't have to type so much – murftown Apr 01 '11 at 20:07
  • 6
    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? – Allen Apr 01 '11 at 20:15
  • 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! ;) – murftown Apr 01 '11 at 21:03
  • I have a key question on this point. (5 years late.) If I use `import module as mod` to avoid `from module import *`, how do I reload `module` as `mod`? – Jareth Holt Feb 26 '16 at 17:50
  • 1
    @JarethHolt, you've probably figured this out by now, but just do `reload(mod)` – Junier Aug 25 '16 at 20:01
  • Actually, you can officially use `from math import *` (mostly because mathematical functions have well-known names). – Eric O. Lebigot Aug 01 '18 at 16:06
7

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

import sys
Y = reload(sys.modules["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.modules["X"])  # No X symbol created!
from X import Y, Z, T
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
4

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.

tzot
  • 92,761
  • 29
  • 141
  • 204
3

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
ksiu
  • 31
  • 3
1

When importing using from whatever_module import whatever, whatever is counted as part of the importing module, so to reload it - you should reload your module. But just reloading your module you will still get the old whatever - from the already-imported whatever_module, so you need to reload(whatever_module), and than reload your module:

# reload(whatever_module), if you imported it
reload(sys.modules['whatever_module'])
reload(sys.modules[__name__])

if you used from whatever_module import whatever you can also consider

whatever=reload(sys.modules['whatever_module']).whatever

or

whatever=reload(whatever_module).whatever
Ohad Cohen
  • 5,756
  • 3
  • 39
  • 36
0
import re

for mod in sys.modules.values():
    if re.search('name', str(mod)):
        reload(mod)
jennifer
  • 11
  • 2
-2

for python 3.7 :

from importlib import reload #import function "reload"
import YourModule #import your any modules
reload(YourModule) #reload your module

Reload function can be called from your own function

def yourFunc():
   reload(YourModule)