Skip to content

Instantly share code, notes, and snippets.

@jgarte
Forked from widdowquinn/emacs_python_ide.md
Created March 30, 2022 00:34
Show Gist options
  • Save jgarte/ac88f7daa4d06fd354257382a3c7a648 to your computer and use it in GitHub Desktop.
Save jgarte/ac88f7daa4d06fd354257382a3c7a648 to your computer and use it in GitHub Desktop.
Turning Emacs into a Python IDE

Turning emacs into a Python IDE

## Create a new config/initialisation file

Create a user-level initialisation file init.el:

touch .emacs.d/init.el

Install packages

Add the following to the init.el file:

;; init.el --- Emacs configuration

;; Set OSX function key as Meta


;; INSTALL PACKAGES
;; --------------------------------------

(require 'package)
(add-to-list 'package-archives                                                                                                        
             '("elpy" . "http://jorgenschaefer.github.io/packages/"))   
(add-to-list 'package-archives
       '("melpa" . "https://melpa.org/packages/") t)

;; activate all packages
(package-initialize)

;; fetch the list of packages available 
(unless package-archive-contents
  (package-refresh-contents))

;; define list of packages to install
(defvar myPackages
  '(better-defaults
    material-theme
    exec-path-from-shell
    elpy
    pyenv-mode))

;; install all packages in list
(mapc #'(lambda (package)
    (unless (package-installed-p package)
      (package-install package)))
      myPackages)

;; Use shell's $PATH
(exec-path-from-shell-copy-env "PATH")

;; init.el ends here

Here, better-defaults is a set of minor changes to Emacs defaults, and material-theme is an Emacs display theme.

We have to use the GitHub repo for elpy, or else an error is thrown with a variable xref-location1

If, on startup, packages can't be found, try M-x pacakge-refresh-contents.

Basic customisation

Add the following to the init.el file, after the INSTALL PACKAGES section:

;; BASIC CUSTOMIZATION
;; --------------------------------------

(setq inhibit-startup-message t)   ;; hide the startup message
(load-theme 'material t)           ;; load material theme
(global-linum-mode t)              ;; enable line numbers globally
(setq linum-format "%4d \u2502 ")  ;; format line number spacing
;; Allow hash to be entered  
(global-set-key (kbd "M-3") '(lambda () (interactive) (insert "#")))

This loads the material theme, turns off the startup message, and enables line numbering in all buffers.

Turn on elpy for Python development

Extend the list of packages to install, by adding elpy:

;; define list of packages to install
(defvar myPackages
  '(better-defaults
    material-theme
    elpy))

and add the commands to enable elpy and its interactions:

(elpy-enable)
(pyenv-mode)
(setq python-shell-interpreter "ipython"
      python-shell-interpreter-args "-i --simple-prompt")

To help elpy out, provide autocompletion and syntax checking/linting, install the following at the command-line (e.g. with pip):

jedi
autopep8
flake8
ipython
importmagic
yapf

Using the virtualenv Python

M-x pyvenv-activate

Then enter the name/location of the venv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment