[IDE] emacs lsp-mode + python-language-server

0. Emacs + LSP

lsp-mode 是 emacs 基于 LSP 的客户端。

python-language-server 是 LSP 的 Python 实现。

本文介绍了 Emacs lsp-mode 与 python-language-server 一起使用的办法。


1. emacs lsp-mode

1.1 安装emacs

本例中使用的是,Emacs For Mac OS X,下载后安装即可。

1.2 .emacs.d配置

emacs启动时默认执行 ~/.emacs.d/init.el,我们新建一个 init.el,写入如下内容,
配置 emacs 从 melpha 下载 package。

(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (proto (if no-ssl "http" "https")))
  (when no-ssl
    (warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
  ;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
  (add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
  ;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
  (when (< emacs-major-version 24)
    ;; For important compatibility libraries like cl-lib
    (add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)

1.3 安装package

打开emacs,先更新package列表,然后安装use-packagepython-modelsp-mode

(1)更新package列表

M-x package-refresh-contents 

表示先按Alt + x,然后输入 package-refresh-contents,最后按 回车

(2)安装package

M-x package-install  use-package 
M-x package-install  python-mode 
M-x package-install  lsp-mode 
M-x package-install  company-lsp 

安装完后,发现 ~/.emacs.d/init.el 中多了以下内容,

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages (quote (lsp-mode python-mode use-package))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

1.4 启用lsp-mode

打开 ~/.emacs.d/init.el,加入如下内容,

(setq lsp-clients-python-command "/usr/local/bin/pyls")
(use-package lsp-mode
  :hook (python-mode . lsp)
  :commands lsp)

其中,lsp-clients-python-command 要设置为 python-language-server 可执行文件的地址(下文介绍),emacs 会自动启动 server。

:hook (python-mode . lsp) 表示启用 python-mode 时调用命令 lsp

2. python-language-server

2.1 正常安装

$ pip install -U setuptools
$ pip install 'python-language-server[all]'

python-language-server的安装路径为,/Library/Python/2.7/site-packages

$ pip show python-language-server
Name: python-language-server
Version: 0.26.1
Summary: Python Language Server for the Language Server Protocol
Home-page: https://github.com/palantir/python-language-server
Author: Palantir Technologies, Inc.
Author-email: None
License: UNKNOWN
Location: /Library/Python/2.7/site-packages
Requires: future, configparser, pluggy, jedi, futures, backports.functools-lru-cache, python-jsonrpc-server
Required-by: pyls-isort

可执行文件 pyls 位于 /usr/local/bin/pyls

$ which pyls
/usr/local/bin/pyls

2.2 可能的一些错误

安装过程中,可能会遇到一系列的报错。

(1)安装 pipsudo easyinstall pip 报错

Could not find suitable distribution for Requirement.parse('pip')

解决方案:

$ curl 'https://bootstrap.pypa.io/get-pip.py' > get-pip.py
$ sudo python get-pip.py
$ sudo easy_install pip

参考:https://segmentfault.com/q/1010000014299471

(2)pip install 'python-language-server[all]' 报错

'install_requires' must be a string or list of strings

解决方案:

# install python
$ brew install python2
# change the path of the current terminal session
$ export PATH=/usr/local/share/python:$PATH
# setup the brew-installed pip
$ sudo pip install -U setuptools
# finally, install pls
$ sudo pip install 'python-language-server[all]'

参考:https://github.com/palantir/python-language-server/issues/349

(3)pip install --upgrade setuptools 报错

Could not install packages due to an EnvironmentError

解决方案:

$ sudo pip install setuptools --upgrade --ignore-installed

参考:https://github.com/pypa/pip/issues/5329

(4)setuptools_scm 报错

"setuptools_scm" could not be found

解决方案:

$ sudo pip install setuptools_scm

参考:https://github.com/pypa/pypi-legacy/issues/322

3. main.py

3.1 select action

新建一个 main.py 文件,用emacs打开,

消息区会显示:

main.py is not part of any project. Select aciton: 

这里要按 键,进行选择,

选最后一个就好,

Import project root /Users/xxx/.../

其中,Yasnippet is not present but ‘lsp-enable-snippet’ is set to ‘t’
可以在 ~/.emacs.d/init.el 中加入如下配置解决,

(setq lsp-enable-snippet nil)

3.2 LSP Connection

选好 action 后,emacs 会自动启动 pyls,并建立连接,
消息区显示,

LSP :: Connected to [pyls:3578 status:starting]

3.3 智能提示

我们在编辑区输入 print,emacs弹出了智能提示,

注:这个提示是 company-lsp 弹出的。


参考

github: lsp-mode
LSP
github: python-language-server

你可能感兴趣的:([IDE] emacs lsp-mode + python-language-server)