comment 0

Best way to organize your Python Import statements

importsThere is a highly strategic thing to do when dealing with multi-developer and multi-branch project, especially if you indent to start automating the merge between branches: enforce a strict structure of the import statement in you Python script, and especially you import one and only one element per line. Here is why.

I was working on a large Python project which was using several branches:

  • main: where developers push there daily modification. This is equivalent to having master holding all brand new features.
  • staging: frozen version of the main, for validation
  • prod: the production branch

Having branch both for prod and validation allows late patches to be applied, like for example typo fixes, minor bugs fixes, etc.

However, when we fix an issue on, for example, staging, we want also this fix on main. Also, when a late bug fix is merged on prod, we want it in staging and in main.

We have implemented a system that performs automatically the merge from prod to staging and then from staging to prod.

drawit-diagram-14

This automatic merge does marvelously most of the job but there could be some conflict we have to resolve manually.

However we found out that most of the conflicts where actually happening on the head of the Python files, where are located the import statements.

Here is an example. Let’s say you have in all branches the same code and you import several module on the same line:

Because you fix an issue on the bottom on the file, you add some new imports on the prod branch:

At the same time on main, some else adds another import:

As you can see, there will be a conflict on line 2, because logging have been added by one developer on prod branch, while re has been added by another on on main.

Please note that we have the same problem with from...import syntax.

Because we are developer, we want to focus on interesting challenge. Manually merge this kind of obvious conflict is too time consuming.

We end up enforcing a simple rule:

import one and only one object per line

Actually we also enforce this obvious one:

organize (split and sort) imports statement following PEP8 imports order recommendation.

In other words, we want:

instead of:

It is more verbose, yes, BUT, merges like I’ve just described will be obvious for git merge.

From the PEP8 recommendation about import statement order and organisation, we changed in the following block:

Imports should usually be on separate lines, e.g.:

It’s okay to say this though:

to

Imports should be on separate lines and alphabetically sorted, e.g.:

Same applies to:

Also, since developers has no choice where to put an import statement, when 2 developers on two branches add the same import, it will be placed at the same time, so merge will also be obvious.

We have writen a fiximport module for Python, and a Sublime Text plugin to automatically sort and split import statements. There is a pretty good pypi package that can do it and also completely reorganise the import statements: isort. It worth a try.