python3.7模块内容_如何在python3.7中重新加载模块?

在Python3.7.1环境下,开发者遇到C模块无法自动重载的问题。尝试使用`importlib.reload()`函数,但模块的更新并未在不重启的情况下生效。文中通过编写和编译C模块的示例,展示了模块加载和更新的过程,并在重新加载后发现模块时间戳未改变,提示可能存在模块缓存问题。为解决此问题,需要理解Python模块的加载机制和正确重载的方法。
摘要由CSDN通过智能技术生成

在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

我的模块似乎永远不会重新加载。正确的方法是什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值