[Solved]-How to run django shell from Emacs?

8👍

OK, so I hacked this by myself today. A major part of it is copy-and-paste from py-shell from python-mode.el.

(defun django-shell (&optional argprompt)
  (interactive "P")
  ;; Set the default shell if not already set
  (labels ((read-django-project-dir 
    (prompt dir)
    (let* ((dir (read-directory-name prompt dir))
           (manage (expand-file-name (concat dir "manage.py"))))
      (if (file-exists-p manage)
          (expand-file-name dir)
        (progn
          (message "%s is not a Django project directory" manage)
          (sleep-for .5)
          (read-django-project-dir prompt dir))))))
(let* ((dir (read-django-project-dir 
         "project directory: " 
         default-directory))
       (project-name (first 
              (remove-if (lambda (s) (or (string= "src" s) (string= "" s))) 
                 (reverse (split-string dir "/")))))
       (buffer-name (format "django-%s" project-name))
       (manage (concat dir "manage.py")))
  (cd dir)
  (if (not (equal (buffer-name) buffer-name))
      (switch-to-buffer-other-window
       (apply 'make-comint buffer-name manage nil '("shell")))
    (apply 'make-comint buffer-name manage nil '("shell")))
  (make-local-variable 'comint-prompt-regexp)
  (setq comint-prompt-regexp (concat py-shell-input-prompt-1-regexp "\\|"
                     py-shell-input-prompt-2-regexp "\\|"
                     "^([Pp]db) "))
  (add-hook 'comint-output-filter-functions
        'py-comint-output-filter-function)
  ;; pdbtrack

  (add-hook 'comint-output-filter-functions 'py-pdbtrack-track-stack-file)
  (setq py-pdbtrack-do-tracking-p t)
  (set-syntax-table py-mode-syntax-table)
  (use-local-map py-shell-map)
  (run-hooks 'py-shell-hook))))

2👍

This is a pretty old question, but it’s probably still useful for someone. I’ve found the easiest way of doing this is by adding the following to my .emacs

(setq python-shell-interpreter "python"
      python-shell-interpreter-args "-i /absolute/path/to/manage.py shell_plus")

You can then use any of the python-shell-interpreter commands, and everything will run in the django shell instead of with the regular python interpreter.

I wrote a blog post about it here.

👤Farid

1👍

Using ansi-term will make ipython’s tab-completion work, however note that this will remap all C-x [...] keybindings to C-c [...].

If you like it, you can easily create a keybinding for it by putting this to your .emacs:

(defun start-my-ipython-term ()
  (interactive)
  (ansi-term "/usr/bin/ipython"))
(global-set-key (kbd "<your keybinding here>") 'start-my-ipython-term)

1👍

I did simply create a replacement ipython shell script.

I use python-mode.el and ipython.el; related .emacs.el fragment goes like this:

(setq ipython-command "/Users/japhy/bin/smart_ipython")
(require 'ipython)

;; fix completion for ipython 0.10
(setq ipython-completion-command-string
      "print(';'.join(__IP.Completer.all_completions('%s'))) #PYTHON-MODE SILENT\n")

where smart_ipython script looks like this:

#!/bin/sh
set -e

/bin/echo -n "Select Django project/dir, or press enter for plain ipython: "

read selection
case $selection in
    '') exec ipython ;;
    project) cd /Users/japhy/Projekty/some/project/dir ;;
    # other often used projects go here
    *) cd $selection ;;
esac
exec python manage.py shell

1👍

After researching this question I think the best solution that will work for multiple django projects without changing your config each time you swap is python-django.el plus correct configuration of directory-local variables.

python-django is a great addition to python.el for django users, with many small quality of life improvements, like running commands etc.

To get it to always launch the django shell when in a project, you need to set the proper directory-local variables, by creating a .dir-locals.el file in the root of your project. You can use this config for the .dir-locals.el file. The critical part is setting your python-shell-interpreter args to manage.py shell for your project.

((python-mode
  (python-shell-interpreter . "python")
  (python-shell-interpreter-args . "/home/youruser/code/yourproject/manage.py shell")
  (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
  (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
  (python-shell-completion-setup-code . "from IPython.core.completerlib import module_completion")
  (python-shell-completion-module-string-code . "';'.join(module_completion('''%s'''))\n")
  (python-shell-completion-string-code . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
  (python-shell-extra-pythonpaths "/home/youruser/code/yourproject/apps/")
  (python-shell-virtualenv-path . "/home/youruser/.virtualenvs/yourproject")))

“`

The config is taken from this blogpost by the author of the project.

0👍

It won’t work in shell? I managed to get a django shell session going in emacs just now.

Hit M-x shell and then start your python shell inside that bash shell session, like so:

 M-x shell

the shell spawns

prompt> cd path/to/my/django/directory
prompt> python manage.py shell
Python 2.6.1 (r261:67515, Jul  7 2009, 23:51:51) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>

and it should bring up a django shell just as if you are working in a bare terminal, but it is another buffer in Emacs.

As far as the integration (sending code to the shell to be evaluated, etc) it seems like you may be able find what you’re looking for at the bottom of the page here (Emacs Wiki for python.el). There’s plenty of info there about getting iPython working with python.el, and you may be able to get that to run your django shell by modifying the code there or in python.el.

0👍

This is old, but hopefully this will help someone.

The most up-to-date and effortless way to run Django development under Emacs is by using Django-Emacs mode.

It can be found here.

https://github.com/fgallina/python-django.el

It looks something like this:

enter image description here

When you have this mode installed, you get a menu entry titled “Django” in your emacs. As you can see on the screenshot above, you have all the commands you can imagine in the menu or as keyboard shortcuts. The Django Shell is there, alongside all the other things.

It is a really cool mode.

P.S. I am not a developer nor an interested party when it comes to this mode. I am a happy user.

Edit:

It appears that the format for .dir-local.el file has changed slightly. I am currently using Emacs 25.2.2 and the following format works for this task

((python-mode . ((python-shell-interpreter . "/home/user/miniconda3/envs/xxx-web/bin/python")
         (python-shell-interpreter-args . "/var/www/sites/xxx-web/xxx/manage.py shell")
         (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
         (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
         (python-shell-completion-setup-code . "from IPython.core.completerlib import module_completion")
         (python-shell-completion-string-code . "';'.join(module_completion('''%s'''))\n")
         (python-shell-completion-string-code . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
         (python-shell-virtualenv-root . "/home/user/miniconda3/envs/xxx-web"))))

Leave a comment