在Python3.7.1中,模块重载是如何工作的?
设置和摘要
我在Linux中运行python 3.7.1。我正在开发一个C模块,一旦模块发生变化,重新加载它将非常方便。我跟随
How do I unload (reload) a Python module?
但是我不能让它在这种环境下工作。
为了演示我的问题,我在教程中编写了一个基于垃圾邮件示例的简单模块,返回模块的构建时间。它永远不会重新加载。
代码
该实现是本教程中的垃圾邮件示例。它有一个函数hello,它返回构建时间:
return Py_BuildValue("s", __TIME__);
我正在编译和加载以下python脚本:
import os
import helloworld
print(helloworld.hello('test'))
os.system("touch helloworld.c")
os.system("python3 setup.py build")
os.system("python3 setup.py install --user")
from importlib import reload
helloworld=reload(helloworld)
print(helloworld.hello('test'))
导入模块,触摸主文件,编译并安装模块,然后重新加载。
产量
模块应该在重新加载后显示新的编译时间,但输出不会更改(我省略了一些调试消息,输出是第一行/最后一行,08:04:20):
python driver.py
08:04:20
running build
running build_ext
building 'helloworld' extension
gcc ...
running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-3.7/helloworld.cpython-37m-x86_64-linux-gnu.so -> /home/wuebbel/.local/lib/python3.7/site-packages
running install_egg_info
Removing /home/wuebbel/.local/lib/python3.7/site-packages/HelloWorld-2.1-py3.7.egg-info
Writing /home/wuebbel/.local/lib/python3.7/site-packages/HelloWorld-2.1-py3.7.egg-info
08:04:20
再次运行脚本将加载正确的模块并显示新时间:
wuebbel@02e267406db3:~/Projekte/Mandelbrot Demos/helloworld$ python driver.py
08:16:58
...
08:16:58
我的模块似乎永远不会重新加载。正确的方法是什么?