python - python - 如何从Emacs运行Django shell?

我希望能够在Emacs缓冲区中运行./manage.py shell,所有好特性都有,如魔术命令和自动补全,

是否有可能?

时间:

好吧,我今天hack了这个,它的主要部分是来自python-mode.el py-shell的拷贝粘贴 。


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



这是个很老的问题,但可能对某人仍然有用。 我找到了这样做的最简单方法,是将以下内容添加到我的.emacs


(setq python-shell-interpreter"python"


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



然后可以使用任何python-shell-interpreter命令,而且所有的命令都将在Django shell中运行,而不是使用常规的python解释器。

我写了一篇关于它的博客文章这里

使用ansi-term将制作工作的标签,然后注意,这将重新映射所有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)



我只是创建了一个替换的ipython shell script 。

我使用python-mode.el和ipython.el ; 相关的.emacs. el fragment如下所示:

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

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



它在shell中不起作用? 我刚刚在emacs中找到了一个Django shell会话。

点击M-x shell,然后在bash shell会话中启动python shell,如下所示:

 
 M-x shell



 

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中的另一个缓冲区。

...