Can I import the module again?

Asked 1 years ago, Updated 1 years ago, 133 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.