You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Emacs中通过C-c C-c或python-execute-file执行Django自定义命令

在Emacs中执行Django自定义命令的问题

问题现象

尝试通过Emacs的C-c C-c或python-execute-file执行Django自定义命令时,出现以下错误:

-*- mode: compilation; default-directory: "~/src/django/" -*-
Comint started at Fri Feb 17 16:45:33

/home/user/.local/share/virtualenvs/django-OfUvXXat/bin/python csv_import.py
/home/user/.local/share/virtualenvs/django-OfUvXXat/bin/python: can't open file 'csv_import.py': [Errno 2] No such file or directory

Inferior Python exited abnormally with code 2 at Fri Feb 17 16:45:33

但在终端执行以下命令完全正常:

python manage.py csv_import

自定义命令详情

  • 文件路径:~/django/crm/management/commands/csv_import.py
  • 代码内容:
from django.core.management.base import BaseCommand, CommandError
from crm.models import Organization,Person
class Command(BaseCommand):
    def handle(self, *args, **options):
       print(Organization.objects.all())

已尝试的无效配置

(setq python-shell-interpreter "python"
      python-shell-interpreter-args "-i /home/user/src/django/manage.py shell")

(defun django-shell ()
  (interactive)
  (let ((python-shell-interpreter (read-file-name "Locate manage.py "))
        (python-shell-interpreter-args "shell"))
    (run-python (python-shell-calculate-command) nil t)))

环境:LSP、Pyright、Pipenv


解决方法

方法1:绑定自定义执行函数(推荐)

Django自定义命令无法单独运行,必须通过manage.py调用。可以给/management/commands/下的Python文件绑定专属执行函数,替换默认的C-c C-c行为:

(defun run-django-management-command ()
  (interactive)
  (let* ((cmd-filename (file-name-nondirectory buffer-file-name))
         (cmd-name (file-name-sans-extension cmd-filename))
         (manage-dir (locate-dominating-file buffer-file-name "manage.py")))
    (if manage-dir
        (compile (format "%s %s %s"
                         python-shell-interpreter
                         (concat manage-dir "manage.py")
                         cmd-name))
      (error "未找到项目根目录的manage.py"))))

;; 在Python模式下,仅当文件位于management/commands目录时绑定快捷键
(add-hook 'python-mode-hook
          (lambda ()
            (when (string-match-p "/management/commands/" buffer-file-name)
              (local-set-key (kbd "C-c C-c") 'run-django-management-command))))

配置后,打开自定义命令文件按C-c C-c,Emacs会自动找到项目根目录的manage.py并执行python manage.py 命令名。

方法2:直接使用编译模式

通过M-x compile打开编译命令输入框,手动输入:

python manage.py csv_import

Emacs会自动使用当前虚拟环境的Python解释器(若已正确配置Pipenv集成)。

方法3:修复Django Shell配置

如果需要在Shell中调试命令逻辑,调整django-shell函数确保正确加载Django环境:

(defun django-shell ()
  (interactive)
  (let* ((manage-dir (locate-dominating-file default-directory "manage.py"))
         (project-name (file-name-nondirectory (directory-file-name manage-dir)))
         (python-shell-interpreter-args (format "shell --settings=%s.settings" project-name))
         (python-shell-interpreter (concat (getenv "VIRTUAL_ENV") "/bin/python")))
    (run-python (concat python-shell-interpreter " " python-shell-interpreter-args) nil t)))

内容的提问来源于stack exchange,提问作者jjk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 08:56:07