Python editting

  1. Install Emacs.
  2. Install ELPY.
  3. Optional stuff.

1. Install GNU Emacs

OS X

Run:

brew update                     # Synchronize the local Homebrew database
brew install emacs --with-cocoa # Allows to start Emacs from the launchpad or from Spotlight
brew linkapps emacs             # To symlink Emacs to "/Applications"

Ubuntu

Run:

apt-get install emacs

Windows

Download it from "http://ftp.gnu.org/gnu/emacs/windows/ and install it.

2. Install ELPY (Emacs Lisp Python Environment)

ELPY is an Emacs package which provides Python's commands completion, indentation highlighting, snippet expansion, code hinting, code navigation, code refactoring, on-the-fly checks, virtualenv support and test running. Original documentation: https://elpy.readthedocs.io/en/latest/introduction.html#installation. Another interesting (and very comprehensive) resource is: https://realpython.com/emacs-the-best-python-editor/.

Update PATH

If you are using a Python enviroment, the shell PATH variable must include the "binary" files installed by pip. For example, if you are using pyenv, add

export PATH=/home/vruiz/.pyenv/versions/3.6.4/bin/:$PATH

to your shell configuration file (~/.bashrc, for example).

Install ELPY package from MELPA

In Emacs, run <Esc> x package-install <Enter> elpy <Enter>. Enable this package by including

(package-initialize)
(elpy-enable)

into your ~/.emacs Emacs configuration file. Restart Emacs.

Configure Elpy:

In Emacs, key-in:

<Esc> x elpy-config <Enter>

Elpy will check the Elpy-related Python packages which are available and will display a summary. At this moment, the following packages should be missing (and therefore, installed). After that, Emacs need to be reset (closed and open):

  1. Jedi, an autocompletion tool for Python that can be used for text editors:
    pip install jedi
  2. Rope, a Python refactoring:
    pip install rope
  3. Import Magick, add, remove and manage imports:
    pip install importmagic
  4. autopep8, automatically formats Python code to conform to the PEP 8 style guide:
    pip install autopep8
  5. YAPF, formats Python code to conform the clang-format style guide:
    pip install yapf
  6. Flake8, a source code checker:
    pip install flake8

3. Optional stuff

In UNIX environments, the Emacs configuration data should be located in the file $HOME/.emacs or in the file $HOME/.emacs.d/init.el. In Windows OS (if the HOME environment variable is not set), in C:/.emacs.d/init.el.

Install theme package (optional)

Run emacs and, type <Esc> x package-install <Enter> better-defaults <Enter>. Remember that autocompletation is always available! Install also material-theme. To enable these themes, add:

(setq inhibit-startup-message t) ;; hide the startup message
(load-theme 'material t) ;; load material theme
(global-linum-mode t) ;; enable line numbers globally

to your configuration file, and restart Emacs.

3. Jedi directly from Emacs

3.1. Install MELPA

Add this code to the Emacs configuration file: $HOME/.emacs:

;; Standard package.el + MELPA setup
;; (See also: https://github.com/milkypostman/melpa#usage)
(require 'package)
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)


;; Standard Jedi.el setting
(add-hook 'python-mode-hook 'jedi:setup)
(setq jedi:complete-on-dot t)

;; Type:
;;     M-x package-install RET jedi RET
;;     M-x jedi:install-server RET
;; Then open Python file.

In the shell:

sudo apt-get install virtualenv
# pip install epc # ... sometimes

In emacs:

M-x package-install RET jedi RET
M-x jedi:install-server RET