Skip to content

Instantly share code, notes, and snippets.

@peatmonster
Last active July 30, 2023 08:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peatmonster/90e60ae41f95e7c3e696159b2b3586a7 to your computer and use it in GitHub Desktop.
Save peatmonster/90e60ae41f95e7c3e696159b2b3586a7 to your computer and use it in GitHub Desktop.

Modern Emacs Python IDE

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)

Steps to install

  1. Install Python:
    sudo apt update && sudo apt upgrade
    sudo apt install python3-dev python3-pip python3-venv
    
  2. Install Emacs:
    sudo apt install emacs
    

    Note: If you prefer your editors terminal-based use the emacs-nox package.

  3. Set up MELPA:
    emacs ~/.emacs
    
    Add 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)
  4. Close and open Emacs.
  5. Refresh the package list:
    M-x package-refresh-contents
    
  6. Show the package list (M-x package-list-packages) and install the following packages:
    • elpy
    • flycheck
    • magit
    • poetry
  7. Configure Emacs:
    emacs ~/.emacs
    
    Add 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)
  8. Open a .py file.
  9. Set up the Elpy virtual environment:
    M-x elpy-rpc-reinstall-virtualenv
    
    You will be asked "Automatically install the RPC dependencies from PyPI", answer y

Notes

  • 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:
    1. M-x compile
    2. ./example.py The nice thing about this is you can jump straight to errors with:
    CTRL-x + `
    
  • Open Magit with:
    CTRL-x + g
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment