Python
package
interpreter
import
duplicate

How to re import an updated package while in Python Interpreter?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In a running Python interpreter, importing a module again with a plain import statement does not reload the updated source file. Python caches imported modules in sys.modules, so if you edit a package during an interactive session, you usually need importlib.reload() or a full interpreter restart.

Why a Second import Does Not Help

When Python imports a module for the first time, it stores the module object in an internal cache. Later imports of the same module name reuse that existing object.

That behavior is normally a feature because it prevents repeated import work, but it also means this pattern does not pick up file changes:

python
import mymodule
import mymodule

The second line just reuses the already loaded module object.

Reload with importlib.reload

The standard way to reload a module in the same interpreter session is:

python
1import importlib
2import mymodule
3
4# Edit mymodule.py on disk, then run:
5importlib.reload(mymodule)

reload() re-executes the module code in the existing module object. That is usually enough during interactive debugging or exploratory work.

A complete example:

python
1import importlib
2import greeting
3
4print(greeting.say_hello())
5
6# After editing greeting.py on disk:
7importlib.reload(greeting)
8print(greeting.say_hello())

If the file changed and the reload succeeded, the second call should reflect the new implementation.

Re-importing Names Is Different from Reloading Modules

A common source of confusion is importing names directly:

python
from greeting import say_hello

If you reload the module later, the name say_hello that was already imported into your local scope still points to the old function object.

The safer pattern during iterative work is:

python
1import importlib
2import greeting
3
4importlib.reload(greeting)
5greeting.say_hello()

Or, if you really want direct names again, re-run the from ... import ... statement after the reload.

Package Reloading and Dependencies

Reloading one module does not automatically reload every dependency it imported. If package_a imports package_b, and you changed package_b, reloading only package_a may not be enough.

In practice:

  • Reload the module you edited.
  • Reload dependent modules if they cached objects from it.
  • Restart the interpreter when the dependency graph becomes confusing.

For small experiments, reload is convenient. For deeper package changes, a restart is often the cleaner choice.

Watch Out for Module State

Reloading re-executes the module code, but it does not reset every reference in the rest of the interpreter. Existing objects created before reload may still behave according to the old code.

For example, if you created an instance from a class before reloading the module, that instance is still an instance of the old class definition.

That is why reload is best for quick function-level iteration, not as a perfect substitute for a fresh process.

IPython and Notebook Convenience

If you work in IPython or Jupyter, autoreload can reduce manual steps:

python
%load_ext autoreload
%autoreload 2

That setting tries to reload modules automatically before executing code. It is convenient, but the same caveats about stale references and existing objects still apply.

Common Pitfalls

The most common mistake is running import module again and expecting Python to read the file from disk a second time. It usually will not.

Another issue is reloading the module but continuing to use names imported earlier with from module import something. Those names still point at the old objects unless you import them again.

People also overestimate what reload can fix. If your session already holds many instances, cached globals, or cross-module references, restarting the interpreter may be faster and safer.

Finally, if the file on disk contains a syntax error after your edit, reload() will fail. In that case, fix the code and reload again.

Summary

  • Plain import does not reload an updated module in the same interpreter session.
  • Use importlib.reload(module) to re-execute module code.
  • Prefer import module style during interactive work so reloaded names stay attached to the module.
  • Reloading one module does not automatically refresh all dependencies or old object instances.
  • When the session becomes stateful and messy, restarting Python is often the cleanest solution.

Course illustration
Course illustration

All Rights Reserved.