Last active
July 21, 2022 00:40
-
-
Save Nathan-Furnal/07413de07b9ac08340a5d33b541e9a6b to your computer and use it in GitHub Desktop.
Python setup for Emacs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;======================================== | |
;;; Disclaimer | |
;;;======================================== | |
;; This configuration relies on use-package: https://github.com/jwiegley/use-package | |
;;;======================================== | |
;;;======================================== | |
;;; Python | |
;;;======================================== | |
(use-package python | |
:ensure t | |
:defer t | |
:delight "Py" | |
:config | |
;; Remove guess indent python message | |
(setq python-indent-guess-indent-offset-verbose nil)) | |
;; Hide the modeline for inferior python processes (as well as R) | |
(use-package inferior-python-mode | |
:ensure nil | |
:hook (inferior-python-mode . hide-mode-line-mode)) | |
(use-package hide-mode-line | |
:ensure t | |
:defer t) | |
;;; Use poetry to manage packages and when to switch to virtualenvs | |
;;; See https://python-poetry.org/ for more info | |
;;; See https://python-poetry.org/docs/managing-environments/ for virtual envs management | |
(use-package poetry | |
:ensure t | |
:defer t | |
:config | |
(setq poetry-tracking-strategy 'switch-buffer) | |
(setenv "WORKON_HOME" "~/.cache/pypoetry/virtualenvs")) ;; keeping track of virtualenvs available | |
;; Buffer formatting on save | |
(use-package blacken | |
:ensure t | |
:defer t | |
:custom | |
(blacken-allow-py36 t) | |
(blacken-skip-string-normalization t) | |
:hook (python-mode . blacken-mode)) | |
;; numpy docstring for python | |
(use-package numpydoc | |
:ensure t | |
:defer t | |
:custom | |
(numpydoc-insert-examples-block nil) | |
(numpydoc-template-long nil) | |
:bind (:map python-mode-map | |
("C-c C-n" . numpydoc-generate))) | |
;;; Next up are packages which are not strictly Python related | |
;;; and apply for lots of languages, YMMV but it's a good idea to use company for completion | |
;;; and some form of language server interface (lsp-mode or eglot), this file uses lsp-mode. | |
(use-package company | |
:ensure t | |
:defer t | |
:diminish "" | |
:custom | |
(company-dabbrev-other-buffers t) | |
(company-dabbrev-code-other-buffers t) | |
;; M-<num> to select an option according to its number. | |
(company-show-numbers t) | |
;; Only 2 letters required for completion to activate. | |
(company-minimum-prefix-length 3) | |
;; Do not downcase completions by default. | |
(company-dabbrev-downcase nil) | |
;; Even if I write something with the wrong case, | |
;; provide the correct casing. | |
(company-dabbrev-ignore-case t) | |
;; Don't wait before completion. | |
(company-idle-delay 0) | |
;; No company-mode in shell & eshell | |
(company-global-modes '(not eshell-mode shell-mode)) | |
:hook ((text-mode . company-mode) | |
(prog-mode . company-mode))) | |
;;; `lsp-mode` proper | |
;;; The config should be relatively agnostic up to here | |
(use-package lsp-mode | |
:ensure t | |
:defer t | |
:delight " LSP" | |
:defines (lsp-keymap-prefix lsp-mode-map) | |
:init | |
(setq lsp-keymap-prefix "C-c l") | |
:custom | |
(lsp-keep-workspace-alive nil) | |
(lsp-auto-guess-root nil) | |
(lsp-eldoc-enable-hover nil) | |
(lsp-signature-auto-activate nil) | |
(lsp-completion-enable t) | |
:hook (lsp-mode-hook . lsp-enable-which-key-integration) | |
:commands (lsp lsp-deferred) | |
:bind (:map lsp-mode-map | |
("M-<RET>" . lsp-execute-code-action))) | |
;; Debugger | |
(use-package dap-mode | |
:ensure t | |
:defer t | |
:after lsp-mode | |
:config | |
(dap-auto-configure-mode)) | |
;;; See https://emacs-lsp.github.io/lsp-mode/ for more info | |
(use-package lsp-pyright | |
:ensure t | |
:defer t | |
:custom | |
(lsp-pyright-disable-language-service nil) | |
(lsp-pyright-disable-organize-imports nil) | |
(lsp-pyright-auto-import-completions t) | |
(lsp-pyright-use-library-code-for-types t) | |
(lsp-completion-enable t) | |
:hook ((python-mode . (lambda () | |
(poetry-tracking-mode) | |
(require 'lsp-pyright) | |
(lsp-deferred))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist relies on a couple packages some are Emacs related, some are external tools for which there are Emacs packages:
It's a lot so you should probably work your way through each package before just copy/pasting this.
Note that if you use something like anaconda, there are packages like https://github.com/necaris/conda.el and https://github.com/jorgenschaefer/pyvenv, the latter being more agnostic to how virtual envs are managed.