how to reimport module to python then code be changed after import
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When Python imports a module, it caches that module object in sys.modules. Changing the source file afterward does not automatically update the already imported module in the current process. If you want Python to pick up the new code without restarting the interpreter, the usual tool is importlib.reload.
Why a Second import Usually Does Not Help
A normal import statement checks whether the module is already loaded. If it is, Python reuses the cached module object instead of re-executing the file.
That behavior is a feature. It avoids repeated work and keeps module-level state consistent. It also explains why editing a module file after import seems to have no effect.
Reload a Module with importlib.reload
To re-execute the module's code, import the module object itself and pass it to reload.
This tells Python to re-read and re-execute the module source. The module object stays in place, but its contents are updated from the file.
The important detail is that reload works on a module object, not a module name string.
A Small Example
Suppose mymodule.py initially contains:
You import it in a Python session:
Output:
Now change the file to:
Then reload it:
Now the output becomes:
That is the normal development workflow in interactive shells and notebooks when a full restart is inconvenient.
from module import name Has a Catch
One of the most common reasons reload appears not to work is this form:
In that case, greet is copied into the current namespace. Reloading mymodule does not automatically replace the already imported local name.
If you plan to reload often, prefer import mymodule and then call mymodule.greet().
Reloading Has Limits
reload re-executes the module, but it does not magically rebuild everything that depended on the old objects. Existing class instances continue to use the class definitions they were created from. Functions copied elsewhere may still point to old code objects. Any module-level mutable state may also survive in surprising ways depending on how the module reinitializes itself.
That is why reload is useful mainly for interactive development, not as a general production mechanism for live code replacement.
Common Pitfalls
The first pitfall is using import module again and expecting it to refresh the file. It usually will not, because Python consults sys.modules first.
Another mistake is mixing reload with from module import name and then wondering why the imported function or class did not change. Reload updates the module object, not every local alias created earlier.
Developers also often forget that module-level side effects run again on reload. If the module opens files, registers handlers, or mutates global state during import, reloading can duplicate those effects.
Finally, if you are in a notebook environment, remember that reload solves only part of the problem. Old objects created in earlier cells may still be alive. Sometimes a kernel restart is the cleaner and more predictable option.
Summary
- Python caches imported modules in
sys.modules, so a secondimportusually does not reload changed code. - Use
importlib.reload(module)when you need to re-execute a module in the current process. - Prefer
import moduleoverfrom module import nameif you expect to reload frequently. - Reloading updates the module object, but not every old reference or instance created before the reload.
- For complex stateful programs, restarting the interpreter is often safer than repeated reloads.

