gpt4 book ai didi

python - 修改后重新加载模块

转载 作者:行者123 更新时间:2023-12-01 07:02:02 24 4
gpt4 key购买 nike

是否有在编辑时重新加载 python 模块的惯用方法?我将配置保存在 my_config_module.py 文件中,并希望自动检测和加载配置更改。目前我正在尝试这样的事情,但我发现它丑陋且不安全:

import my_config_module
import importlib

last_modification = os.stat('my_config_module.py').st_mtime

while True:
last_mod = os.stat('my_config_module.py').st_mtime
if last_mod != last_modification:
importlib.raload(my_config_module)
last_modification = last_mod
# main loop, some of my code

最佳答案

您应该将数据保存在单独的文件中,并在修改时重新加载它(从而避免重新加载代码的需要)。

关于python - 修改后重新加载模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58592116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com