How to Work with Python Modules and Update Them with Reload

In Python, a module is simply a .py
file containing reusable code such as functions, classes, or variables. You can import built-in modules or create your own custom ones within a project.
Importing Built-in and Custom Modules
import os
print(os.getcwd()) # Prints the current working directory
You can also create your own module. For example, a file named mymodule.py
:
# mymodule.py
def greet():
print("Hello from mymodule!")
Then, you can import and use it in your main script or Python shell:
import mymodule
mymodule.greet() # Output: Hello from mymodule!
Updating an Imported Module
If you modify mymodule.py
:
# mymodule.py
def greet():
print("Hi! This is an updated greeting.")
Then rerun mymodule.greet()
in the same shell or interactive session, Python will still use the previously cached version of the module, showing the old output.
Reloading the Module
To reflect the updated code without restarting the session, use importlib.reload()
:
import importlib
importlib.reload(mymodule)
mymodule.greet() # Output: Hi! This is an updated greeting.
Conclusion
Python caches modules after the first import to optimize performance. During development or testing, when frequent changes are made to a module, use importlib.reload()
to update the session with the latest version of the module without restarting the interpreter.
Subscribe to my newsletter
Read articles from Sparsh Jaipuriyar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
