Emacs Python

Emacs for Python

Python is all the rage for data scientists and web developers alike but how do you start? The best way to learn to program is to try out functions and ideas yourself. With Python, you have a great tool available by default: the interpreter. You can actually use it as a shell for everyday tasks. In the interpreter you can try the mathematics, create your ‘Hello World!’ and even define some functions. This is an excellent way to create a specific function before you start using your editor or IDE. If you want the full REPL, use Ipython or even better a Jupyter notebook. In a Jupyter Notebook, you can mix code with comment sections and even the results from your code. There are many examples of this online.

When you are ready to start a full project, you have a few things to consider. First, you need to know what software you need. For each project, you will probably use different libraries and levels. In Python, this has been a special point of attention after the 3.x series was introduced. Some functions from 2.x are not supported by 3.x, because of this situation virtual environments was invented. Virtual environments are useful for making sure you use the particular libraries you expect and nothing else.

When you choose IDE, or editor, you want to support these virtual environments to make your work simpler. You also need to have an easy way to test your latest patches. Other things you want at your fingertips are documentation, code completion and a testing environment.

In Emacs, you have support for Python code highlighting from a vanilla install. When you want more, there are packages available for these functions and a few more. The philosophy of Emacs is that you will not need to leave the editor to keep doing your work. To achieve this, there are shells, a REPL and even support to have your Jupyter notebook inside the editor. You can also start a web server and use your browser to see what your latest code has done for your web page.

Installing support

Flycheck is a general tool for keeping your spelling correct, while it also suggests completions during typing. To install, you need to first install from MELPA. The command is ‘M-x package-install flycheck-pyflakes’. You can also choose Flake8 or Pylint.

ELPY is a collection of Python features for emacs. It binds together ROPE, jedi, Yapf, black and the most important; You can choose your syntax checker. You start installing Elpy from MELPA, using package-manager. After this job is done, you need to add the modules in your Python environment. Please, use a virtual environment! To install these packages, you would ordinarily use pip in a shell. With Elpy, though, you can just start the built-in configuration command ‘M-x elpy-config’. You will see a buffer that contains a list of all the programs that are supported. The list also shows which programs are installed and available. The great part is that you also have links to install the programs in your environment.

Figure 1: Configuring Elpy for Emacs

 

You also have the option to add extensions for Elpy. One extension supports Django and its classes. Other tools you may want to have are ein, this one gives you the ability to create, read and edit Jupyter notebooks. To install, use the package manager as usual (M-x package-install ein). You can open notebooks that are local and remote.

Figure 2: Jupyter Server Running

Running the interpreter inside

When you are writing your code, you may want to try out short code snippets like you would on the command line. With emacs, you can start the Python interpreter while having Python code buffer open. The ‘C-c C-p’ key chord will open the interpreter in a new window.

Figure 3: The Python interpreter running inside Emacs

Here, you can do all the things you can do in the regular Python interpreter. In fact, it is the interpreter, only it is embedded inside Emacs. You can also have the interpreter window open and test run the code form the other window.

Code completion

To handle code completion and document lookup you can use anaconda-mode, this mode is commonly installed as a dependency to company. Company is a code-completion system that has modules for many different programming languages.

Snippets

When you are coding, you many times need to type the same text with small variations. Functions have a special format, for example. To save time you can have snippets available that gives you the structure needed. Many of the parts you then fill in like a form. To call a snippet, you start typing ‘class’ for example and then hit the ‘TAB’ key. You now have a class ready for you to fill in. The community have written yasnippets and distributed them freely. You can also write your own to match your needs.

Pre-built solutions

There are several pre-built solutions called distributions. One is Prelude, you get the distribution by downloading the settings files. The best way to track the distribution is to use their GitHub repositories. Every time you believe they have some great improvement, update using ‘git pull’. A newly installed Prelude does not have support for much at all, this is a deliberate strategy to allow you to choose what you want to work with. To make your copy work for Python, remove the comment mark at the correct line in your personal/prelude-modules.el file. Prelude will compile and use the setting after you next start Emacs. All your personal settings are stored in the personal/ directory. You most likely will add your own yasnippets here and any customisation you make will also go here. Prelude has a good balance between making configuration easier and letting the user stay in control.

Emacs has many functions to help you write code in Python, in fact the tools cover all parts of developing software. You do not really need much else than emacs and the Python environment set-up to finish an entire project.

About the author

Mats Tage Axelsson

Mats Tage Axelsson

I am a freelance writer for Linux magazines. I enjoy finding out what is possible under Linux and how we can all chip in to improve it. I also cover renewable energy and the new way the grid operates. You can find more of my writing on my blog.