Files
org-roam/20240130122737-elpy.org
2025-11-05 09:18:11 +01:00

2.6 KiB

elpy

Elpy is an emacs package to bring powerful python editing to Emacs. It combines and configures a number of other packages, both written in Emacs Lisp as well as Python. Elpy is fully documented at Readthedocs

Installation

Elpy is available on melpa, the most straightforward way to install it is to use use-package:

  (use-package elpy
  :ensure t
  :init
  (elpy-enable))

Quickstart

Once installed, Elpy will automatically provide code completion, syntax error highlighting and code hinting (in the modeline) for python files. Elpy offers a lot of features, but the following keybindings should be enough to get started:

C-c C-C

evaluates the current python script (or region if something is selected) in an interactive python shell. The python shell is automatically displayed aside of your script.

C-Ret

evaluates the current statement (current line plus the following nested lines).

C-c C-z

switches between your script and the interactive shell.

C-c C-d

displays documentation for the thing under cursor. The documentation will pop in a different buffer, that can be closed with q.

Emacs implementation

(use-package elpy
  :ensure t
  :hook ((elpy-mode . flycheck-mode)
           (elpy-mode . (lambda ()
                          (set (make-local-variable 'company-backends)
                               '((elpy-company-backend :with company-yasnippet))))))
  :init
  (elpy-enable)
  :config
  (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
  (setq elpy-shell-echo-output nil)
  (setq elpy-shell-echo-input nil) 
  (setq elpy-rpc-python-command "python3")
  (setq elpy-rpc-timeout 2))

(when (require 'flycheck nil t)
  (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
  (define-key elpy-mode-map (kbd "C-c p") 'flycheck-previous-error)
  (define-key elpy-mode-map (kbd "C-c n") 'flycheck-next-error)
  (add-hook 'elpy-mode-hook 'flycheck-mode))

In addition to that there is an entry in thea visual fill function for elpy:

(use-package visual-fill-column
  :hook ((org-mode . diz/org-mode-visual-fill)
	 (matlab-mode . diz/org-mode-visual-fill)
	 (elpy-mode . diz/pyth-mode-visual-fill) # <- This line
	 (python-mode . diz/pyth-mode-visual-fill))) # <- and this one