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

0. Emacs + LSP

lsp-modeemacs 基于 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 <RET>

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

(2)安装package

M-x package-install <RET> use-package <RET>
M-x package-install <RET> python-mode <RET>
M-x package-install <RET> lsp-mode <RET>
M-x package-install <RET> company-lsp <RET>

安装完后,发现 ~/.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: 

这里要按 <Tab> 键,进行选择,

选最后一个就好,

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354