Search code examples
pythonisortpylama

Checking for imports order with pylama


I use autoflake to automatically remove unused imports and variables from my code locally and pylama on Jenkins CI to check the code for multiple things, including unused imports and imported variables (W0611).

Now I would like to introduce isort to the project to sort imports.

Is there a way for pylama to check if imports are properly sorted?


Solution

  • I found isort has already pylama plugin. It's implemented in isort repository and mentioned in pylama help output:

    pylama -h
      ...
      --linters LINTERS, -l LINTERS
                            Select linters. (comma-separated). Choices are mccabe,
                            pep257,pydocstyle,pep8,pycodestyle,pyflakes,isort.
    

    It's not enabled by default so you need to configure pylama explicitly

    pylama --linters isort file_to_test
    

    or in pylama.ini configuration file

    [pylama]
    linters = isort
    

    (Default linters are pycodestyle, pyflakes, mccabe).

    In contrast to check -c it doesn't provide information which lines are incorrectly imported. If something is wrong it always shows same error message:

    file_to_test.py:0: [I] Incorrectly sorted imports. [isort]