Wandering Thoughts archives

2019-08-18

Early notes on using LSP-based editing in GNU Emacs for Python

The two languages that I most use GNU Emacs for these days are Go and Python. After I got LSP-based editing working for Go, I decided to take a run at getting it to work for Python as well. Python is one of the languages that lsp-mode supports, through pyls, so I was hoping that it would be an install and go experience. The reality was not quite so smooth and I've wound up with some open questions and uncertainties.

As I usually do with pip-based install instructions, I opted to use 'pip install --user', which puts the resulting programs in ~/.local/bin. Since this isn't on my regular $PATH, I had to arrange for GNU Emacs to be able to see the pyls program before lsp-mode could do anything. Once it did, warnings popped up all over the Python code that I tried it out on, because I'd installed it as 'python-language-server[all]', which installs all linters and checkers. I must regretfully report that my code is not at all clean to all of them; for example, I frequently use short variable names that are all in lower case. After poking at this a bit I decided that I didn't want any linters right now. Some of the linters apparently could be disabled by 'pip uninstall', but others have standard Ubuntu versions and it's not clear how to tell lsp-mode to tell pyls to turn them off, and anyway some of them may be used to detect outright syntax errors, which I would like flagged.

Talking of syntax errors brings up the next issue, which is Python 2 versus Python 3. While we're moving towards Python 3, we still have plenty of Python 2 code, and so I would like a LSP-based setup that works smoothly with both. Unfortunately, as far as I can see pyls is at least partially specific to the version of Python you install it for. I actually used pip3 to install the Python 3 versions of things (since that's our future and seems the right choice if I have to pick one). This still seems to at least partially work for some test Python 2 code, in that in simple navigation works, but various syntax warnings and so on appear and there may be other LSP things that don't.

(As far as I can tell, pyls has no particular provisions for picking Python versions, which is not surprising. Some things I've read suggest that most people who have to deal with this use per-project virtualenvs, and Python 2 projects would then have the Python 2 version of pyls installed in their virtualenv. Manually starting GNU Emacs with a $PATH that finds the Python 2 version of pyls first does seem to work right, and I may be able to partially automate this with a frontend script for pyls that tries to figure out which Python version is more likely for the current context.)

All of this makes me fairly uncertain about whether lsp-mode is currently worth it for my Python programming. It does give me nice things like completions, but it's probably not going to be a set and forget thing the way it is for Go. Probably I'm going to be shaving more yaks before I have clear answers.

(There are various writeups on the net of using Python with lsp-mode but they seem to mostly come from people who already know a lot of Emacs, which is not me these days. Reading them and flailing away at my .emacs has been a humbling experience.)

PS: As usual, writing this entry pushed me to go further, try more things, and do more experimentation than I had at the start, which is a good thing.

python/PythonEmacsLSPNotes written at 22:56:47; Add Comment


Page tools: See As Normal.
Search:
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.