Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Reloading a Module

In this lesson, you’ll learn about reloading a module. For efficiency, a module is only loaded once per interpreter session. That’s fine for function and class definitions, which typically make up the bulk of a module’s contents.

But a module can contain executable statements as well, usually for initialization. Be aware that these statements will only be executed the first time a module is imported.

Take a look at mod.py:

s = "Computers are useless. They can only give you answers."
a = [100, 200, 300]

def printy(arg):
    print(f'arg = {arg}')

class Classy:
    pass

print(f'a = {a}')

Here’s what you get:

>>>
>>> import mod
a = [100, 200, 300]
>>> import mod
>>> import mod
>>> mod.a
[100, 200, 300]
>>> mod.s
'Computers are useless. They can only give you answers.'

The print() statement is not executed on subsequent imports. (For that matter, neither is the assignment statement, but as the final display of the value of mod.a shows, that doesn’t matter. Once the assignment is made, it sticks.)

If you make a change to a module and need to reload it, you need to either restart the interpreter or use a function called reload() from module importlib. Without exiting the REPL, change mod.py:

s = "Computers are useless. They can only give you answers."
a = [100, 200, 300]

def printy(arg):
    print(f'arg = {arg}')

class Classy:
    pass

print(f'a is equal {a}')

Now import mod again:

>>>
>>> import mod
>>> exit()

Nothing happens, and the changes to the file are not shown. Exit the REPL and then enter it again:

>>>
>>> import mod
a is equal [100, 200, 300]
>>> import mod
>>> import importlib
>>> importlib.reload(mod)
a is equal [100, 200, 300]
<module 'mod' from '/Users/chris/ModulesAndPackages/mod.py'>

Jarrod on May 16, 2020

Ok, and the usefulness is how? I am not seeing the use case for this and why it is useful to know about it?

Chris Bailey RP Team on May 18, 2020

Hi @cartwrightjarrod, The usefulness will depend on the developer. If you never make any changes to your modular code while in an interactive environment, you may not need it. The important thing to understand is that if you change thing inside your modules, what you have previously imported will not be updated, until you restart your session or your use importlib.reload(). The other place I see this happening is in jupyter notebooks, where the current state may not be clear.

tarunkrishna on June 5, 2020

Hi, i was wondering about the difference between importlib.reload(my_module) to simply reload(my_module). also i was wondering when i reload with reload function of importlib, will it reload recursivly? as if the module my_module import other modules, would they be reloaded as well? if not, is there an easy way to do it? is there a good guide to the under the hood of the import how it keeps the references of the imports? as i noticed it can be pretty tricky when you wish to reload a module which has dependencies.

thanks

Become a Member to join the conversation.