Emacs中Python开发

在Emacs中开发Python的指南,包含虚拟环境、LSP、Python项目配置、Emacs工程配置。

创建虚拟环境

cd /path/to/project
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

安装LSP服务端

Pyright is a full-featured, standards-based static type checker for Python. It is designed for high performance and can be used with large Python source bases.

npm install -g pyright

项目配置

安装完Pyright后,在 pyproject.toml 中增加配置:

[tool.pyright]
venvPath = "."
venv = ".venv"

[tool.black]
line-length = 120

python-mode配置

在Emacs中使用Eglot作为LSP客户端,在 .dir-locals.el 中增加配置:

((python-base-mode . ((python-indent-offset . 4)
                      (python-indent-guess-indent-offset-verbose . nil)
                      (python-shell-interpreter . "python")
                      (python-shell-virtualenv-root . "/path/to/project/.venv/"))))