Advice for Python users in 2018

When I look how scientists use Python, I often see bad habits and problems:

  • not fully switching to Python >= 3.6,
  • difficulties with using the system Python (so one needs sudo to install Python packages) and/or virtual environments,
  • not using IPython,
  • using only Jupyter and notebooks (see the presentation "I Don't Like Notebooks".
  • using a bad Python editor or good editors badly configured (for example with Vim or Emacs),

Still using Python 2.7 in 2018!

The first issue of Unix users still using Python 2.7 without any good reasons is mainly related to PEP 394 (the system python command points towards python2), which starts to be outdated.

My advice would be to NEVER use Python 2.7 for scientific code today, to stop to write code compatible with Python 2.7 and even, to use f-strings.

If you encounter a "Python 2.7" problem in one old script (for example a print without parenthesis), put as soon as possible a little bit of energy to update the code [1].

[1]Of course, there are some cases where it is more complicated than that...

For simple cases, no virtual environments

The problems with using the system Python or virtual environments can be solved by using pyenv and/or miniconda.

For simple usage, I would recommend NOT using virtual environments, which are complicated and error-prone. See this tutorial on how to setup Ubuntu 18.04 to use Python like this.

For advanced users, tools like virtualenv, pipenv and conda env are of course very useful.

Use IPython!

When you try code that you've just written, launch your scripts in IPython with the magic command %run.

pip install ipython
ipython
run path/towards/myscript.py

It is then possible to interact with the objects and also to use the magic command %debug if you get an error.

Editors

One must use a good editor to write Python code! Coding in Python with a bad editor is just a nightmare. Don't do that!

There are many editors which somehow support Python coding and I'm going to recommend just few editors/IDEs.

Visual Studio Code

I used to use Emacs (with this configuration) and few weeks ago, I started to use more and more Visual Studio Code with its Python extension (with this configuration).

For developping a Python package, it is indeed very good and it really improves my productivity and even the quality of the code, see for example this blog post by Kenneth Reitz

On Unix, one may want to avoid using a binary build by Microsoft so we can use vscodium.

Let's say that there are of course things that could be improved in VSCode, as for example the behavior of the tab key.

Simple IDEs for scientists

For scientists and for simpler programming (only scripts), other tools, as Spyder, Pyzo or Eric should be more adapted.

Jupyter-lab

For interactive data processing (and not for everything), JupyterLab starts to be a great tool.