README.md
eglot-python-preset
Configures Python LSP support for Emacs using Eglot, including support for PEP-723 script metadata and automatic project detection.
This package configures Eglot to work with Python files using either ty or basedpyright as the language server. It automatically handles environment synchronization for uv-managed scripts with inline dependencies.
Prerequisites
- Emacs 30.2 or later
- uv - fast Python package installer and resolver
- One of the following language servers:
- ty (>= v0.0.8) - Astral's new Python type
checker. It can be installed globally or in a project-root
.venv. - basedpyright - fork of pyright
with additional features. It can be installed globally or in a project-root
.venv.
- ty (>= v0.0.8) - Astral's new Python type
checker. It can be installed globally or in a project-root
Installation
Choose one of the following ways to install. After that, opening Python files will automatically start the LSP server using Eglot.
From MELPA (recommended)
| 1 | (use-package eglot-python-preset |
| 2 | :ensure t |
| 3 | :after eglot |
| 4 | :custom |
| 5 | (eglot-python-preset-lsp-server 'ty) ; or 'basedpyright |
| 6 | :config |
| 7 | (eglot-python-preset-setup)) |
Manual installation
Clone this repository and add to your Emacs configuration:
| 1 | mkdir -p ~/devel |
| 2 | git clone https://github.com/mwolson/eglot-python-preset ~/devel/eglot-python-preset |
| 1 | (add-to-list 'load-path (expand-file-name "~/devel/eglot-python-preset")) |
| 2 | (require 'eglot-python-preset) |
| 3 | (setopt eglot-python-preset-lsp-server 'ty) ; or 'basedpyright |
| 4 | (eglot-python-preset-setup) |
Usage
Standard Python Projects
For standard Python projects (those with pyproject.toml or
requirements.txt), the package automatically detects the project root and
starts Eglot with appropriate configuration.
PEP-723 Scripts
PEP-723 allows embedding dependency metadata directly in Python scripts using special comments:
| 1 | # /// script |
| 2 | # requires-python = ">=3.10" |
| 3 | # dependencies = [ |
| 4 | # "requests>=2.31.0", |
| 5 | # ] |
| 6 | # /// |
| 7 | |
| 8 | import requests |
| 9 | |
| 10 | response = requests.get("https://example.com") |
| 11 | print(response.status_code) |
When you open a file containing PEP-723 metadata:
- The package detects the script metadata automatically
- It locates or allows you to manually create an isolated environment via
uv - The LSP server is configured to use that environment for type checking
Commands
M-x eglot-python-preset-sync-environment- Sync dependencies for the current PEP-723 script usinguv sync --script, then restart Eglot. Use this after adding or modifying dependencies in your script's metadata block.M-x eglot-python-preset-remove-environment- Remove the cached uv environment for the current PEP-723 script. Useful when you want to force a clean reinstall of dependencies or troubleshoot environment issues. After removal, runeglot-python-preset-sync-environmentto recreate it.M-x eglot-python-preset-run-script- Run the current PEP-723 script usinguv run. Opens a compilation buffer with the output.
Configuration
eglot-python-preset-lsp-server
Choose which language server to use:
| 1 | (setopt eglot-python-preset-lsp-server 'ty) ; default |
| 2 | ;; or |
| 3 | (setopt eglot-python-preset-lsp-server 'basedpyright) |
eglot-python-preset-python-project-markers
Files that indicate a Python project root:
| 1 | (setopt eglot-python-preset-python-project-markers |
| 2 | '("pyproject.toml" "requirements.txt")) ; default |
Workspace Configuration (basedpyright)
To customize basedpyright settings, set eglot-workspace-configuration before
calling eglot-python-preset-setup. Your settings will be merged with PEP-723
script configurations (which add the Python interpreter path).
Example to disable auto-import completions and set type checking mode:
| 1 | ;; With use-package |
| 2 | (use-package eglot-python-preset |
| 3 | :ensure t |
| 4 | :after eglot |
| 5 | :custom |
| 6 | (eglot-python-preset-lsp-server 'basedpyright) |
| 7 | (eglot-workspace-configuration |
| 8 | '(:basedpyright.analysis |
| 9 | (:autoImportCompletions :json-false |
| 10 | :typeCheckingMode "basic"))) |
| 11 | :config |
| 12 | (eglot-python-preset-setup)) |
| 13 | |
| 14 | ;; Or manually (can be spread across multiple init.el sections) |
| 15 | (setopt eglot-python-preset-lsp-server 'basedpyright) |
| 16 | (setopt eglot-workspace-configuration |
| 17 | (plist-put eglot-workspace-configuration |
| 18 | :basedpyright.analysis |
| 19 | '(:autoImportCompletions :json-false |
| 20 | :typeCheckingMode "basic"))) |
| 21 | (eglot-python-preset-setup) |
Notes
- The package uses
uvfor all Python environment management. Ensureuvis installed and in your PATH. - For standard Python projects, the package prefers
tyorbasedpyright-langserverfrom a project-root.venvand otherwise falls back to PATH. - For PEP-723 scripts, environments are cached by
uvand shared across sessions. - If you see a warning about the environment not being synced, run
M-x eglot-python-preset-sync-environment.