You open app/models.py, press M-. on a class name and Emacs asks for a tags table you never built. That is stock Emacs without a language server, and it is where every Emacs Python IDE setup starts. The fix takes about forty lines of init.el: a tree-sitter major mode, Eglot talking to a language server, a way to pick the right virtual environment, a formatter, and the REPL that python.el has shipped for years.
This page assumes the base config from the starter guide and Emacs 29.1 or newer, since both Eglot and tree-sitter support are built in from that release on. If you are still installing Emacs itself, the install guide comes first.
python-mode or python-ts-mode
Emacs has two built-in major modes for Python, both defined in python.el. python-mode uses regular-expression font locking. python-ts-mode, available since Emacs 29.1, parses the buffer with tree-sitter and highlights more precisely: decorators, f-string contents and nested definitions come out right more often.
python-ts-mode needs the Python grammar installed once. Emacs can fetch and compile it for you if a C compiler is available:
(setq treesit-language-source-alist
'((python "https://github.com/tree-sitter/tree-sitter-python")))
;; Run once: M-x treesit-install-language-grammar RET python RET
(add-to-list 'major-mode-remap-alist '(python-mode . python-ts-mode))
The remap means every .py file opens in python-ts-mode while packages that hook into python-mode keep working through the shared parent behaviour. If the grammar is missing, Emacs warns and you can fall back by removing the remap line.
Choosing a language server for Emacs Python LSP
The language server does the heavy lifting: completion, go to definition, find references, hover docs, rename and diagnostics. Emacs only displays what the server reports, so the choice of server decides how good your Emacs Python LSP experience is.
- basedpyright is a community fork of Microsoft’s pyright with extra checks and features that are otherwise limited to the Pylance extension. It installs from PyPI.
- pyright is the original, installed through npm.
- python-lsp-server (
pylsp) is written in Python and extended through plugins, for example for Ruff or mypy.
pipx install basedpyright # provides basedpyright-langserver
npm install -g pyright # provides pyright-langserver
pipx install python-lsp-server # provides pylsp
You need only one. For most projects basedpyright gives the most accurate type information and the fastest completion. pylsp is the better pick if you want everything in Python and like assembling plugins.
Eglot: the LSP client for an Emacs Python IDE
Eglot is the built-in LSP client and needs very little configuration. It already knows several Python servers; the explicit entry below just makes the choice visible in your config.
(use-package eglot
:hook ((python-mode python-ts-mode) . eglot-ensure)
:config
(add-to-list 'eglot-server-programs
'((python-mode python-ts-mode)
. ("basedpyright-langserver" "--stdio"))))
With this hook, opening any Python file starts the server for the current project, where “project” usually means the enclosing Git repository. M-. now jumps to definitions, M-? finds references, eglot-rename renames a symbol across files, and Flymake underlines errors as you type. Completion arrives through the standard completion-at-point, so a popup UI such as Corfu shows it without extra glue; the completion guide covers that choice.
If you chose pylsp instead, its plugins are switched on through Eglot’s workspace configuration rather than through Emacs variables. The python-lsp-ruff plugin, for example, is enabled like this:
(setq-default eglot-workspace-configuration
'(:pylsp (:plugins (:ruff (:enabled t)))))
The same variable can live in .dir-locals.el when one project needs different settings, which keeps your Emacs Python LSP defaults global and the exceptions local.
This is the point where Emacs starts to feel like a Python IDE. What is still missing is environment awareness, formatting and a fast way to run things.
Virtual environments in your Emacs Python setup
A language server that cannot see your installed packages reports every third-party import as unresolved. There are two clean ways to fix that.
The first is to tell the server directly. pyright and basedpyright read pyrightconfig.json or a [tool.pyright] (or [tool.basedpyright]) table in pyproject.toml:
[tool.basedpyright]
venvPath = "."
venv = ".venv"
This works no matter how Emacs was started, and it also helps colleagues using other editors. If your project keeps its environment in .venv at the root, basedpyright often finds it without any setting at all.
The second way is to activate the environment inside Emacs, so that run-python, M-x compile and the language server all use the same interpreter. pyvenv does this, and its tracking mode reads a buffer-local setting you can store per project in .dir-locals.el:
(use-package pyvenv
:ensure t
:config (pyvenv-tracking-mode 1))
;; .dir-locals.el in the project root
((python-ts-mode . ((pyvenv-activate . "/home/me/code/shop/.venv"))))
After the environment changes, run M-x eglot-reconnect so the server restarts with the new path. If you already use direnv in the shell, the envrc package applies .envrc files per buffer and replaces pyvenv entirely.
Formatting with Ruff or Black
Formatting on save keeps diffs small and ends style discussions. Ruff has become the common choice because it is a fast formatter and linter in one binary, and its formatter is designed to match Black’s output closely.
apheleia, from the Radian project, formats asynchronously and keeps the cursor in place after the buffer changes. It ships definitions for both black and ruff:
(use-package apheleia
:ensure t
:config
(setf (alist-get 'python-ts-mode apheleia-mode-alist) '(ruff))
(setf (alist-get 'python-mode apheleia-mode-alist) '(ruff))
(apheleia-global-mode 1))
If you prefer something smaller, Steve Purcell’s reformatter.el turns any command-line formatter into a mode in a few lines:
(use-package reformatter
:ensure t
:config
(reformatter-define ruff-format
:program "ruff"
:args '("format" "--stdin-filename" "x.py" "-"))
:hook (python-ts-mode . ruff-format-on-save-mode))
With formatting on save, the Emacs Python IDE behaves the way most editors do out of the box. Pick one tool for formatting. Running apheleia, a reformatter mode and Eglot’s own formatting together leads to buffers that change twice on save.
Checking the setup on a real project
Before adding more packages, test the setup on a project you know well. Open a file that imports a third-party library and press M-. on one of its names. If Emacs jumps into the library source inside your virtual environment, the server sees the right interpreter. If it reports nothing found, the environment step above is the culprit, not Eglot.
Next, make a deliberate type error and save. A red underline within a second or two means diagnostics flow; M-x flymake-show-buffer-diagnostics lists them all. When nothing appears, M-x eglot-events-buffer shows whether the server started at all, and the first lines usually name the missing executable. Most Emacs Python LSP problems come down to a server binary that is not on the exec-path Emacs sees, which is common when Emacs is launched from a desktop menu rather than a shell. The exec-path-from-shell package fixes that on macOS and some Linux desktops.
Running tests from Emacs
The zero-dependency route is M-x compile with pytest as the command. The *compilation* buffer turns tracebacks into clickable links, and M-g n walks through failures one by one.
(add-hook 'python-ts-mode-hook
(lambda ()
(setq-local compile-command "pytest -q")))
For finer control, the python-pytest package from MELPA adds commands to run the test at point, the current file, or the last failed tests, plus a transient menu with python-pytest-dispatch. It works well for pytest-based projects; for unittest suites, compile with python -m unittest is enough.
The REPL: run-python and C-c C-c
The REPL integration is the oldest part of Emacs Python support and still one of its best features. C-c C-p runs run-python and opens an inferior Python process tied to the current buffer.
From the source buffer you then send code:
- C-c C-c sends the whole buffer (
python-shell-send-buffer). - C-c C-r sends the active region.
- C-M-x sends the function or class around point.
- C-c C-z jumps to the REPL buffer and back.
By default C-c C-c does not execute an if __name__ == "__main__": block; give it a prefix argument (C-u C-c C-c) when you want that block to run too.
IPython works as the interpreter if you prefer its output:
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "-i --simple-prompt")
The --simple-prompt flag matters: without it IPython’s terminal features garble the comint buffer. With pyvenv active, run-python picks up the environment’s interpreter, so the REPL sees the same packages as the language server.
What a complete Emacs Python IDE config adds up to
Put together, the pieces above give you an Emacs Python IDE that is easy to reason about: one mode, one LSP client, one server, one formatter, one test entry point and the REPL. Each part is replaceable on its own. Swapping basedpyright for pylsp is a one-line change in eglot-server-programs; swapping apheleia for reformatter.el touches only the formatting block.
A Python IDE in a box like PyCharm hides those decisions. Here they sit in plain view, which is the reason this kind of Emacs Python setup stays debuggable a year later. If a feature breaks, M-x eglot-events-buffer shows the raw LSP traffic, and M-x describe-mode lists every minor mode active in the buffer.
The same pattern (tree-sitter mode, Eglot, a server, a formatter) carries over almost unchanged to the Go guide and the Rust guide, which makes Python a good first language to configure. If you also write type stubs or notebooks, look at basedpyright’s settings for strictness levels next, since a strict project-wide setting in pyproject.toml changes what Flymake shows far more than any Emacs option does.
Questions readers ask
Which Python language server should I use with Eglot?
basedpyright or pyright give the best type checking and fast completions. python-lsp-server (pylsp) is pure Python and easy to extend with plugins. Start with basedpyright unless your team already standardises on pylsp.
Why does Eglot not see the packages in my virtual environment?
The language server was started before the environment was active, or it cannot find the environment on its own. Activate the venv first and run M-x eglot-reconnect, or point the server at the venv in pyrightconfig.json or pyproject.toml.
Do I still need elpy or anaconda-mode?
Not for a new config. Eglot plus a language server covers completion, jump to definition, diagnostics and documentation, which used to be the reason to install those packages. The built-in python.el still provides the REPL and indentation.
How do I send code to the Python REPL in Emacs?
Start a shell with C-c C-p (run-python), then use C-c C-c to send the whole buffer, C-c C-r to send the active region and C-M-x to send the current function or class. C-c C-z jumps to the REPL buffer.
Should I use python-mode or python-ts-mode?
Use python-ts-mode on Emacs 29 or later if the tree-sitter grammar is installed, because it highlights more accurately. Both modes come from the same built-in python.el and share the REPL commands, so switching back is painless if the grammar is missing.