python模块如何导入解释器,如何在Python解释器中重新导入更新的包?

在Python3中,`reload()`函数已不再可用,取而代之的是`imp.reload()`。当调用`imp.reload(module)`时,模块的代码将被重新编译并执行,更新模块内的对象绑定。旧对象仅在引用计数降为零时被回收。此功能允许在不退出Python解释器的情况下更新模块并查看变化,例如在交互式环境中修改模块后立即生效。
摘要由CSDN通过智能技术生成

“重载不再是Python 3中的函数。使用imp.reload()而不是从注释中复制

使用reload内置函数:

When reload(module) is executed:

Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The init function of extension modules is not called a second time.

As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero.

The names in the module namespace are updated to point to any new or changed objects.

Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.

例:

# Make a simple function that prints "version 1"

shell1$ echo 'def x(): print "version 1"' > mymodule.py

# Run the module

shell2$ python

>>> import mymodule

>>> mymodule.x()

version 1

# Change mymodule to print "version 2" (without exiting the python REPL)

shell2$ echo 'def x(): print "version 2"' > mymodule.py

# Back in that same python session

>>> reload(mymodule)

>>> mymodule.x()

version 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值