[Fixed]-How to start django shell with ipython in qtconsole mode?

4๐Ÿ‘

โœ…

I created a shell script with the following:

/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.

And I created a python script shell.py in /path/to/my/site with:

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.

๐Ÿ‘คRickG

7๐Ÿ‘

The docs here say:

If youโ€™d rather not use manage.py, no problem. 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).

So it should be enough to set that environment variable and then run ipython qtconsole. You could make a simple script to do this for you automatically.

๐Ÿ‘คThomas K

2๐Ÿ‘

You can check the code that runs the shell here. Youโ€™ll see that there is no where to configure what shell is run.

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. Change it to run the QT console and then you can run manage.py shell_qt.

1๐Ÿ‘

Since Django version 1.4, usage of django.core.management.setup_environ() is deprecated. A solution that works for both the IPython notebook and the QTconsole is this (just execute this from within your Django project directory):

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:

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.

If you want to automate these imports, you can e.g. create an IPython profile by running:

ipython profile create mydjangoproject

Each profile contains a directory called startup. You can put arbitrary Python scripts in there and they will be executed just after IPython has started. 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 ImportErrors. You can then start IPython with the given profile like this:

ipython qtconsole --profile=mydjangoproject

or

ipython notebook --profile=mydjangoproject
๐Ÿ‘คpemistahl

0๐Ÿ‘

I also wanted to open the Django shell in qtconsole. Looking inside manage.py solve the problem for me:
Launch IPython qtconsole, cd to the project base directory and run:

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

Dont forget to change โ€˜myprojectโ€™ to your project name.

๐Ÿ‘คNagasaki45

0๐Ÿ‘

You can create a command that extends the base shell command and imports the IPythonQtConsoleApp like so:

create file qtshell.py in yourapp/management/commands with:

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

๐Ÿ‘คfrmdstryr

0๐Ÿ‘

A somewhat undocumented feature of shell_plus is the ability to run it in โ€œkernel only modeโ€. This allows us to connect to it from another shell, such as one running qtconsole.

For example, in one shell do:

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:

ipython qtconsole --existing kernel-23600.json

This should now open a 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.

๐Ÿ‘คtutuDajuju

-1๐Ÿ‘

You can install django extensions and then run

python manage.py shell_plus --ipython
๐Ÿ‘คfmeurou

Leave a comment