python - dir-locals.el 设置未应用于某些变量
问题描述
我想使用我的 .dir-locals.el 在项目上设置 virtualenv 名称。该变量已设置,但在用作其他连接变量中的参数时不被视为。
我尝试将 .dir-locals.el 文件添加到 python 项目中。在其中,我为这个特定项目设置了 virtualenv 根名称。之后我启动 Emacs,打开一个 .py 文件以检查是否所有需要的变量都使用 virtualenv 名称进行了更改。
我的 emacs 配置中关于 python-mode 的部分
;;; Python mode
(require 'python)
(ensure-package 'python-environment)
(require 'python-environment)
(ensure-package 'python-mode)
(require 'python-mode)
;;; dash used for pip-requirements
(ensure-package 'dash)
(require 'dash)
(require 'pip-requirements)
(require 'jedi-core)
(require 'company-jedi)
(defun init-python-mode()
(setq-local tab-width 4)
(setq-local indent-tabs-mode nil)
(set (make-local-variable 'company-backends)
'((company-jedi company-dabbrev-code)
company-capf company-files))
(setq python-environment-directory (expand-file-name "~/.virtualenvs")
python-indent-guess-indent-offset nil
python-indent-offset 4
jedi:complete-on-dot t
jedi:use-shortcuts t
jedi:tooltip-method nil
jedi:get-in-function-call-delay 0
python-shell-interpreter (concat python-environment-directory
"/" python-environment-default-root-name
"/bin" "/python")
jedi:server-command (list (concat python-environment-directory
"/" python-environment-default-root-name
"/bin" "/jediepcserver"))
python-shell-interpreter (concat python-environment-directory
"/" python-environment-default-root-name
"/bin" "/python")
flycheck-python-pylint-executable (concat python-environment-directory
"/" python-environment-default-root-name
"/bin" "/pylint")
flycheck-python-flake8-executable (concat python-environment-directory
"/" python-environment-default-root-name
"/bin" "/flake8")
flycheck-python-pycompile-executable (concat python-environment-directory
"/" python-environment-default-root-name
"/bin" "/python")
)
(flycheck-mode t)
)
(defun after-init-python-mode()
(eldoc-mode -1)
)
(define-key python-mode-map (kbd "C-c C-k") #'comment-dwim)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-hook 'python-mode-hook #'init-python-mode)
(add-hook 'python-mode-hook #'jedi:setup)
(add-hook 'python-mode-hook #'after-init-python-mode)
项目的 .dir-locals.el 文件
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")
((python-mode . ((python-environment-default-root-name . "abc")
)))
Emacs 似乎对 .dir-locals.el 变量做出反应,我确认这些变量在启动时是安全的。之后,通过执行: 'describe-variable python-environment-default-root-name' 我得到这个:
python-environment-default-root-name is a variable defined in ‘python-environment.el’.
Its value is "abc"
Original value was "default"
Local in buffer setup.py; global value is "default"
This variable’s value is directory-local, set by the file
‘/home/mkj/dev/adm/.dir-locals.el’.
......这是预期的。
执行时:'describe-variable jedi:server-command'、'describe-variable python-shell-interpreter',以及使用 python-environment-default-root-name 的其他变量,我得到这个:
jedi:server-command is a variable defined in ‘jedi-core.el’.
Its value is ("/home/mkj/.virtualenvs/default/bin/jediepcserver")
This variable may be risky if used as a file-local variable.
python-shell-interpreter is a variable defined in ‘python.el’.
Its value is "/home/mkj/.virtualenvs/default/bin/python"
Original value was "python"
在我看来,使用 python-environment-default-root-name 的变量名的 setq 仅在设置默认值并且 .dir-locals.el 值被忽略或设置太晚时设置一次。
这里是否存在竞争条件,或者这只是为 python 模式设置基于 virtualenv 的变量的错误方法?
解决方案
我发现 elpy 更适合这个目的。它似乎可以无缝地更改公司后端、python 解释器、flycheck 等的变量,而无需更改 pyvenv-workon 变量。