在 iPython 和 Django 中使用配置文件
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道 ipythonrc,但如果您只需要模型,您可以使用 <代码>django-extensions。 安装后,您将获得大量新的管理命令,包括 shell_plus,它将打开 ipython 会话并自动加载所有模型:
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, includingshell_plus
, which will open a ipython session and autoload all your models:BryanWheelock 您的解决方案将不起作用,因为您的 shell 是生成的结果,而不是与其直接交互的结果。 你想做的就是这个——或者至少这是我所做的。
在您的工作空间(您输入 python manage.py shell 的位置)中创建一个 ipythonrc 文件。 在其中添加以下内容:
例如,我还在我的中添加了以下行。
这允许您在模块中使用日志记录并保持干燥。 希望这可以帮助。
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:For example I also add the following lines in mine..
This allows you to use logging in your modules and keep it DRY. Hope this helps.
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 ofembed
and pass some arguments to it.I have also wrote a blog post, you can find the detail here