如何在Python Interpreter中重新导入更新的包? [重复]

本文翻译自:How to re import an updated package while in Python Interpreter? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I often test my module in the Python Interpreter, and when I see an error, I quickly update the .py file. 我经常在Python Interpreter中测试我的模块,当我看到错误时,我会快速更新.py文件。 But how do I make it reflect on the Interpreter ? 但是如何让它反映在口译员身上呢? So, far I have been exiting and reentering the Interpreter because re importing the file again is not working for me. 所以,我一直在退出并重新进入解释器,因为重新导入文件对我来说不起作用。


#1楼

参考:https://stackoom.com/question/2rz1/如何在Python-Interpreter中重新导入更新的包-重复


#2楼

In Python 3, the behaviour changes. 在Python 3中,行为发生了变化。

>>> import my_stuff

... do something with my_stuff, then later: ...用my_stuff做一些事情,然后再做:

>>>> import imp
>>>> imp.reload(my_stuff)

and you get a brand new, reloaded my_stuff. 你得到一个全新的,重新加载my_stuff。


#3楼

All the answers above about reload() or imp.reload() are deprecated. 以上关于reload()imp.reload()所有答案都已弃用。

reload() is no longer a builtin function in python 3 and imp.reload() is marked deprecated (see help(imp) ). reload()不再是python 3中的内置函数, imp.reload()被标记为已弃用(请参阅help(imp) )。

It's better to use importlib.reload() instead. 最好使用importlib.reload()代替。


#4楼

import sys
del sys.modules['module_name']

#5楼

dragonfly's answer worked for me (python 3.4.3). 蜻蜓的回答对我有用(python 3.4.3)。

import sys
del sys.modules['module_name']

Here is a lower level solution : 这是一个较低级别的解决方案:

exec(open("MyClass.py").read(), globals())

#6楼

No matter how many times you import a module, you'll get the same copy of the module from sys.modules - which was loaded at first import mymodule 无论你导入一个模块多少次,你都会从sys.modules获得相同的模块副本 - 这是在第一次import mymodule加载的

I am answering this late, as each of the above/previous answer has a bit of the answer, so I am attempting to sum it all up in a single answer. 我正在回答这个问题,因为上面/上面的每个答案都有一点答案,所以我试图在一个答案中总结一下。

Using built-in function : 使用内置功能

For Python 2.x - Use the built-in reload(mymodule) function. 对于Python 2.x - 使用内置的reload(mymodule)函数。

For Python 3.x - Use the imp.reload(mymodule) . 对于Python 3.x - 使用imp.reload(mymodule)

For Python 3.4 - In Python 3.4 imp has been deprecated in favor of importlib ie importlib.reload(mymodule) 对于Python 3.4 -在Python 3.4 imp已经被弃用,取而代之的importlibimportlib.reload(mymodule)

Few caveats : 几点需要注意

  • It is generally not very useful to reload built-in or dynamically loaded modules. 重新加载内置或动态加载的模块通常不是很有用。 Reloading sys , __main__ , builtins and other key modules is not recommended. 不建议重新加载sys__main__builtins和其他关键模块。
  • In many cases extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded. 在许多情况下,扩展模块不是设计为多次初始化,并且在重新加载时可能以任意方式失败。 If a module imports objects from another module using from ... import ..., calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.name) instead. 如果一个模块使用from ... import ...从另一个模块导入对象,则为另一个模块调用reload()不会重新定义从它导入的对象 - 另一种方法是重新执行from语句,另一个是改为使用import和限定名称(module.name)。
  • If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. 如果模块实例化类的实例,则重新加载定义类的模块不会影响实例的方法定义 - 它们继续使用旧的类定义。 The same is true for derived classes. 派生类也是如此。

External packages : 外包装

reimport - Reimport currently supports Python 2.4 through 2.7. 重新导入 - Reimport目前支持Python 2.4到2.7。

xreload - This works by executing the module in a scratch namespace, and then patching classes, methods and functions in place. xreload - 这是通过在临时命名空间中执行模块,然后修补类,方法和函数来实现的。 This avoids the need to patch instances. 这避免了修补实例的需要。 New objects are copied into the target namespace. 新对象将复制到目标命名空间中。

livecoding - Code reloading allows a running application to change its behaviour in response to changes in the Python scripts it uses. 实时编码 - 代码重新加载允许正在运行的应用程序更改其行为以响应其使用的Python脚本中的更改。 When the library detects a Python script has been modified, it reloads that script and replaces the objects it had previously made available for use with newly reloaded versions. 当库检测到Python脚本已被修改时,它会重新加载该脚本并替换之前可用于新重新加载版本的对象。 As a tool, it allows a programmer to avoid interruption to their workflow and a corresponding loss of focus. 作为一种工具,它允许程序员避免中断他们的工作流程并相应地失去焦点。 It enables them to remain in a state of flow. 它使它们能够保持流动状态。 Where previously they might have needed to restart the application in order to put changed code into effect, those changes can be applied immediately. 以前他们可能需要重新启动应用程序以使更改的代码生效,这些更改可以立即应用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值