lsp-python如何与pyvenv协同工作

大家好,我最近在尝试使用lsp来写python,但是在使它工作在virtualenv下的时候遇到一些问题,我的配置

(use-package pyvenv
  :ensure t
  :config
  (setenv "WORKON_HOME" (expand-file-name "~/anaconda3/envs/"))
  )

(use-package lsp-python
  :ensure t
  :config
  (setq lsp-print-io t)
  (add-hook 'python-mode-hook (lambda ()
                                (pyvenv-mode t)
                                (lsp-python-enable)))
  )

但是报了以下错误

lsp--stdio-wait: Content-Length: 1365

{
  "jsonrpc": "2.0",
  "method": "initialize",
  "params": {
    "processId": 7451,
    "rootPath": "/Users/xxx/xxxx/xxxx/",
    "rootUri": "file:///Users/xxx/xxxx/xxxx/",
    "capabilities": {
      "workspace": {
        "applyEdit": true,
        "executeCommand": {
          "dynamicRegistration": true
        },
        "workspaceFolders": true
      },
      "textDocument": {
        "synchronization": {
          "willSave": true,
          "didSave": true,
          "willSaveWaitUntil": true
        },
        "documentSymbol": {
          "symbolKind": {
            "valueSet": [
              1,
              2,
              3,
              4,
              5,
              6,
              7,
              8,
              9,
              10,
              11,
              12,
              13,
              14,
              15,
              16,
              17,
              18,
              19,
              20,
              21,
              22,
              23,
              24,
              25
            ]
          },
          "hierarchicalDocumentSymbolSupport": true
        },
        "formatting": {
          "dynamicRegistration": true
        },
        "codeAction": {
          "dynamicRegistration": true
        }
      }
    },
    "initializationOptions": null
  },
  "id": 1
}
lsp-python has exited (exited abnormally with code 1)
File mode specification error: (lsp-timed-out-error)

lsp-python-stderr ImportError: cannot import name 'JsonRpcException' 更详细的可以见我在github上的issue#458,希望得到你们的帮助。

或者可以分享一下你们的配置吗-。-

你还是用 VSCode 或是 PyCharm 吧

1 个赞

我平时vscode,pycharm都有使用啊,只是好奇社区里都是怎么使lsp和virtualenv一同工作的。

add-hook 放到:init里。

然后是我用的一些dir-local variables. 可以 自动启动virtual environment。使用pyvenv-workon需要设置$WORKON_HOME

Enable the virtual environment, and set the flycheck pylint executable.

pyvenv-workon: pyvenv-virtual-env-name

((nil . ((eval . (unless (and (boundp 'pyvenv-virtual-env-name) (equal pyvenv-virtual-env-name "cs231n"))
                   (pyvenv-workon "cs231n")))))
 (python-mode . ((flycheck-python-pylint-executable . "/usr/local/miniconda3/envs/cs231n/bin/pylint"))))

OR:

pyvenv-activate: pyvenv-virtual-env

((nil . ((eval . (unless (and (boundp 'pyvenv-virtual-env) (equal pyvenv-virtual-env "/usr/local/miniconda3/envs/cs231n"))
                   (pyvenv-activate "/usr/local/miniconda3/envs/cs231n")))))
 (python-mode . ((flycheck-python-pylint-executable . "/usr/local/miniconda3/envs/cs231n/bin/pylint"))))

感谢🙏,不过我这里尝试后依然会出现同样的问题,请问你的pyls是什么版本的呢,我查到一个相关问题,说pyls 0.21版本的bug导致了lsp-python不能正常工作#434

我用的0.21.2。emm,感觉暂时还是抛弃lsp了,我发现我enable了venv以后,还是有一些问题,有时候需要revert-buffer一下才能正常工作。还是用回anaconda-mode了,问题少一点。

BTW: 你可以参考doom 的python layer,建议使用conda.el而不是pyvenv,如果你是用anaconda的。使用(conda-env-autoactivate-mode t)自动化env activate,效果还不错。

我试试看,我现在是小项目用emacs,没补全也OK,大点的项目还是用的pycharm,不过开了emacs键位。