This gives you a modern editing experience with:
- Autocomplete and error checking (Elpy + Flycheck)
- Proper Git integration (Magit)
- A great package manager within Emacs (MELPA)
-
Install Python:
sudo apt update && sudo apt upgrade sudo apt install python3-dev python3-pip python3-venv -
Install Emacs:
sudo apt install emacsNote: If you prefer your editors terminal-based use the
emacs-noxpackage. -
Set up MELPA:
emacs ~/.emacsAdd the following:
(require 'package) (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t) ;; Comment/uncomment this line to enable MELPA Stable if desired. See `package-archive-priorities` ;; and `package-pinned-packages`. Most users will not need or want to do this. ;;(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t) (package-initialize)
-
Close and open Emacs.
-
Refresh the package list:
M-x package-refresh-contents -
Show the package list (
M-x package-list-packages) and install the following packages:- elpy
- flycheck
- magit
- poetry
-
Configure Emacs:
emacs ~/.emacsAdd the following:
;; Python IDE (defvar myPackages '(elpy flycheck ) ) (elpy-enable) (when (require 'flycheck nil t) (setq elpy-modules (delq 'elpy-module-flymake elpy-modules)) (setq elpy-eldoc-show-current-function nil) (add-hook 'elpy-mode-hook 'flycheck-mode) (add-hook 'elpy-mode-hook 'poetry-tracking-mode)) ;; Magit (global-set-key (kbd "C-x g") 'magit-status)
-
Open a
.pyfile. -
Set up the Elpy virtual environment:
M-x elpy-rpc-reinstall-virtualenvYou will be asked "Automatically install the RPC dependencies from PyPI", answer y.
-
[Optional] You might want to automatically load
.editorconfigfiles [https://editorconfig.org]:- Show the package list (
M-x package-list-packages) and installeditorconfig. - Add the following to your
~/.emacs:;; EditorConfig (require 'editorconfig) (editorconfig-mode 1)
- Show the package list (
- Always put the correct shebang at the top of Python scripts:
#!/usr/bin/env python3 - Make your scripts exectuable:
chmod +x example.py - Run scripts from inside Emacs with:
M-x compile./example.pyThe nice thing about this is you can jump straight to errors with:
CTRL-x + ` - Open Magit with:
CTRL-x + g