django、python:shell 中的重新加载函数

发布于 2024-09-26 16:31:04 字数 226 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深空失忆 2024-10-03 16:31:04

我通过从 sys.modules 中删除模块并进行新的导入来重新加载该模块。

import sys
sys.modules.pop('your.module')
import your.module
# or
from your.module import your_function

I reload the module by remove it from the the sys.modules and make a new import.

import sys
sys.modules.pop('your.module')
import your.module
# or
from your.module import your_function
左耳近心 2024-10-03 16:31:04

尝试在所需模块上使用内置的“重新加载”功能。

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

痕至 2024-10-03 16:31:04

在导入任何代码之前启用 IPython 自动重新加载扩展:

%load_ext autoreload
%autoreload 2

我将它与常规 django shell 一起使用,它工作得很好,尽管它确实有一些限制:

警告:

以可靠的方式重新加载 Python 模块通常很困难,并且可能会发生意想不到的事情。 %autoreload 尝试通过用新版本替换模块中先前的函数代码对象和部分类来解决常见陷阱。这使得以下事情起作用:

  • 重新加载“xxx”时,通过“from xxx import foo”导入的函数和类将升级到新版本。
  • 类的方法和属性在重新加载时升级,因此在重新加载之前创建的对象“c”上调用“c.foo()”会导致执行“foo”的新代码。

一些已知的剩余警告是:

  • 替换代码对象并不总是成功:将类中的 @property 更改为普通方法或将方法更改为成员变量可能会导致问题(但仅限于旧对象)。
  • 在重新加载之前从模块中删除(例如通过猴子修补)的功能不会升级。
  • C 扩展模块无法重新加载,因此无法自动重新加载。*

来源:https://ipython.org/ipython-doc/3/config/extensions/autoreload.html#caveats

另一个不错的选择是在单独的脚本中编写代码并将其发送到 django shell,如下所示:

manage.py shell < my_script.py

Enable IPython autoreload extension before importing any code:

%load_ext autoreload
%autoreload 2

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:

  • Functions and classes imported via ‘from xxx import foo’ are upgraded to new versions when ‘xxx’ is reloaded.
  • Methods and properties of classes are upgraded on reload, so that calling ‘c.foo()’ on an object ‘c’ created before the reload causes the new code for ‘foo’ to be executed.

Some of the known remaining caveats are:

  • Replacing code objects does not always succeed: changing a @property in a class to an ordinary method or a method to a member variable can cause problems (but in old objects only).
  • Functions that are removed (eg. via monkey-patching) from a module before it is reloaded are not upgraded.
  • C extension modules cannot be reloaded, and so cannot be autoreloaded.*

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:

manage.py shell < my_script.py
谁把谁当真 2024-10-03 16:31:04

我编写了一个脚本,它非常适合在 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.

嘿咻 2024-10-03 16:31:04

我发现的最简单的解决方案是 shell_plus 和 iPython 的组合,它会自动重新加载函数。

  1. pip install ipython
  2. pip install django-extensions
  3. 将 'django_extensions' 添加到您的 INSTALLED_APPS
  4. python manage.py shell_plus

The easiest solution I've found is combination of shell_plus and iPython which automatically reloads functions.

  1. pip install ipython
  2. pip install django-extensions
  3. add 'django_extensions' to your INSTALLED_APPS
  4. python manage.py shell_plus
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文