Selasa, 12 Maret 2019

reload module with pyximport?

I have a python program that loads quite a bit of data before running. As such, I'd like to be able to reload code without reloading data. With regular python, importlib.reload has been working fine. Here's an example:

setup.py:


from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

extensions = [
Extension("foo.bar", ["foo/bar.pyx"],
language="c++",
extra_compile_args=["-std=c++11"],
extra_link_args=["-std=c++11"])
]
setup(
name="system2",
ext_modules=cythonize(extensions, compiler_directives={'language_level' : "3"}),
)

foo/bar.py


cpdef say_hello():
print('Hello!')

runner.py:


import pyximport
pyximport.install(reload_support=True)

import foo.bar
import subprocess
from importlib import reload

if __name__ == '__main__':

def reload_bar():
p = subprocess.Popen('python setup.py build_ext --inplace',
shell=True,
cwd='<your directory>')
p.wait()

reload(foo.bar)
foo.bar.say_hello()

But this doesn't seem to work. If I edit bar.pyx and run reload_bar I don't see my changes. I also tried pyximport.build_module() with no luck -- the module rebuilt but didn't reload. I'm running in a "normal" python shell, not IPython if it makes a difference.



from reload module with pyximport?

reload module with pyximport? Rating: 4.5 Diposkan Oleh: Admin

0 komentar:

Posting Komentar

Popular Posts