简体   繁体   English

如何在qtconsole模式下使用ipython启动django shell?

[英]how to start django shell with ipython in qtconsole mode?

When i start django shell by typing python manage.py shell the ipython shell is started. 当我通过键入python manage.py shell启动django shell时,启动ipython shell。 Is it possible to make Django start ipython in qtconsole mode? 是否有可能让Django在qtconsole模式下启动ipython? (ie make it run ipython qtconsole ) (即让它运行ipython qtconsole

Arek Arek

edit: so I'm trying what Andrew Wilkinson suggested in his answer - extending my django app with a command which is based on original django shell command. 编辑:所以我正在尝试Andrew Wilkinson在他的回答中提出的建议 - 使用基于原始django shell命令的命令扩展我的django应用程序。 As far as I understand code which starts ipython in original version is this: 据我所知,在原始版本中启动ipython的代码是这样的:

from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    requires_model_validation = False

    def handle_noargs(self, **options):
        from IPython.frontend.terminal.embed import TerminalInteractiveShell
        shell = TerminalInteractiveShell()
        shell.mainloop()

any advice how to change this code to start ipython in qtconsole mode? 任何建议如何更改此代码以在qtconsole模式下启动ipython?

second edit: what i found and works so far is - start 'ipython qtconsole' from the location where settings.py of my project is (or set the sys.path if starting from different location), and then execute this: 第二次编辑:到目前为止我发现和工作的是 - 从我的项目的settings.py所在的位置开始'ipython qtconsole'(或者如果从不同的位置开始设置sys.path),然后执行:

import settings
import django.core.management
django.core.management.setup_environ(settings)

and now can i import my models, list all instances etc. 现在我可以导入我的模型,列出所有实例等。

The docs here say: 这里的文档说:

If you'd rather not use manage.py, no problem. 如果你不想使用manage.py,没问题。 Just set the DJANGO_SETTINGS_MODULE environment variable to mysite.settings and run python from the same directory manage.py is in (or ensure that directory is on the Python path, so that import mysite works). 只需将DJANGO_SETTINGS_MODULE环境变量设置为mysite.settings并从manage.py所在的同一目录运行python(或确保该目录位于Python路径上,以便导入mysite工作)。

So it should be enough to set that environment variable and then run ipython qtconsole . 所以它应该足以设置该环境变量然后运行ipython qtconsole You could make a simple script to do this for you automatically. 您可以创建一个简单的脚本来自动执行此操作。

I created a shell script with the following: 我用以下内容创建了一个shell脚本:

/path/to/ipython
qtconsole --pylab inline -c "run /path/to/my/site/shell.py"

You only need the --pylab inline part if you want the cool inline matplotlib graphs. 如果你想要酷的内联matplotlib图,你只需要--pylab inline部分。

And I created a python script shell.py in /path/to/my/site with: 我在/path/to/my/site创建了一个python脚本shell.py

import os
working_dir = os.path.dirname(__file__)
os.chdir(working_dir)
import settings
import django.core.management
django.core.management.setup_environ(settings)

Running my shell script gets me an ipython qtconsole with the benefits of the django shell. 运行我的shell脚本获得了一个ipython qtconsole,它具有django shell的优点。

You can check the code that runs the shell here . 您可以在此处检查运行shell的代码。 You'll see that there is no where to configure what shell is run. 您将看到没有配置shell运行的位置。

What you could do is copy this file, rename it as shell_qt.py and place it in your own project's management/commands directory. 您可以做的是复制此文件,将其重命名为shell_qt.py并将其放在您自己项目的管理/命令目录中。 Change it to run the QT console and then you can run manage.py shell_qt . 更改它以运行QT控制台,然后您可以运行manage.py shell_qt

Since Django version 1.4, usage of django.core.management.setup_environ() is deprecated. 自Django 1.4版以来,不推荐使用django.core.management.setup_environ() A solution that works for both the IPython notebook and the QTconsole is this (just execute this from within your Django project directory): 适用于IPython笔记本和QTconsole的解决方案就是这样(只需在Django项目目录中执行此操作):

In [1]: from django.conf import settings

In [2]: from mydjangoproject.settings import DATABASES as MYDATABASES

In [3]: settings.configure(DATABASES=MYDATABASES)

Update: If you work with Django 1.7, you additionally need to execute the following: 更新:如果您使用Django 1.7,您还需要执行以下操作:

In [4]: import django; django.setup()

Using django.conf.settings.configure() , you specify the database settings of your project and then you can access all your models in the usual way. 使用django.conf.settings.configure() ,您可以指定项目的数据库设置,然后您可以按常规方式访问所有模型。

If you want to automate these imports, you can eg create an IPython profile by running: 如果要自动执行这些导入,可以通过运行以下命令创建IPython配置文件:

ipython profile create mydjangoproject

Each profile contains a directory called startup . 每个配置文件包含一个名为startup的目录 You can put arbitrary Python scripts in there and they will be executed just after IPython has started. 您可以在其中放置任意Python脚本,它们将在IPython启动后立即执行。 In this example, you find it under 在此示例中,您可以找到它

~/.ipython/profile_<mydjangoproject>/startup/

Just put a script in there which contains the code shown above, probably enclosed by a try..except clause to handle ImportError s. 只需在其中放置一个脚本,其中包含上面显示的代码,可能包含在try..except子句中以处理ImportError You can then start IPython with the given profile like this: 然后,您可以使用给定的配置文件启动IPython,如下所示:

ipython qtconsole --profile=mydjangoproject

or 要么

ipython notebook --profile=mydjangoproject

I also wanted to open the Django shell in qtconsole. 我还想在qtconsole中打开Django shell。 Looking inside manage.py solve the problem for me: Launch IPython qtconsole, cd to the project base directory and run: 在manage.py里面找我解决问题:启动IPython qtconsole,cd到项目基目录并运行:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

Dont forget to change 'myproject' to your project name. 别忘了将'myproject'更改为您的项目名称。

You can create a command that extends the base shell command and imports the IPythonQtConsoleApp like so: 您可以创建一个扩展基本shell命令的命令,并像这样导入IPythonQtConsoleApp:

create file qtshell.py in yourapp/management/commands with: 使用以下命令在yourapp / management /命令中创建文件qtshell.py:

from django.core.management.commands import shell

class Command(shell.Command):
    def _ipython(self):
        """Start IPython Qt console"""
        from IPython.qt.console.qtconsoleapp import IPythonQtConsoleApp
        app = IPythonQtConsoleApp.instance()
        app.initialize(argv=[])
        app.start()

then just use python manage.py qtshell 然后只需使用python manage.py qtshell

A somewhat undocumented feature of shell_plus is the ability to run it in "kernel only mode". shell_plus一个有点shell_plus记录的功能是能够在“仅内核模式”下运行它。 This allows us to connect to it from another shell, such as one running qtconsole. 这允许我们从另一个shell连接到它,例如运行qtconsole的shell。

For example, in one shell do: 例如,在一个shell中执行:

django-admin shell_plus --kernel
# or == ./manage.py shell_plus --kernel

This will print out something like: 这将打印出如下内容:

 # Shell Plus imports ... ... To connect another client to this kernel, use: --existing kernel-23600.json 

Then, in another shell run: 然后,在另一个shell运行:

ipython qtconsole --existing kernel-23600.json

This should now open a QtConsole. 现在应该打开一个QtConsole。 One other tip, instead of running another shell, you can also hit Ctrl+Z , and run bg to tell current process to run in background. 另一个提示,而不是运行另一个shell,你也可以按Ctrl + Z ,然后运行bg告诉当前进程在后台运行。

您可以安装django扩展然后运行

python manage.py shell_plus --ipython

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM