Jutting Bytes

Digressions of a research engineer

Setting Up Emacs as Python Development Environment.

| Comments

I have recently started to investigate a project using Python as its main language. Since I have become found of both flycheck and company-mode (especially using irony-mode), I wanted to enrich my configuration from my Emacs’ prolusion to take advandantage of on-the-fly checking and auto-completion. Here is how to do so in a minimalistic manner.

First of all, for the embedded python interpreter to behave as excpected, you need to have set up some environment variables considering locale in either your ~/.bashrc or ~/.bash_profile, whether your or working on Linux on MacOS.

1
2
3
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

Using MacOS, you’ll have to import them in your Emacs environment using the excellent exec-path-from-shell.

1
2
3
4
(when (memq window-system '(mac ns))
  (setq exec-path-from-shell-arguments (quote ("-l")))
  (setq exec-path-from-shell-variables (quote ("PATH" "MANPATH" "CMAKE_PREFIX_PATH" "LC_ALL" "LANG" "LC_CTYPE")))
       (exec-path-from-shell-initialize))

Now, we need to respectively add the requirements and configure them as you wish.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Completion requirements
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(prolusion-require-package 'company)
(prolusion-require-package 'company-jedi)

;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Completion configuration
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq jedi:environment-root (expand-file-name "prolusion-environment-root" prolusion-save-dir))
(setq python-environment-directory (expand-file-name "prolusion-environment" prolusion-save-dir))
(setq python-environment-default-root-name "root")
(setq jedi:complete-on-dot t)

(eval-after-load 'company '(add-to-list 'company-backends 'company-jedi))

(add-hook 'python-mode-hook 'company-mode)

This will give you completion for system-wide python packages. Note that I have not yet tested it with third-party once, such as those that come with e.g. conda virtual environments.

Now for the on-the-fly checking.

1
(add-hook 'python-mode-hook 'flycheck-mode)

Find out the whole Emacs’s configuration here: https://github.com/jwintz/prolusion.

Note that the first step to get python’s jedi server running is to run:

1
M-x jedi:install-server

And that this requires virtualenv to be installed and referred by your $PATH variable.

I installed it using my system wide python install’s pip (via brew install python), which implies to be careful when using conda virtual environments that come together with their own python installation by using source activate which alters your PATH environment variable. Not to mention that this overrides MacOS’s default python installation.

1
$ pip install virtualenv

Comments