2009-09-07 60 views
10

我希望能夠在Emacs緩衝區中運行./manage.py shell,其中包含從ipython獲得的所有好東西,如魔術命令和自動完成。理想情況下,我也希望能夠將代碼從緩衝區評估到django shell。如何從Emacs運行django shell?

這可能嗎?

回答

0

它不會在shell工作?我設法剛剛獲得了一個django shell會話進入emacs。

命中M-x shell,然後啓動的bash shell會話內你的Python殼,就像這樣:

M-x shell 

殼產卵

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) 
>>> 

,它應該彈出一個Django的外殼,就好像你在裸機上工作,但它是Emacs中的另一個緩衝區。至於集成(發送代碼到被評估的shell等),似乎你可能能夠在頁面底部找到你想要找的東西。here (Emacs Wiki for python.el)。關於讓iPython與python.el一起工作有很多信息,您可以通過修改代碼或python.el中的代碼來運行django shell。

1

使用ansi-term將使ipython的製表符完成工作,但請注意,這將重新映射所有C-x [...]鍵綁定到C-c [...]

如果你喜歡它,你可以輕鬆地在這對你的.emacs爲它創建一個鍵綁定:

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

我沒有麻煩啓動一個ipython緩衝區,我想要一個'manage.py shell'緩衝區。 ansi-term不允許你傳遞參數給它執行的程序。 – 2009-09-08 07:40:26

8

好了,我今天自己砍死這一點。它的主要部分是從py-shellpython-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)))) 
1

我沒有簡單地創建一個替代ipython shell腳本。

我使用python-mode.el和ipython.el;相關.emacs.el片段是這樣的:

 
(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") 

其中smart_ipython腳本是這樣的:

#!/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 
2

這是一個非常古老的問題,但它可能仍然是有用的人。我發現這樣做的最簡單的方法是通過添加以下到我的.emacs

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

然後,您可以使用任何蟒蛇殼解釋命令的,一切都將在Django的shell中運行,而不是與常規的Python解釋器。

我寫了一篇關於它的博客文章here

0

研究這個問題後,我認爲這會爲多個Django項目工作,而無需每次交換時改變你的配置最好的解決辦法是python-django.el目錄局部變量的配置是否正確。

蟒蛇,Django是一個偉大的除了python.el Django的用戶,隨着生活的改善許多小的質量,比如運行命令等

爲了得到它總是推出Django的外殼在一個項目的時候,您需要通過在項目的根目錄中創建.dir-locals.el文件來設置正確的目錄本地變量。您可以將此配置用於.dir-locals.el文件。關鍵部分是爲您的項目設置您的python-shell解釋器參數爲manage.py shell

((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"))) 

```

的配置是由該項目的作者採取from this blogpost