Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Reloading modules in Python
Next article icon

Reloading modules in Python

Last Updated : 21 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a case where we want to update and test a Python module without restarting the interpreter. This is especially helpful during development when you're modifying module files externally and want those changes to reflect immediately. Python allows us to reload a previously imported module using the reload() function. For example, if you've edited a utility file utils.py while your script is running, reloading lets you re-import the updated content without restarting the Python shell.

When Should You Reload a Module?

  • During debugging or live testing
  • When using interactive interpreters like IPython or Jupyter
  • While developing plugins or modular systems
  • To reflect changes made in external .py files without restarting the session

Reloading Modules in Python 2.x

In Python 2.x, you can directly use the built-in reload() function to reload a module that was previously imported.

Python
import mymodule  # Modify 'mymodule.py' externally here...  reload(mymodule) 

Explanation:

  • mymodule is first imported using import.
  • After making external changes to mymodule.py, calling reload(mymodule) reloads the updated version without restarting the interpreter.
  • No need to re-import — reload() works on an already imported module object.

Reloading Modules in Python 3.0 to 3.3

In Python versions 3.0 to 3.3, the built-in reload() function was removed. Instead, you need to import it from the imp module.

Python
import mymodule import imp  # Modify 'mymodule.py' externally...  imp.reload(mymodule) 

Explanation:

  • First, mymodule is imported as usual.
  • The imp module provides the reload() function during this version range.
  • After editing mymodule.py, imp.reload(mymodule) reloads the module with updated changes.

Note: The imp module is deprecated as of Python 3.4 and is replaced by importlib.

Reloading Modules in Python 3.4 and Above

Starting from Python 3.4, the recommended way to reload a module is by using importlib.reload().

Python
import mymodule import importlib  # Modify 'mymodule.py' externally...  importlib.reload(mymodule) 

Explanation:

  • mymodule is first imported normally.
  • Then, importlib.reload(mymodule) reloads the updated module.
  • This is the current and official way to reload modules in Python 3.4+.

Note: Use this method in all modern Python scripts and notebooks when testing module changes.

Can You Unload a Module in Python?

Currently, Python does not provide a built-in way to fully unload a module once it's imported. Once a module is loaded into memory, it stays available in sys.modules until the program ends. Although you can delete a module from sys.modules, this doesn't guarantee its complete removal from memory or that its references are gone:

Python
import mymodule import sys  del sys.modules['mymodule'] 


This will only remove the module from the sys.modules cache. If any variable or object is still referencing the module, it won’t be fully unloaded.

Why Unloading Isn’t Supported?

  • Python manages modules globally in memory.
  • Unloading dynamically could lead to inconsistent behavior if parts of your code still hold references to the old module.

Next Article
Reloading modules in Python

K

kartik
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

    Python Module Index
    Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
    4 min read
    C Extension Module using Python
    Writing a simple C extension module directly using Python’s extension API and no other tools. It is straightforward to make a handcrafted extension module for a simple C code. But first, we have to make sure that the C code has a proper header file. Code #1 : C #include <math.h> extern int gcd
    4 min read
    Python Modules
    Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
    7 min read
    Built-in Modules in Python
    Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
    9 min read
    External Modules in Python
    Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences