views:

591

answers:

4

I'd like to be able to run ./manage.py shell in an Emacs buffer, with all the nice stuff that you get from ipython, like magic commands and autocompletion. Ideally I would also like to be able evaluate code from a buffer to the django shell.

Is this possible?

A: 

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.

Pinochle
+1  A: 

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)
m01
I don't have trouble starting an ipython buffer, I want a `manage.py shell` buffer. ansi-term doesn't allow you to pass arguments to the program it executes.
Ryszard Szopa
+4  A: 

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))))
Ryszard Szopa
+1  A: 

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
Maciej Pasternacki