我怎样才能让flycheck使用virtualenv

Ale*_*lla 5 python emacs virtualenv flycheck

我很高兴通过jedi配置emacs并使用自动完成功能,并通过flycheck和booturap中创建的virtualenvs进行语法检查.这一切似乎都有效.

我想添加使用flycheck-pylint的功能(导入时出错),但我无法使其正常工作.即使我手动更改了virtualenv(Mx:pyvenv-activate RET path-to-my-venv),我仍然会看到很多导入错误来自于使用的错误virtualenv.

我目前的初始化代码:

(require 'pyvenv)
(add-hook 'after-init-hook #'global-flycheck-mode)
(defun set-flake8-executable ()
  (pyvenv-activate (get-current-buffer-venv))
  (flycheck-set-checker-executable (quote python-flake8)
               (get-current-buffer-flake8)))
Run Code Online (Sandbox Code Playgroud)

其中"get-current-buffer-venv"和"get-current-buffer-flake8"是实现我特定设置并正常工作的函数.

如何更改使用的解释器?

Ale*_*lla 9

感谢Lunaryorn在github上的回答,我意识到还有一个flycheck-set-pylint-executable.现在一切正常,配置如下:

(defun set-flychecker-executables ()
  "Configure virtualenv for flake8 and lint."
  (when (get-current-buffer-flake8)
    (flycheck-set-checker-executable (quote python-flake8)
                                     (get-current-buffer-flake8)))
  (when (get-current-buffer-pylint)
    (flycheck-set-checker-executable (quote python-pylint)
                                     (get-current-buffer-pylint))))
(add-hook 'flycheck-before-syntax-check-hook
          #'set-flychecker-executables 'local)
Run Code Online (Sandbox Code Playgroud)


Mek*_*ekk 6

今天解决这个问题,我找到了另一个解决方案(适用于当前版本的 Flycheck,截至 2020 年 6 月)。

.dir-locals.el只需为给定项目使用适当的设置进行创建即可。喜欢:

((python-mode
  (flycheck-python-flake8-executable . "/home/marcin/.virtualenvs/adgv/bin/python")
  (flycheck-python-pylint-executable . "/home/marcin/.virtualenvs/adgv/bin/python")))
Run Code Online (Sandbox Code Playgroud)

(使用也可以创建文件M-x add-dir-local-variable,但请记住在命令周围添加双引号)