Isort: use isort in tox, my package is formatted third party

Created on 1 Jul 2020  Β·  3Comments  Β·  Source: PyCQA/isort

Hello, When I run ISort in Tox, my project package will be judged as a third-party package, but when I run ISort directly, there is no problem. Is there anything wrong? It's also normal when I use Pipenv with tox.

The scene is reproduced as follows

Project details

project tree

(demo)  kevin@kevin ξ‚° /tmp/test/demo ξ‚° ξ‚  master ξ‚° tree                   
.
β”œβ”€β”€ demo
β”‚   β”œβ”€β”€ cmd.py
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── server.py
β”œβ”€β”€ Pipfile
β”œβ”€β”€ Pipfile.lock
β”œβ”€β”€ setup.cfg
β”œβ”€β”€ setup.py
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── test_server.py
└── tox.ini

2 directories, 10 files

file detail

demo/server.py

import os

import uvicorn

from demo.server import app


def run():
    uvicorn.run(
        app
    )

test/test_server.py

import os

import pytest
from fastapi.testclient import TestClient

from demo.server import app


def test_server():
    client = TestClient(app)
    resp = client.get('/')
    assert resp.status_code == 200

setup.py

from os import path

from setuptools import find_packages, setup

setup(
    name='demo',
    version='1.0.1',
    # package_dir={'': 'src'},
    # packages=find_packages(where='src'),
    python_requires='>=3.6, <4',
    entry_points={
        'console_scripts': [
            'demo=demo.cmd:run'
        ],
    },
)

setup.cfg

[isort]
not_skip = __init__.py
skip =
    .tox

[tool:pytest]
testpaths = tests
python_files = tests.py test_*.py *_tests.py

tox.ini


[tox]
envlist = py37

[testenv]
skipsdist = true
skip_install = true
usedevelop = false
changedir = {toxinidir}
deps = 
    ; pipenv
    isort
commands = 
    ; pipenv install -d --skip-lock
    isort --recursive --check-only --diff demo tests

Run detail

run isort only

(demo)  kevin@kevin ξ‚° /tmp/test/demo ξ‚° ξ‚  master ξ‚° isort
Skipped 2 files

run isort in tox

(demo)  kevin@kevin ξ‚° /tmp/test/demo ξ‚° ξ‚  master ξ‚° rm -rf .tox 
(demo)  kevin@kevin ξ‚° /tmp/test/demo ξ‚° ξ‚  master ξ‚° tox
py37 create: /tmp/test/demo/.tox/py37
py37 installdeps: isort
py37 installed: isort==4.3.21
py37 run-test-pre: PYTHONHASHSEED='1399069139'
py37 run-test: commands[0] | isort --recursive --check-only --diff demo tests
ERROR: /tmp/test/demo/demo/cmd.py Imports are incorrectly sorted.
--- /tmp/test/demo/demo/cmd.py:before   2020-07-01 17:42:38.270308
+++ /tmp/test/demo/demo/cmd.py:after    2020-07-01 19:30:40.169042
@@ -1,7 +1,6 @@
 import os

 import uvicorn
-
 from demo.server import app


ERROR: /tmp/test/demo/tests/test_server.py Imports are incorrectly sorted.
--- /tmp/test/demo/tests/test_server.py:before  2020-07-01 17:45:35.386590
+++ /tmp/test/demo/tests/test_server.py:after   2020-07-01 19:30:40.172157
@@ -1,9 +1,8 @@
 import os

 import pytest
+from demo.server import app
 from fastapi.testclient import TestClient
-
-from demo.server import app


 def test_server():
ERROR: InvocationError for command /tmp/test/demo/.tox/py37/bin/isort --recursive --check-only --diff demo tests (exited with code 1)
________________________________________________________________________________________________________ summary ________________________________________________________________________________________________________
ERROR:   py37: commands failed
(demo)  ✘ kevin@kevin ξ‚° /tmp/test/demo ξ‚° ξ‚  master ξ‚° 

run isort and piprnv in tox

Pipfile

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
isort = "*"
pytest = "*"

[packages]
fastapi = "*"
uvicorn = "*"

Run pipenv first, and run isort

tox.ini


[tox]
envlist = py37

[testenv]
skip_install = true
usedevelop = false
changedir = {toxinidir}
deps = 
    pipenv
    isort
commands = 
    pipenv install -d --skip-lock
    isort --recursive --check-only --diff demo tests

Result

(demo)  kevin@kevin ξ‚° /tmp/test/demo ξ‚° ξ‚  master ● ξ‚° rm -rf .tox
(demo)  kevin@kevin ξ‚° /tmp/test/demo ξ‚° ξ‚  master ● ξ‚° 
(demo)  kevin@kevin ξ‚° /tmp/test/demo ξ‚° ξ‚  master ● ξ‚° tox
py37 create: /tmp/test/demo/.tox/py37
py37 installdeps: pipenv, isort
py37 installed: appdirs==1.4.4,certifi==2020.6.20,distlib==0.3.1,filelock==3.0.12,importlib-metadata==1.7.0,isort==4.3.21,pipenv==2020.6.2,six==1.15.0,virtualenv==20.0.25,virtualenv-clone==0.5.4,zipp==3.1.0
py37 run-test-pre: PYTHONHASHSEED='2834522456'
py37 run-test: commands[0] | pipenv install -d --skip-lock
Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You can set PIPENV_VERBOSITY=-1 to suppress this warning.
Installing dependencies from Pipfile…
  🐍   β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰ 3/3 β€” 00:00:00
py37 run-test: commands[1] | isort --recursive --check-only --diff demo tests
________________________________________________________________________________________________________ summary ________________________________________________________________________________________________________
  py37: commands succeeded
  congratulations :)

summary

I don't know why. Even though 'pipenv' is possible, I want to break 'isort' separately from 'tox'. I'm trying to find a Python project template that works for me, but this problem is holding me back.

Thank you very much if you can help me.

The source code for this demo project is here: demo.

question

All 3 comments

Can you see if this issue still happens for you in the 5.0.0 release I just pushed out? https://timothycrosley.github.io/isort/docs/major_releases/introducing_isort_5/. Environmental inconsistencies was one of the major problems I aimed to squash in the new release

I tested the new version 5.0.2 successfully. And the github action I added to the demo project was successful. Very happy. Thank you for your help.

This was tested in my laptop's Windows environment. On Monday I will update Linux on my working development machine.

(demo) E:\workspace\develop\python\demo>rm -rf .tox

(demo) E:\workspace\develop\python\demo>tox
py37 create: E:\workspace\develop\python\demo\.tox\py37
py37 installdeps: isort
py37 installed: isort==5.0.2
py37 run-test-pre: PYTHONHASHSEED='736'
py37 run-test: commands[0] | isort --check-only --diff --src demo tests
______________________________________________________________________________________________________ summary ______________________________________________________________________________________________________ 
  py37: commands succeeded
  congratulations :)

hello, @timothycrosley .

I found a problem while using isort today.

Since I use pytest , conftest.py file exists in the tests directory to define the test configuration. When I refer to the contents of this file in the tests file, when I run isort in the tox, it will treat this part of the content as a third-party package, hoping to make adjustments to this problem later. Thank you.

This is the demo project's action result. Action is use the latest version 5.0.4.

I cannot reopen this issue, so should I make a new one ?

I sincerely look forward to your reply.

Was this page helpful?
0 / 5 - 0 ratings