【问题标题】:How can I make flycheck use virtualenv我怎样才能让 flycheck 使用 virtualenv
【发布时间】:2015-10-05 06:26:36
【问题描述】:

我刚刚愉快地配置了 emacs,通过 jedi 自动完成,通过 flycheck 和在 bootstrap 中创建的 virtualenvs 进行语法检查。这一切似乎都奏效了。

我想添加使用 flycheck-pylint 的功能(以获取导入错误),但我无法使其工作。即使我手动更改了 virtualenv (M-x: 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)))

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

如何更改使用的解释器?

【问题讨论】:

    标签: python emacs virtualenv flycheck


    【解决方案1】:

    今天解决这个问题,我找到了另一个解决方案(适用于当前版本的 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")))
    

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

    【讨论】:

      【解决方案2】:

      感谢Lunaryorn on 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)
      

      【讨论】:

        猜你喜欢
        • 2016-05-06
        • 2021-05-27
        • 2017-09-07
        • 2011-06-14
        • 1970-01-01
        • 2021-10-02
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        相关资源
        最近更新 更多