如何在 django shell 中重新加载模块?
我正在使用 Django 并一直使用 Django shell。烦人的部分是,虽然 Django 服务器会在代码更改时重新加载,但 shell 不会,因此每次我对正在测试的方法进行更改时,我都需要退出 shell 并重新启动它,重新导入我的所有模块需要,重新初始化我需要的所有变量等等。虽然 iPython 的历史节省了很多打字时间,但这仍然是一个痛苦。有没有办法让 django shell 自动重新加载,就像 django 开发服务器一样?
我了解 reload(),但我导入了很多模型,并且通常使用 from app.models import *
语法,因此 reload() 没有太大帮助。
I am working with Django and use Django shell all the time. The annoying part is that while the Django server reloads on code changes, the shell does not, so every time I make a change to a method I am testing, I need to quit the shell and restart it, re-import all the modules I need, reinitialize all the variables I need etc. While iPython history saves a lot of typing on this, this is still a pain. Is there a way to make django shell auto-reload, the same way django development server does?
I know about reload(), but I import a lot of models and generally use from app.models import *
syntax, so reload() is not much help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我建议使用 IPython 自动重新加载扩展。
从现在开始,所有导入的模块都将在评估之前刷新。
如果在
%load_ext autoreload
命令之前导入了某些内容,也可以使用。还可以使用
%aimport
命令和 3 个自动重新加载策略来阻止某些导入刷新:这通常适合我的使用,但有一些注意事项:
I'd suggest use IPython autoreload extension.
And from now all imported modules would be refreshed before evaluate.
Works also if something was imported before
%load_ext autoreload
command.There is possible also prevent some imports from refreshing with
%aimport
command and 3 autoreload strategies:This generally works good for my use, but there are some cavetas:
我的解决方案是编写代码并保存到文件中,然后使用:
所以我可以进行更改,保存并再次运行该命令,直到修复我想要修复的任何问题。
My solution to it is I write the code and save to a file and then use:
So I can make the change, save and run that command again till I fix whatever I'm trying to fix.
我建议使用 django-extensions 项目,如 dongweiming 上面所述。但不只是使用“shell_plus”管理命令,而是使用:
这将在您的 Web 浏览器上打开 IPython 笔记本。在单元格中编写代码、导入等并运行它。
当您更改模块时,只需单击笔记本菜单项“内核->重新启动”
即可,您的代码现在正在使用修改后的模块。
I recommend using the django-extensions project like stated above by dongweiming. But instead of just 'shell_plus' management command, use:
This will open a IPython notebook on your web browser. Write your code there in a cell, your imports etc. and run it.
When you change your modules, just click the notebook menu item 'Kernel->Restart'
There you go, your code is now using your modified modules.
查看django-extensions项目提供的manage.py shell_plus命令。它将在 shell 启动时加载所有模型文件。并自动重新加载您的任何修改但不需要退出,您可以直接在那里调用
look at the manage.py shell_plus command provided by the django-extensions project. It will load all your model files on shell startup. and autoreload your any modify but do not need exit, you can direct call there
似乎关于这个主题的普遍共识是 python reload() 很糟糕,并且没有好的方法来做到这一点。
It seems that the general consensus on this topic, is that python reload() sucks and there is no good way to do this.
将 shell_plus 与 ipython 配置一起使用。这将在 shell_plus 自动导入任何内容之前启用
autoreload
。编辑您的 ipython 配置文件 (
~/.ipython/profile_default/ipython_config.py
):打开 shell - 请注意,您不需要包含
--ipython
:现在定义任何内容在
SHELL_PLUS_PRE_IMPORTS
或SHELL_PLUS_POST_IMPORTS
(docs) 将自动重新加载!请注意,如果您的 shell 位于调试器(例如
pdb.set_trace()
)处,那么当您保存文件时,它可能会干扰重新加载。Use shell_plus with an ipython config. This will enable
autoreload
before shell_plus automatically imports anything.Edit your ipython profile (
~/.ipython/profile_default/ipython_config.py
):Open a shell - note that you do not need to include
--ipython
:Now anything defined in
SHELL_PLUS_PRE_IMPORTS
orSHELL_PLUS_POST_IMPORTS
(docs) will autoreload!Note that if your shell is at a debugger (ex
pdb.set_trace()
) when you save a file it can interfere with the reload.针对这种不便,我的解决方案如下。我正在使用 IPython。
对于Python 3.x,必须使用以下方式导入“reload”:
希望有帮助。当然这是为了调试目的。
干杯。
My solution for this inconvenient follows. I am using IPython.
For Python 3.x, 'reload' must be imported using:
Hope it helps. Of course it is for debug purposes.
Cheers.
如果没有一些技巧,Reload() 无法在 Django shell 中工作。您可以具体检查此线程 na 和我的答案:
如何通过“manage.py shell”使用交互式解释器重新加载 Django 模型模块?
Reload() doesn't work in Django shell without some tricks. You can check this thread na and my answer specifically:
How do you reload a Django model module using the interactive interpreter via "manage.py shell"?
为此,我结合了两个答案,想出了一种简单的单行方法。
您可以使用 -c 运行 django shell,它将运行您传递的命令,但它会在代码运行后立即退出。
诀窍是设置您需要的内容,运行 code.interact(local=locals()),然后从您传递的代码中重新启动 shell。像这样:
对我来说,我只想要丰富的库的检查方法。只有几行:
最后,最重要的是一个别名
现在,如果我启动 shell 并说,想要检查表单类,我会得到这个漂亮的输出:
Using a combination of 2 answers for this I came up with a simple one line approach.
You can run the django shell with -c which will run the commands you pass however it quits immediately after the code is run.
The trick is to setup what you need, run code.interact(local=locals()) and then re-start the shell from within the code you pass. Like this:
For me I just wanted the rich library's inspect method. Only a few lines:
Finally the cherry on top is an alias
Now if I startup my shell and say, want to inspect the form class I get this beautiful output:
不完全是你想要的,但我现在倾向于构建自己的管理命令来测试和摆弄事物。
在命令中,您可以按照您想要的方式设置一堆本地变量,然后放入交互式 shell 中。
无需重新加载,而是一种简单且不那么烦人的交互式测试 django 功能的方法。
Not exactly what you want, but I now tend to build myself management commands for testing and fiddling with things.
In the command you can set up a bunch of locals the way you want and afterwards drop into an interactive shell.
No reload, but an easy and less annoying way to interactively test django functionality.
您可以设置管理命令 ,而不是从 Django shell 运行命令像这样并每次重新运行。
Instead of running commands from the Django shell, you can set up a management command like so and rerun that each time.
解决方案使用从 importlib 重新加载,如下
solution Use reload from importlib as follows