繁体   English   中英

python3.7中的模块重新加载如何工作?

[英]How does module reload in python3.7 work?

如何在python 3.7.1中重新加载模块?

设置和摘要

我在Linux中运行python 3.7.1。 我正在开发一个C模块,一旦更改了模块就可以很方便地重新加载它。 我遵循了如何卸载(重新加载)Python模块? ,但我无法在这种环境下使用它。

为了演示我的问题,我在教程中基于SPAM示例编写了一个简单的模块,该模块返回模块的构建时间。 它将永远不会被重新加载。

该实现是该教程中的垃圾邮件示例。 它有一个函数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

看来我的模块永远都不会重新加载。 正确的做法是什么?

我对重新加载包含已编译代码的模块的可行性感到怀疑,请阅读以下答案: https : //stackoverflow.com/a/48814213/6451573 ,这表明PEP 489更改了模块重新加载(在python 3.6和3.7之间),可能是“破坏共享库重载。

由于共享库加载的限制(POSIX上的dlopen和Windows上的LoadModuleEx都受限制),在磁盘上更改修改后的库后,通常无法加载它。

那可以解释您遇到的问题。

如果是可行的情况,我建议您有一个主python程序来创建程序包,然后使用单独的python进程来加载它。

subprog.py:

import helloworld
print(helloworld.hello('test'))

master.py

import os,subprocess,sys
def runit():
   subprocess.run([sys.executable,os.path.join(os.path.dirname(__file__),"subprog.py")])

runit()

os.system("touch helloworld.c")
os.system("python3 setup.py build")
os.system("python3 setup.py install --user")

runit()

使用sys.executable可以确保将相同的python可执行文件用于主python进程和子进程,因此更加透明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM