./manage.py shell
魔法のコマンドやオートコンプリートなど、ipython から得られるすべての優れた機能を使用して、Emacs バッファーで実行できるようにしたいと考えています。理想的には、バッファから django シェルまでのコードを評価できるようにしたいと考えています。
これは可能ですか?
OK、今日は自分でこれをハッキングしました。その大部分は frompy-shell
からのコピー アンド ペースト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))))
これはかなり古い質問ですが、おそらく誰かにとってはまだ役に立ちます。これを行う最も簡単な方法は、.emacs に以下を追加することです。
(setq python-shell-interpreter "python"
python-shell-interpreter-args "-i /absolute/path/to/manage.py shell_plus")
その後、任意の python-shell-interpreter コマンドを使用できます。通常の python インタープリターではなく、すべてが django シェルで実行されます。
私はそれについてのブログ投稿をここに書きました。
代わりの ipython シェル スクリプトを作成しただけです。
私は python-mode.el と ipython.el を使用します。関連する .emacs.el フラグメントは次のようになります。
(setq ipython-command "/Users/japhy/bin/smart_ipython") ('ipython が必要) ;; 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
を使用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)
この質問を調査した後、スワップするたびに構成を変更せずに複数のdjangoプロジェクトで機能する最良の解決策は、ディレクトリローカル変数python-django.el
の正しい構成に加えることだと思います。
python-django は django ユーザー向けの python.el への素晴らしい追加であり、コマンドの実行など、多くの小さな生活の質の向上をもたらします。
.dir-locals.el
プロジェクトで常にdjangoシェルを起動するには、プロジェクトのルートにファイルを作成して、適切なディレクトリローカル変数を設定する必要があります。この構成を.dir-locals.el
ファイルに使用できます。重要な部分は、プロジェクトの python-shell-interpreter 引数を に設定すること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")))
```
構成は、プロジェクトの作成者によってこのブログ投稿から取得されます。
では動作しshell
ませんか? ちょうど今、emacs で django シェル セッションを開始することができました。
次のように、その bash シェル セッション内で python シェルをヒットM-x shell
して開始します。
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 の別のバッファーです。
統合(評価されるシェルにコードを送信するなど)に関する限り、ここのページの下部で探しているものを見つけることができるようです(Emacs Wiki for python.el)。iPython を python.el で動作させるための情報がたくさんあります。そこまたは python.el のコードを変更することで、django シェルを実行するためにそれを取得できる場合があります。