我正在尝试自动更新我的 python 程序即时使用的 Cython .so 模块。下载新模块后del module
and import module
Python 似乎仍在导入旧版本。
From 这个问题 https://stackoverflow.com/questions/17955194/how-to-reload-a-cython-module-interactively-using-pyximport,我已经尝试过,但没有成功:
from importlib import reload
import pyximport
pyximport.install(reload_support=True)
import module as m
reload(m)
From 这个问题 https://stackoverflow.com/questions/21630060/unloading-a-module-in-python-3-x,我也尝试过这个,但它也不起作用:
del sys.modules['module']
del module
import module
我也尝试过这个但没有成功:
from importlib import reload
import my_module
my_module = reload(my_module)
知道如何即时导入 Cython .SO 文件吗?
EDIT: Adding code for update check and download
update_filename = "my_module.cpython-37m-darwin.so"
if __name__ == '__main__':
response = check_for_update()
if response != "No new version available!":
print (download_update(response))
def check_for_update():
print("MD5 hash: {}".format(md5(__file__)))
s = setup_session()
data = {
"hash": md5(__file__),
"type": "md5",
"platform": platform.system()
}
response = s.post(UPDATE_CHECK_URL, json=data)
return response.text
def download_update(url):
s = setup_session()
with s.get(url, stream=True) as r:
r.raise_for_status()
with open(update_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return update_filename
下载新的 SO 文件后,我手动输入上面列出的命令。