简体   繁体   English

如何从Emacs运行django shell?

[英]How to run django shell from Emacs?

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. 我希望能够在Emacs缓冲区中运行./manage.py shell ,其中包含你从ipython获得的所有好东西,比如魔术命令和自动完成。 Ideally I would also like to be able evaluate code from a buffer to the django shell. 理想情况下,我还希望能够将缓冲区中的代码计算到django shell。

Is this possible? 这可能吗?

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

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 . 在这里写了一篇关于它的博客文章。

Using ansi-term will make ipython's tab-completion work, however note that this will remap all Cx [...] keybindings to Cc [...] . 使用ansi-term将使ipython的制表完成工作,但请注意,这会将所有Cx [...]键绑定重新映射到Cc [...]

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)

I did simply create a replacement ipython shell script. 我只是创建了一个替换ipython shell脚本。

I use python-mode.el and ipython.el; 我使用python-mode.el和ipython.el; related .emacs.el fragment goes like this: 相关的.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

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. 要使它在项目中始终启动django shell,您需要通过在项目的根目录中创建.dir-locals.el文件来设置正确的目录本地变量。 You can use this config for the .dir-locals.el file. 您可以将此配置用于.dir-locals.el文件。 The critical part is setting your python-shell-interpreter args to manage.py shell for your project. 关键部分是将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. 该项目的作者从该博客帖子中获取配置。

It won't work in shell ? 它不适用于shell I managed to get a django shell session going in emacs just now. 我设法刚刚在emacs中进行了django shell会话。

Hit Mx shell and then start your python shell inside that bash shell session, like so: 点击Mx 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) . 至于集成(将代码发送到要评估的shell等),您似乎可以在页面底部找到您要查找的内容(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. 有很多关于让iPython使用python.el的信息,你可以通过修改那里或python.el中的代码来运行你的django shell。

This is old, but hopefully this will help someone. 这是旧的,但希望这将有助于某人。

The most up-to-date and effortless way to run Django development under Emacs is by using Django-Emacs mode. 在Emacs下运行Django开发的最新和最轻松的方法是使用Django-Emacs模式。

It can be found here. 在这里能找到它。

https://github.com/fgallina/python-django.el https://github.com/fgallina/python-django.el

It looks something like this: 它看起来像这样:

在此输入图像描述

When you have this mode installed, you get a menu entry titled "Django" in your emacs. 安装此模式后,您的emacs中会出现一个名为“Django”的菜单项。 As you can see on the screenshot above, you have all the commands you can imagine in the menu or as keyboard shortcuts. 正如您在上面的屏幕截图中看到的那样,您可以在菜单中看到所有可以想象的命令或键盘快捷键。 The Django Shell is there, alongside all the other things. 除了所有其他的东西,Django Shell就在那里。

It is a really cool mode. 这是一个非常酷的模式。

PS I am not a developer nor an interested party when it comes to this mode. PS对于这种模式,我不是开发者也不是感兴趣的一方。 I am a happy user. 我是一个快乐的用户。

Edit: 编辑:

It appears that the format for .dir-local.el file has changed slightly. 似乎.dir-local.el文件的格式稍有变化。 I am currently using Emacs 25.2.2 and the following format works for this task 我目前正在使用Emacs 25.2.2,以下格式适用于此任务

((python-mode . ((python-shell-interpreter . "/home/user/miniconda3/envs/xxx-web/bin/python")
         (python-shell-interpreter-args . "/var/www/sites/xxx-web/xxx/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-string-code . "';'.join(module_completion('''%s'''))\n")
         (python-shell-completion-string-code . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
         (python-shell-virtualenv-root . "/home/user/miniconda3/envs/xxx-web"))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM