Isort: Cannot import name 'SortImports' from 'isort'

Created on 6 Jul 2020  路  13Comments  路  Source: PyCQA/isort

Hi,

I recently updated ISORT using Poetry on a clean virtualenv. I'm getting the following error when I tried to run Flake8.

flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "I00" due to cannot import name 'SortImports' from 'isort' (d:\github\crypto-candlesticks\.venv\lib\site-packages\isort\__init__.py).

Kindly let me know if there's additional information you may need, these are the virtualenv packages.


Packages

[tool.poetry.dev-dependencies]

black = "^19.10b0"
mypy = "^0.782"
sphinx = "^3.1.1"
pytest = "^5.4.3"
coverage = {extras = ["toml"], version = "^5.1"}
pytest-cov = "^2.10.0"
pytest-mock = "^3.1.1"
sphinx-autodoc-typehints = "^1.11.0"
nox = "^2020.5.24"
flake8-annotations = "^2.1.0"
flake8-bandit = "^2.1.2"
flake8-black = "^0.2.0"
flake8-docstrings = "^1.5.0"
flake8-import-order = "^0.18.1"
safety = "^1.9.0"
darglint = "^1.4.1"
wemake-python-styleguide = "^0.14.0"
xdoctest = "^0.12.0"
bump2version = "^1.0.0"
sphinx_autodoc_typehints = "^1.11.0"
isort = "^5.0.3"

documentation integration question

Most helpful comment

This also affects pylint:

  File "/usr/local/lib/python3.6/site-packages/pylint/checkers/imports.py", line 712, in _check_imports_order
    isort_obj = isort.SortImports(
AttributeError: module 'isort' has no attribute 'SortImports'

All 13 comments

isort 5.0.0 is not API compatible with isort 4.0.0. In theory, since isort follows the commonly used semantic versioning scheme this shouldn't automatically break things as they pin to <=4.0.0 until they are ready. Some plugins have not, such as flake8-isort: https://github.com/gforcada/flake8-isort/issues/88. In time all plugins should come over to the new version, and I will be looking at any opportunities to help them. In the meantime you can choose to either disable the offending plugins or hold off on the major isort release.

This also affects pylint:

  File "/usr/local/lib/python3.6/site-packages/pylint/checkers/imports.py", line 712, in _check_imports_order
    isort_obj = isort.SortImports(
AttributeError: module 'isort' has no attribute 'SortImports'

This also affects pylint:

  File "/usr/local/lib/python3.6/site-packages/pylint/checkers/imports.py", line 712, in _check_imports_order
    isort_obj = isort.SortImports(
AttributeError: module 'isort' has no attribute 'SortImports'

Pylint has pinned the version in https://github.com/PyCQA/pylint/pull/2773. Updating Pylint to the latest version fixes this issue for me.

Since Fedora rawhide now has isort 5.0.5, pylint 2.5.3 is broken. The affected portion is in pylint/checkers/imports.py,

isort_obj = isort.SortImports(
            file_contents="",
            known_third_party=self.config.known_third_party,
            known_standard_library=self.config.known_standard_library,
        )

I can submit a patch but I'm not even sure where to start.

latest isort 5.x is giving the error, add the following to your requirements.txt : isort==4.3.21. Solved the error for me.

@iamsaurabhc, it is not a bug with isort but a bug with the integration. Presuming you want all the niceties that come from isort 5, pinning 4.3.21 isn't a solution. However, it is a very reasonable stop gap until the compatible versions of dependent projects are released. This issue will remain open until the major dependent projects are updated.

:tada: flake8-isort has included isort 5 support in their latest release!

The nice thing about SortImports is that it returned an object that you could use to find out things like what imports the given file is using. Is there a way in the 5.0 API to find out what the imports are in a given file? I poked around a little bit and I didn't see a straightforward way.

@adzenith I think isort.parse.file_contents should give you exactly what you want:

```import isort

isort.parse.file_contents("import os")


See: https://timothycrosley.github.io/isort/reference/isort/parse/#parsedcontent

It returns the parsing results in the following format:

class ParsedContent(NamedTuple):
in_lines: List[str]
lines_without_imports: List[str]
import_index: int
place_imports: Dict[str, List[str]]
import_placements: Dict[str, str]
as_map: Dict[str, Dict[str, List[str]]]
imports: Dict[str, Dict[str, Any]]
categorized_comments: "CommentsDict"
change_count: int
original_line_count: int
line_separator: str
sections: Any

```
Let me know if this is missing anything you would like to see, or if any other APIs would be useful for you. Also, it's always great to get a sense of how people are using the APIs, so if you wouldn't mind sharing that would be helpful!

If you would like to get a sense of the why behind the change, you can read a bit of background information on my blog: https://timothycrosley.com/project-8-isort5

Thanks, and I hope this is helpful!

~Timothy

Thanks for the super fast reply! This is exactly what I need. I was using SortedImports.imports, and this ParsedContent.imports looks to be exactly identical. I can just swap it out.
We're using isort to sort our imports, and also using the API to figure out dependencies between scripts. Using isort to get the imports makes it very easy!
Thanks again!

:tada: Pylint has now released an update with isort 5 support!

Closing this now, as I believe most notable projects that utilize isort have now been updated to include isort 5 compatibility.

For those that stumble into this issue:

If you run into this error with the latest version of a library, utility, or editor that uses isort please feel empowered to open a new issue or comment here. If you maintain a project that uses isort and would like assistance in moving over please reach out or open an issue.

Thanks everyone!

~Timothy

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevindaum picture kevindaum  路  4Comments

lee-kagiso picture lee-kagiso  路  4Comments

ionelmc picture ionelmc  路  3Comments

jack1142 picture jack1142  路  3Comments

darkclouder picture darkclouder  路  3Comments