Emacs für Python und Poesie mit „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
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
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="."
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
For a sanity check, consider running
basedpyright --version
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)))
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.
Das obige ist der detaillierte Inhalt vonEmacs für Python und Poesie mit „basedpyright-langserver'.. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen











Python eignet sich für Datenwissenschafts-, Webentwicklungs- und Automatisierungsaufgaben, während C für Systemprogrammierung, Spieleentwicklung und eingebettete Systeme geeignet ist. Python ist bekannt für seine Einfachheit und sein starkes Ökosystem, während C für seine hohen Leistung und die zugrunde liegenden Kontrollfunktionen bekannt ist.

Sie können die Grundlagen von Python innerhalb von zwei Stunden lernen. 1. Lernen Sie Variablen und Datentypen, 2. Master -Steuerungsstrukturen wie wenn Aussagen und Schleifen, 3. Verstehen Sie die Definition und Verwendung von Funktionen. Diese werden Ihnen helfen, einfache Python -Programme zu schreiben.

Python zeichnet sich in Gaming und GUI -Entwicklung aus. 1) Spielentwicklung verwendet Pygame, die Zeichnungen, Audio- und andere Funktionen bereitstellt, die für die Erstellung von 2D -Spielen geeignet sind. 2) Die GUI -Entwicklung kann Tkinter oder Pyqt auswählen. Tkinter ist einfach und einfach zu bedienen. PYQT hat reichhaltige Funktionen und ist für die berufliche Entwicklung geeignet.

Sie können grundlegende Programmierkonzepte und Fähigkeiten von Python innerhalb von 2 Stunden lernen. 1. Lernen Sie Variablen und Datentypen, 2. Master Control Flow (bedingte Anweisungen und Schleifen), 3.. Verstehen Sie die Definition und Verwendung von Funktionen, 4. Beginnen Sie schnell mit der Python -Programmierung durch einfache Beispiele und Code -Snippets.

Python ist leichter zu lernen und zu verwenden, während C leistungsfähiger, aber komplexer ist. 1. Python -Syntax ist prägnant und für Anfänger geeignet. Durch die dynamische Tippen und die automatische Speicherverwaltung können Sie die Verwendung einfach zu verwenden, kann jedoch zur Laufzeitfehler führen. 2.C bietet Steuerung und erweiterte Funktionen auf niedrigem Niveau, geeignet für Hochleistungsanwendungen, hat jedoch einen hohen Lernschwellenwert und erfordert manuellem Speicher und Typensicherheitsmanagement.

Python wird in den Bereichen Webentwicklung, Datenwissenschaft, maschinelles Lernen, Automatisierung und Skripten häufig verwendet. 1) In der Webentwicklung vereinfachen Django und Flask Frameworks den Entwicklungsprozess. 2) In den Bereichen Datenwissenschaft und maschinelles Lernen bieten Numpy-, Pandas-, Scikit-Learn- und TensorFlow-Bibliotheken eine starke Unterstützung. 3) In Bezug auf Automatisierung und Skript ist Python für Aufgaben wie automatisiertes Test und Systemmanagement geeignet.

Um die Effizienz des Lernens von Python in einer begrenzten Zeit zu maximieren, können Sie Pythons DateTime-, Zeit- und Zeitplanmodule verwenden. 1. Das DateTime -Modul wird verwendet, um die Lernzeit aufzuzeichnen und zu planen. 2. Das Zeitmodul hilft, die Studie zu setzen und Zeit zu ruhen. 3. Das Zeitplanmodul arrangiert automatisch wöchentliche Lernaufgaben.

Python zeichnet sich in Automatisierung, Skript und Aufgabenverwaltung aus. 1) Automatisierung: Die Sicherungssicherung wird durch Standardbibliotheken wie OS und Shutil realisiert. 2) Skriptschreiben: Verwenden Sie die PSUTIL -Bibliothek, um die Systemressourcen zu überwachen. 3) Aufgabenverwaltung: Verwenden Sie die Zeitplanbibliothek, um Aufgaben zu planen. Die Benutzerfreundlichkeit von Python und die Unterstützung der reichhaltigen Bibliothek machen es zum bevorzugten Werkzeug in diesen Bereichen.
