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.
我希望能够在Emacs缓冲区中运行./manage.py shell,其中包含你从ipython获得的所有好东西,比如魔术命令和自动完成。理想情况下,我还希望能够将缓冲区中的代码计算到django shell。
Is this possible?
这可能吗?
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
.
好的,所以我今天自己砍了这个。它的一个主要部分是从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))))
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
这是一个非常古老的问题,但它可能对某人有用。我发现最简单的方法是将以下内容添加到我的.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.
然后,您可以使用任何python-shell-interpreter命令,并且所有内容都将在django shell中运行,而不是使用常规python解释器。
I wrote a blog post about it here.
我在这里写了一篇关于它的博客文章。
1
Using ansi-term
will make ipython's tab-completion work, however note that this will remap all C-x [...]
keybindings to C-c [...]
.
使用ansi-term将使ipython的制表完成工作,但请注意,这会将所有C-x键绑定重新映射到C-c [...]。
If you like it, you can easily create a keybinding for it by putting this to your .emacs:
如果您喜欢它,可以通过将它放到.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.
我只是创建了一个替换ipython shell脚本。
I use python-mode.el and ipython.el; related .emacs.el fragment goes like this:
我使用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")
where smart_ipython script looks like this:
其中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
0
It won't work in shell
? I managed to get a django shell session going in emacs just now.
它不会在shell中工作?我设法刚刚在emacs中进行django shell会话。
Hit M-x shell
and then start your python shell inside that bash shell session, like so:
点击M-x shell,然后在bash shell会话中启动你的python shell,如下所示:
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.
它应该调出一个django shell,就像你在裸机终端中工作一样,但它是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.
至于集成(将代码发送到要评估的shell等),您似乎可以在页面底部找到您正在寻找的内容(Emacs Wiki for python.el)。有很多关于让iPython使用python.el的信息,你可以通过修改那里或python.el中的代码来运行你的django shell。
0
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.
在研究了这个问题之后,我认为每次交换时不会更改配置的多个django项目的最佳解决方案是python-django.el以及目录局部变量的正确配置。
python-django is a great addition to python.el for django users, with many small quality of life improvements, like running commands etc.
python-django是django用户的python.el的一个很好的补充,有许多小的生活质量改进,比如运行命令等。
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.
要使它在项目中始终启动django shell,您需要通过在项目的根目录中创建.dir-locals.el文件来设置正确的目录本地变量。您可以将此配置用于.dir-locals.el文件。关键部分是将python-shell-interpreter args设置为项目的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")))
```
```
The config is taken from this blogpost by the author of the project.
配置来自该项目作者的博客文章。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/09/07/bde9039c9250195474ce704a3fe3fb84.html。