我经常在Python解释器中测试我的模块,当我看到错误时,我会快速更新.py文件。但是我如何让它反映在解释器上呢?所以,到目前为止,我一直在退出并重新进入解释器,因为重新导入文件再次不适合我。
当前回答
无论你导入一个模块多少次,你都将从sys. exe中得到相同的模块副本。Modules -在第一次导入mymodule时加载
我很晚才回答这个问题,因为上面/之前的每个答案都有一点答案,所以我试图用一个简单的答案来总结。
使用内置函数:
对于Python 2。x -使用内置的重载(mymodule)函数。
对于Python 3。使用imp.reload(mymodule)。
对于Python 3.4 -在Python 3.4中,imp已弃用,转而支持importlib,即importlib.reload(mymodule)
一些注意事项:
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. 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. 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.
外部包:
reimport - reimport目前支持Python 2.4到2.7。
xreload-通过在scratch命名空间中执行模块,然后 适当地修补类、方法和函数。这就避免了 需要修补实例。将新对象复制到目标中 名称空间。
livecoding - Code reloading allows a running application to change its behaviour in response to changes in the Python scripts it uses. 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. 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.
其他回答
基本上是在allyourcode的asnwer中重新加载。但它不会改变已经实例化的对象或引用函数的底层代码。从他的回答延伸出来:
#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
>>> x = mymodule.x
>>> x()
version 1
>>> x is mymodule.x
True
# 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)
<module 'mymodule' from 'mymodule.pyc'>
>>> mymodule.x()
version 2
>>> x()
version 1
>>> x is mymodule.x
False
这里有一个很好的解释,说明你的依赖模块不会被重新加载,以及可能产生的影响:
http://pyunit.sourceforge.net/notes/reloading.html
pyunit解决这个问题的方法是通过覆盖__import__来跟踪依赖模块,然后从sys. exe中删除每个模块。模块并重新导入。不过他们可能只是装弹而已。
所以,到目前为止,我一直在退出并重新进入解释器,因为重新导入文件再次不适合我。
是的,只要再次输入import,就会得到sys.modules中模块的现有副本。
你可以用reload(module)来更新sys。模块并获得该模块的新副本,但如果任何其他模块引用了原始模块或来自原始模块的任何对象,它们将保留旧的引用,并且会发生非常混乱的事情。
So if you've got a module a, which depends on module b, and b changes, you have to ‘reload b’ followed by ‘reload a’. If you've got two modules which depend on each other, which is extremely common when those modules are part of the same package, you can't reload them both: if you reload p.a it'll get a reference to the old p.b, and vice versa. The only way to do it is to unload them both at once by deleting their items from sys.modules, before importing them again. This is icky and has some practical pitfalls to do with modules entries being None as a failed-relative-import marker.
如果你有一个模块将其对象的引用传递给系统模块——例如它注册了一个编解码器,或者添加了一个警告处理程序——你就卡住了;你无法在不混淆Python环境的情况下重新加载系统模块。
总之:除了最简单的情况外,对于由一个独立脚本加载一个自包含模块的所有情况,reload()是非常棘手的;如果像你暗示的那样,你正在使用一个“包”,你可能会更好地继续循环解释器。
蜻蜓的回答对我有用(python 3.4.3)。
import sys
del sys.modules['module_name']
下面是一个较低层次的解决方案:
exec(open("MyClass.py").read(), globals())
无论你导入一个模块多少次,你都将从sys. exe中得到相同的模块副本。Modules -在第一次导入mymodule时加载
我很晚才回答这个问题,因为上面/之前的每个答案都有一点答案,所以我试图用一个简单的答案来总结。
使用内置函数:
对于Python 2。x -使用内置的重载(mymodule)函数。
对于Python 3。使用imp.reload(mymodule)。
对于Python 3.4 -在Python 3.4中,imp已弃用,转而支持importlib,即importlib.reload(mymodule)
一些注意事项:
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. 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. 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.
外部包:
reimport - reimport目前支持Python 2.4到2.7。
xreload-通过在scratch命名空间中执行模块,然后 适当地修补类、方法和函数。这就避免了 需要修补实例。将新对象复制到目标中 名称空间。
livecoding - Code reloading allows a running application to change its behaviour in response to changes in the Python scripts it uses. 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. 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.
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录