Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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:

Python
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:

Python
>>> 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:

Python
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:

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

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

Python
>>> 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'>

00:00 In this video, I’ll show you about reloading a module. Up to now, you’ve been simply importing a module once. Even if you were to try, a module is only loaded once per interpreter session, and that’s going to work fine for functions and class definitions and importing all those objects initially.

00:17 But sometimes modules can contain executable statements as well. Usually the executable statements are only for initialization, and it makes sense that these statements will only be executed the first time a module is imported. That’s usually okay.

00:33 Let me have you take a look at that.

00:36 Go ahead and reopen mod.py, and inside of it I’m going to have you go ahead and clear out this if __name__ == '__main__':. Just keep a simple print() statement for printing a out. In fact, modify it a little bit and use an f-string.

00:52 And that can show that every time that this module is imported, it would print this out, right? So, save this extra line. Okay, down here in the terminal, what happens if I start the REPL and import mod?

01:06 Well, you can see it here. Initially as it imported mod, it went ahead and did the print() statement inside there using the f-string, and inside here mod.a is available. Now what if you import mod again inside the same session?

01:19 Nothing happens. Is mod.a still here? Yeah. In fact, all the objects are available. So, why would it not rerun this code? Well, that’s the thing. It only is going to import it once and it doesn’t need to re-import it again, so it’s sort of an efficiency thing. Well, what if you needed to reload this?

01:40 Is it possible to reload a module? If you really needed to, there’s two choices here. For one, you can restart the interpreter, and you’ve practiced that multiple times.

01:52 Or there is a module that you can import called importlib, and it includes a function called reload(). Let me show you what that looks like.

02:01 One of the choices is to go ahead and restart the interpreter, which is something that you’ve done a lot now, exiting out and restarting again. But sometimes you may want to actually reload this in, especially if you’ve maybe changed the code and you don’t want to restart the interpreter. Like, maybe you change this a little bit and you save, and now you want to import modthose objects are still there and importing mod didn’t print out anything again.

02:26 If you need this change, the one way that you could do this is to exit and restart.

02:32 And if you were to import, then you can see the change, right? But if you were to import mod again— again, it’s not going to re-execute the code. It’s already been imported.

02:43 So, something to be aware of. If you’re making changes to your module, you would need to use another tool. And that tool is something that can be imported from importlib. And inside of importlib is a function called reload().

02:59 Now that you’ve imported it, reload() is going to reload the module and then return it again. So, in this case, the module is called mod, so importlib.reload(mod) would actually reload it in here.

03:12 It’s actually been reloaded from the source here, executed the little print() statement. So now you have a handy tool that if you’re making changes to your module, you can reload them using importlib and the function reload().

03:30 In the next video, you’ll start to explore Python packages.

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

Avatar image for Chris Bailey

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.

Avatar image for tarunkrishna

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.