Skip to content

Instantly share code, notes, and snippets.

@habamax
Last active April 22, 2024 11:39
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save habamax/290cda0e0cdc6118eb9a06121b9bc0d7 to your computer and use it in GitHub Desktop.
Save habamax/290cda0e0cdc6118eb9a06121b9bc0d7 to your computer and use it in GitHub Desktop.
Setup Emacs for Python with Treesitter and Eglot

Emacs, Python, Treesitter and Eglot

Treesitter (“better”/faster syntax highlighting)

  1. Make sure you have Emacs with treesitter support:

    M-: (treesit-available-p) RET should return t

  2. Make sure you have installed python treesitter grammar
    • Windows: download libtree-sitter-python.dll from https://corwin.bru.st/emacs-tree-sitter/ into ~/.emacs.d/tree-sitter/
    • GNU/Linux: run M-x treesit-install-language-grammar RET type python RET and do a couple of additional y for default options. You will need to have git and C/Cpp compiler available as it would download repo and build grammar library.
  3. Open python file and do M-x python-ts-mode RET to activate treesitter python mode.
  4. Press C-h m to see if python treesitter is in place, third line should state:
    Major mode for editing Python files, using tree-sitter library.
        
  5. Optional, auto enable treesitter for python:
    (setq major-mode-remap-alist
          '((python-mode . python-ts-mode)))
        

Eglot (Emacs LSP client)

  1. Install eglot (not needed for Emacs 29.1)
  2. Install python language server, example for Debian 12:
    $ sudo apt install python3-pylsp
        

    For others, check the their corresponding docs. Or use venv.

  3. In python buffer run M-x eglot RET

It should state it is successfully connected.

Completion UI

For completion UI corfu might work. Install and enable with (global-corfu-mode).

Use (setq tab-always-indent 'complete) to let you TAB to trigger completion. Default is C-M-i.

https://asciinema.org/a/f9hhpZGHqYX1JRrmY2ZWhdn3A.svg

@shaohme
Copy link

shaohme commented Aug 31, 2023

How do you deal with python venvs in your TS setup, if at all?

@habamax
Copy link
Author

habamax commented Aug 31, 2023

TS has nothing to do with venvs, it doesn't run python.

For the eglot I don't do anything (I usually use a single venv to separate python from the one bundled by OS)

@shaohme
Copy link

shaohme commented Aug 31, 2023

Okay. I just wondered because using pyvenv + pyvenv-auto with python-mode is rather nifty when dealing with multiple venvs 😄

@habamax
Copy link
Author

habamax commented Aug 31, 2023

I guess you can do the same here 🤷‍♂️

@altay-oz
Copy link

$ sudo apt install python3-pylsp

@AnabasisXu
Copy link

AnabasisXu commented Feb 5, 2024

Under python-ts-mode, I can not turn on eglot, for it will report:

[eglot] Sorry, couldn't guess for `python-ts-mode'!
Enter program to execute (or <host>:<port>): 

Solved:
It turns out that my eglot version is too old. The built-in version will support all xxx-ts-mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment