I am trying to auto update Cython .so modules that my python program uses on the fly. After I download the new module and del module
and import module
Python seems to still be importing the older version.
From this question, I've tried this but it didn't work:
from importlib import reload
import pyximport
pyximport.install(reload_support=True)
import module as m
reload(m)
From this question, I've also tried this and it didn't work either:
del sys.modules['module']
del module
import module
I've also tried this with no success:
from importlib import reload
import my_module
my_module = reload(my_module)
Any idea how I can get Cython .SO files imported on the fly?
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
After it has downloaded the new SO file, I manually typed the commands I listed above.
m
it should be the new version, but you'll have lots of references to the old version scattered about. Could you show how you're determining that it isn't reloaded? – Azaleeazandir(my_module)
before and after the module is reloaded.dir(my_module)
shows the same properties that were present in the older module and the properties of the new module isn't shown – Apanagedownload_update()
overwrites the old SO module present in the current directory and replaces it with the new one – Apanagem = reload(m)
? i.e. reload can't changem
in place, but can return a new module. – Azaleeazansubprocess.call()
. It's not the best solution but it works. I'll try out your approach tonight and see if that works – Apanagemy_module = reload(my_module)
) but after doing that the 'new' m still hadn't changed from the old m – Apanagemy_module = reload(my_module)
is withreload_support=True
? – Azaleeazanfrom importlib import reload; import pyximport; pyximport.install(reload_support=True)
first after which I imported the module asimport module as m
and thenm = reload(m)
– Apanageimport module_suffix as module
? – Apanagepython3 program.py
it uses the updated module. I'm okay with this but is there a way to auto close and relaunch my python program in this case? – Apanage