在 iPython 和 Django 中使用配置文件

发布于 2024-07-25 20:46:44 字数 255 浏览 8 评论 0原文

我有一个 Django 产品,我正在使用 iPython 与之交互。

我试图在启动 shell 时自动加载模块:

python manage.py shell

我已将 .ipython/ipythonrc 复制到项目的根目录并添加到文件中:

import_some module_name model1 model2

但是,当我启动时shell 中,这些名称未被加载。

我究竟做错了什么?

I've got a Django product I'm using iPython to interact with.

I'm trying to have modules automatically loaded when I start a shell:

python manage.py shell

I've copied .ipython/ipythonrc to the root directory of the project and added to the file:

import_some module_name model1 model2

However, when I start the shell, these names are not being loaded.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

三寸金莲 2024-08-01 20:46:44

我不知道 ipythonrc,但如果您只需要模型,您可以使用 <代码>django-extensions。 安装后,您将获得大量新的管理命令,包括 shell_plus,它将打开 ipython 会话并自动加载所有模型:

python manage.py shell_plus

I don't know about ipythonrc, but if you only need the models, you could use django-extensions. After you install it, you've got a plethora of new managment commands, including shell_plus, which will open a ipython session and autoload all your models:

python manage.py shell_plus
海风掠过北极光 2024-08-01 20:46:44

BryanWheelock 您的解决方案将不起作用,因为您的 shell 是生成的结果,而不是与其直接交互的结果。 你想做的就是这个——或者至少这是我所做的。

在您的工作空间(您输入 python manage.py shell 的位置)中创建一个 ipythonrc 文件。 在其中添加以下内容:

include ~/.ipython/ipythonrc

execute from django.contrib.auth.models import User
# .
# .
# .
execute import_some module_name model1 model2

例如,我还在我的中添加了以下行。

# Setup Logging
execute import sys
execute import logging
execute loglevel = logging.DEBUG
execute logging.basicConfig(format="%(levelname)-8s %(asctime)s %(name)s %(message)s", datefmt='%m/%d/%y %H:%M:%S', stream=sys.stdout )
execute log = logging.getLogger("")
execute log.setLevel(loglevel)
execute log.debug("Logging has been initialized from ipythonrc")
execute log.debug("Root Logger has been established - use \"log.LEVEL(MSG)\"")
execute log.setLevel(loglevel)
execute log.debug("log.setlevel(logging.DEBUG)")
execute print ""

这允许您在模块中使用日志记录并保持干燥。 希望这可以帮助。

BryanWheelock Your solution won't work because your shell is the result of the spawn not a direct interatction with it. What you want to do is this - or at least this is what I do.

Within your workspace (the place where you type python manage.py shell) create a ipythonrc file. In it put the following:

include ~/.ipython/ipythonrc

execute from django.contrib.auth.models import User
# .
# .
# .
execute import_some module_name model1 model2

For example I also add the following lines in mine..

# Setup Logging
execute import sys
execute import logging
execute loglevel = logging.DEBUG
execute logging.basicConfig(format="%(levelname)-8s %(asctime)s %(name)s %(message)s", datefmt='%m/%d/%y %H:%M:%S', stream=sys.stdout )
execute log = logging.getLogger("")
execute log.setLevel(loglevel)
execute log.debug("Logging has been initialized from ipythonrc")
execute log.debug("Root Logger has been established - use \"log.LEVEL(MSG)\"")
execute log.setLevel(loglevel)
execute log.debug("log.setlevel(logging.DEBUG)")
execute print ""

This allows you to use logging in your modules and keep it DRY. Hope this helps.

可是我不能没有你 2024-08-01 20:46:44

django-extensions的shell_plus命令可以自动导入模型,但似乎无法加载ipython的配置文件。 我做了一些黑客工作来完成这项工作。

使用 start_ipython 启动 ipython shell 而不是 embed 并向其传递一些参数。

我还写了一篇博文,你可以找到详细信息 这里

shell_plus command of django-extensions can import the model automatically, but it seems can not load the profile of ipython. I have did some hacky job to make this done.

use start_ipython to launch ipython shell instead of embed and pass some arguments to it.

I have also wrote a blog post, you can find the detail here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文