【问题标题】:How to run django shell from Emacs?如何从 Emacs 运行 django shell?
【发布时间】:2009-09-07 15:16:28
【问题描述】:

我希望能够在 Emacs 缓冲区中运行 ./manage.py shell,其中包含您从 ipython 获得的所有好东西,例如魔术命令和自动完成。理想情况下,我还希望能够评估从缓冲区到 django shell 的代码。

这可能吗?

【问题讨论】:

    标签: python django emacs ipython


    【解决方案1】:

    好的,所以我今天自己破解了这个。它的主要部分是从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))))
    

    【讨论】:

      【解决方案2】:

      这是一个相当古老的问题,但它可能对某些人仍然有用。我发现最简单的方法是将以下内容添加到我的 .emacs

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

      然后您可以使用任何 python-shell-interpreter 命令,一切都将在 django shell 中运行,而不是使用常规的 python 解释器。

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我在启动 ipython 缓冲区时没有问题,我想要一个 manage.py shell 缓冲区。 ansi-term 不允许您将参数传递给它执行的程序。
        【解决方案4】:

        我只是创建了一个替换 ipython shell 脚本。

        我使用 python-mode.el 和 ipython.el;相关的 .emacs.el 片段如下:

        (setq ipython 命令“/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
        

        【讨论】:

          【解决方案5】:

          在研究了这个问题之后,我认为适用于多个 django 项目的最佳解决方案是python-django.el 加上正确的目录局部变量配置

          python-django 是对 django 用户的 python.el 的一个很好的补充,具有许多小的生活质量改进,例如运行命令等。

          要让它在项目中始终启动 django shell,您需要设置正确的目录局部变量,方法是在项目的根目录中创建 .dir-locals.el 文件。您可以将此配置用于.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")))
          

          ```

          该配置由项目作者from this blogpost 获取。

          【讨论】:

          • 直到我写完我的答案后才注意到你的答案。这种模式真的很酷。
          【解决方案6】:

          它在shell 中不起作用?我刚刚设法在 emacs 中进行了 django shell 会话。

          点击M-x shell,然后在该 bash shell 会话中启动您的 python 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 shell,就像你在一个裸终端中工作一样,但它是 Emacs 中的另一个缓冲区。

          就集成(将代码发送到要评估的外壳等)而言,您似乎可以在页面底部here (Emacs Wiki for python.el) 找到您要查找的内容。那里有很多关于让 iPython 与 python.el 一起工作的信息,你可以通过修改那里或 python.el 中的代码来运行你的 django shell。

          【讨论】:

            【解决方案7】:

            这是旧的,但希望这会对某人有所帮助。

            在 Emacs 下运行 Django 开发的最新且最轻松的方法是使用 Django-Emacs 模式。

            可以在这里找到。

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

            看起来像这样:

            安装此模式后,您会在 emacs 中获得一个名为“Django”的菜单条目。正如您在上面的屏幕截图中看到的那样,您可以在菜单中或键盘快捷键中想象到所有命令。 Django Shell 就在那里,以及所有其他东西。

            这是一个非常酷的模式。

            附:对于这种模式,我不是开发人员,也不是感兴趣的一方。我是一个快乐的用户。

            编辑:

            .dir-local.el 文件的格式似乎略有变化。我目前正在使用 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"))))
            

            【讨论】:

              猜你喜欢
              • 2023-03-31
              • 2010-09-19
              • 1970-01-01
              • 2011-03-03
              • 1970-01-01
              • 1970-01-01
              • 2013-02-14
              • 1970-01-01
              相关资源
              最近更新 更多