Developer's blog

Go to Notes

Configure Emacs for Python development

I like to have the same development environment on the local machine and remote servers. In this case it’s possible to write and debug programs not only with your laptop, but also using lightweight tablet or netbook and at the same time have all stuff you have used to like fast CPU, enough memory and all the features of IDE like code navigation or autocompletion. I prefer using Emacs on the Ubuntu machine to achieve this goal.

Emacs GUI version

In this article I’ll show how to install and configure Emacs for Python development.

Assume you have minimal installation of Ubuntu 18.04. First of all, install python and dependencies:

sudo apt-get install python3 python3-pip python3-venv

Install Emacs (emacs package for desktop or emacs-nox for server):

sudo apt-get install emacs-nox

A lot of projects use git as a version control system, so it’s better to have it installed:

sudo apt-get install git

Emacs has it’s own package system package.el. It allows to download and install different Emacs extensions from the Internet. package.el allows to use different repositories with packages. One of the most popular is MELPA. Add such lines into ~/.emacs/init.el file to configure package.el:

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))

(package-initialize)

When Emacs installs packages it checks integrity using package signature. Unfortunately Emacs in Ubuntu 18.04 distribution doesn’t have public keys to verify MELPA packages, so if signature checking is enabled then you’ll have error like this during package installation:

Failed to verify signature archive-contents.sig:
No public key for 066DAFCB81E42C40
Command output:
gpg: Can't check signature: No public key

There are to options how to solve this problem:

  1. disable signature checking;
  2. update public keys.

The first option increases security risks, so we’ll use the second one.

Add such line at the beginning of ~/.emacs/init.el to disable signature checking temporarily:

(setq package-check-signature nil)

Then open Emacs, press M-x and type package-refresh-contents to download packages info. Press M-x and type package-install then enter package name gnu-elpa-keyring-update. This package updates the GPG keys used by Emacs package manager to verify authenticity of packages downloaded from the MELPA archive.

Then delete (setq package-check-signature nil) from Emacs config and restart editor.

Open Emacs, press M-x and type package-list-packages. Select packages:

To select package use i. After all packages have been selected use x to install them.

To enable and configure installed package add following lines into ~/.emacs/init.el:

;; Activate better-defaults package
(require 'better-defaults)

;; Activate zenburn theme
(load-theme 'zenburn t)

;; Enable line numbers for python files
(add-hook 'python-mode-hook 'linum-mode)
;; Insert space after line number to make it easier to read
(setq linum-format "%d ")

;; Use python3 for autocompletion and code navigation
(setq elpy-rpc-python-command "python3")
;; Use python3 as a interpreter in integrated python shell
(setq python-shell-interpreter "python3")

;; Activate elpy
(elpy-enable)

;; Activate projectile
(projectile-mode +1)
(define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)

Open some python file and start typing. Emacs will ask you for installing elpy dependencies required for autocompletion and code navigation. Install it and that’s it.

If you use Emacs from terminal make sure your TERM environment variable equals to xterm-256color. Terminal version looks almost the same as GUI one:

Emacs terminal version

Now you can write python code in Emacs using desktop or remote server. For remote server I recommend using tmux. It allows to make only one connection to the server and run multiple shells in it. Also if you use unstable Internet connection you will not loose any data in case of disconnect.

You may continue exploring the world of Emacs packages or try one of Emacs distributions like Prelude or Spacemacs. There are a lot of interesting staff.