Isort: third party libs not identified when running via tox

Created on 10 May 2018  路  9Comments  路  Source: PyCQA/isort

Isort is doing a good job when running directly, but it fails to identify third party libs when running through tox.

These are the relevant bits of my tox.ini:

[testenv:lint]
skipsdist = true
skip_install = true
deps =
    flake8
    isort
commands =
    flake8 my_app tests examples
    isort -c --recursive my_app tests examples

And setup.cfg:

[isort]
include_trailing_comment = True
known_first_party = my_app
# known_third_party = a_dependency, another_dependency
line_length = 99
lines_between_types = 0
multi_line_output = 4
not_skip = __init__.py
use_parentheses = True

I have seen this behavior in the past, and I know that uncommenting the known_third_party line resolves the issue, but it only does if I add there ALL my third party imports, which might be a long list.

Is this really the expected behavior, or is there something which I can configure differently to make this problem go away?

Most helpful comment

imo it is kind of a problem that isort needs to have the world installed just to sort some imports :man_shrugging: (imagine how much faster your tox env would be if you didn't have to install anything but isort)

All 9 comments

I actually have a potentially related problem where I have some known_third_party configured and I can successfully run isort in my project directory but it fails inside tox due to not picking up the configuration.

Just ran into this problem again today. I even tried to specify the settings path like so:

[testenv:isort]
skip_install = True
deps =
    isort
commands =
    isort -sp {toxinidir} -c -rc --diff {toxinidir}/src/package {toxinidir}/tests

That didn't change anything, though. I have an isort section in my setup.cfg and I use version 4.3.4.

I'm having the inverse situation, I think:

$ rm -fr .tox
$ grep isort tox.ini
[testenv:isort]
deps = isort
    which isort
    isort --version
    isort --check-only --recursive project tests
$ tox -e isort
/home/julien/project/.tox/isort/bin/isort
[...]
                            VERSION 4.3.4
[...]
isort runtests: commands[2] | isort --check-only --recursive project tests
ERROR: /home/julien/project/project/project.py Imports are incorrectly sorted.
ERROR: /home/julien/project/project/views.py Imports are incorrectly sorted.
ERROR: /home/julien/project/tests/test_idp.py Imports are incorrectly sorted.
ERROR: /home/julien/project/tests/test_main.py Imports are incorrectly sorted.
ERROR: /home/julien/project/tests/test_views.py Imports are incorrectly sorted.
ERROR: InvocationError for command '/home/julien/project/.tox/isort/bin/isort --check-only --recursive project tests' (exited with code 1)

$ isort --check-only --recursive project tests
Skipped 2 files

The diff, while running /home/julien/project/.tox/isort/bin/isort --check-only --recursive project tests --diff is that isort is moving my modules up, in the third party group, so my modules are no longer detected as first-party while running in tox.

You may find seed-isort-config useful. isort relies on dynamic analysis (essentially _importing_ your code) to determine whether imports are first party or not. The backing module for seed-isort-config does this statically.

I have the same issue which means I have to remove isort from my tox pipeline.

@jbasko @asottile @JulienPalard @Midnighter in my case, the problem was that the tox environment was not properly configured because of the skip_install flag and, in some cases, some other missing dependencies.

So, after digging a lot into it, this is the configuration that works best for me:

[testenv:lint]
skipsdist = true
extras = dev
commands =
    /usr/bin/env make lint   # this runs flake8 and isort.

Where dev is an entry in my setup.py which installs all the dependencies, including the development ones like isort or flake8.

So, the important points are:

  • do not use skip_install = true
  • make sure that all the third party dependencies are installed in this tox environment.

You can see a fully working configuration example here: https://github.com/HDI-Project/MLBlocks/blob/master/tox.ini

So, since this is not a problem with isort but just a configuration issue, I'm closing this for now.

imo it is kind of a problem that isort needs to have the world installed just to sort some imports :man_shrugging: (imagine how much faster your tox env would be if you didn't have to install anything but isort)

Ok, maybe I have a different issue then. @csala I have deps = -rrequirements.txt and requirements.txt include isort. I don't use skip_install = true.

isort runs in tox, but reports an issue with import order yet the order is fine when running isort in my virtualenv. In setup.cfg I'm listing three known_first_party items and I believe they are not picked up.

I had the same problem than @JulienPalard, I used the --project option to define what my first party actually is.

~~
-p KNOWN_FIRST_PARTY, --project KNOWN_FIRST_PARTY
Force sortImports to recognize a module as being part
of the current python project.
~
~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghickman picture ghickman  路  3Comments

ionelmc picture ionelmc  路  3Comments

kevindaum picture kevindaum  路  4Comments

pawamoy picture pawamoy  路  3Comments

and-semakin picture and-semakin  路  3Comments