Home Backend Development Python Tutorial Emacs for Python and Poetry Using `basedpyright-langserver`

Emacs for Python and Poetry Using `basedpyright-langserver`

Oct 07, 2024 pm 12:10 PM

Emacs for Python and Poetry Using `basedpyright-langserver`

I am very pleased with my current emacs setup for Python, but I found setup to be a little tricky. I will document my setup here for my future self and for any other Pythonistas looking for a solid emacs config.

Why basedpyright?

Up to this point, I've been using mypy --strict on the command line for all my typechecking needs, but mypy is quite slow even for very small codebases, and even in --strict mode it is just not as strict as it could be. It's also not a language server, which I want for my emacs setup.

basedpyright is a fork of pyright with some excellent improvements in both checking and in reliability. It is a fast and wonderfully strict typechecker with some good linting capabilities additionally.

The only drawbacks I am experiencing are that it doesn't play quite as nicely as mypy with the boto3-stubs clients for AWS and that it uses nonstandard # pyright: ignore comments instead of the standard # type: ignore comments, but I can live with those issues in favor of a rigorously strict type-checking experience.

Configuring your Poetry projects to work with basedpyright or pyright

If you have existing projects that use Poetry, you're going to want to fiddle a little with your virtual environment.

Go to the root of each of your projects and run the following command:


poetry config --local virtualenvs.in-project true


Copy after login

This will create a poetry.toml file if one does not already exist and add a corresponding setting.

This, however, will not actually move your existing virtual environment. In order for this change to take effect, you have to remove your existing virtual environment, which you can find by running


poetry env info --path


Copy after login

Don't forget to add the .venv path to your .gitignore and the configuration for any other tools you might use, like pycodestyle, or you'll end up with quite a mess.

Next, you should add a configuration section to your pyproject.toml to tell basedpyright where to look for your virtual environment.


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


Copy after login

You can also do this in pyrightconfig.json file if, unlike me, you do not already feel completely overrun with config files for different Python development tools.

Installing basedpyright as a language server

One very nice thing about basedpyright over pyright is that it builds the nodejs dependency as a wheel, so you can be assured that basedpyright should work on your machine regardless of whether you have nodejs installed.

For isolation, it is usually a good idea to install executable Python packages using pipx instead of pip. Let's go ahead and do that .


pipx install basedpyright


Copy after login

For a sanity check, consider running


basedpyright --version


Copy after login

Installing basedpyright also gives you access to thebasedpyright-langserver command, but that's not really written for users to interact with, so if you runbasedpyright-langserver orbasedpyright-langserver --version or something, you'll get a nodejs stack trace.

Configuring emacs to use basedpyright-langserver

I'll assume you as the reader know how to install packages from MELPA and have a preferred way to do it. Here are all the packages you need:

  • company
  • lsp-mode
  • lsp-pyright
  • lsp-ui
  • python-mode

Get those installed and then open your ~/.emacs or your ~/.emacs.d/init.el and add the following:


;; lsp global settings
(add-hook 'after-init-hook 'global-company-mode)
(setq lsp-auto-guess-root t)

;; python
(require 'lsp-mode)
(setq lsp-pyright-langserver-command "basedpyright")
(add-hook 'python-mode-hook (lambda () (require 'lsp-pyright) (lsp)))


Copy after login

That should be all you need.

A general tip for debugging lsp-mode

If you feel like you've set everything up correctly and you're still having trouble getting lsp-mode to find packages that should be available in Poetry, one thing you might try is to go in and delete your ~/.emacs.d/.lsp-session-v1. This will have the effect of causing lsp-mode to forget about the project root and force it to look for it again.

Happy coding!

I hope this has given you a good head start on your Python development environment. If you have any questions, well, I'm a beginner with all this, and I probably can't help you, but I will consider merge requests if you have any extra tips or tricks for using basedpyright with emacs.

The above is the detailed content of Emacs for Python and Poetry Using `basedpyright-langserver`. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

See all articles