django、python:shell 中的重新加载函数
我在 django IPython-shell 中工作,可以使用 manage.py shell
启动它。 当我在 shell 中加载 extern 函数进行测试时,一切正常。但是当我更改该函数时,shell 仍然具有该函数的旧版本。即使我进行了新的进口。
有谁知道如何重新加载该功能的实际版本?
谢谢问候!
编辑:我使用Windows - 如果这有什么不同的话。
I work in the django IPython-shell, which can be started with manage.py shell
.
When i load an extern function in the shell for testing, everything is alright. But when i make changes to the function, the shell still has the old version of the function. Even if i make a new import.
Does anyone know how to reload the actual version of the function?
Thanks in regards!
Edit: I use windows - if that makes a difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我通过从 sys.modules 中删除模块并进行新的导入来重新加载该模块。
I reload the module by remove it from the the sys.modules and make a new import.
尝试在所需模块上使用内置的“重新加载”功能。
http://docs.python.org/library/functions.html?highlight =重新加载#重新加载
try using built in "reload" function on desired module.
http://docs.python.org/library/functions.html?highlight=reload#reload
在导入任何代码之前启用 IPython 自动重新加载扩展:
我将它与常规 django shell 一起使用,它工作得很好,尽管它确实有一些限制:
警告:
以可靠的方式重新加载 Python 模块通常很困难,并且可能会发生意想不到的事情。 %autoreload 尝试通过用新版本替换模块中先前的函数代码对象和部分类来解决常见陷阱。这使得以下事情起作用:
一些已知的剩余警告是:
来源:https://ipython.org/ipython-doc/3/config/extensions/autoreload.html#caveats
另一个不错的选择是在单独的脚本中编写代码并将其发送到 django shell,如下所示:
Enable IPython autoreload extension before importing any code:
I use it with the regular django shell and it works perfectly, although it does have some limitations:
Caveats:
Reloading Python modules in a reliable way is in general difficult, and unexpected things may occur. %autoreload tries to work around common pitfalls by replacing function code objects and parts of classes previously in the module with new versions. This makes the following things to work:
Some of the known remaining caveats are:
source: https://ipython.org/ipython-doc/3/config/extensions/autoreload.html#caveats
Another great option is to write your code in a separate script and send it to django shell, like this:
我编写了一个脚本,它非常适合在 IPython 中重新加载模块。
它基于上面错误的答案中的技术,您可以从 sys.modules 中删除模块,然后重新导入它。
I wrote a script that works pretty well for reloading modules in IPython.
It is based on the technique in wrongite's answer above where you remove the module from sys.modules then reimport it.
我发现的最简单的解决方案是 shell_plus 和 iPython 的组合,它会自动重新加载函数。
The easiest solution I've found is combination of shell_plus and iPython which automatically reloads functions.