Can I import the module again?

Asked 2 years ago, Updated 2 years ago, 142 views

Without disrupting or restarting the Python server The module needs to be updated.

If mymodule.py changes

unimport mymodule
import mymodule

Or

If mymodule.py changes

reimport mymodule

Is there a way to unimport/reimport modules in this way?

python moduke reload python-import

2022-09-22 22:21

1 Answers

Write reload() to reload modules that have already been imported

import mymodule

while True:
    # a matter of business
    if is_changed(mymodule): #Detect that the module has changed -> See the link below the post
        mymodule = reload(mymodule)

To create a if_changed() function: I want to find out where the module is (path)Please refer to


2022-09-22 22:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.