python重新导入模块_如何“重新导入”模块到python然后在导入后更改代码

I have a foo.py

def foo():

print "test"

In IPython I use:

In [6]: import foo

In [7]: foo.foo()

test

Then I changed the foo() to:

def foo():

print "test changed"

In IPython, the result for invoking is still test:

In [10]: import foo

In [11]: foo.foo()

test

Then I use:

In [15]: del foo

In [16]: import foo

In [17]: foo.foo()

test

I delete the foo.pyc in same folder foo.py exists, but still no luck.

May I know how to reimport the updated code in runtime?

解决方案

For Python 2.x

reload(foo)

For Python 3.x

import importlib

import foo #import the module here, so that it can be reloaded.

importlib.reload(foo)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值