Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import name 'SortImports' from 'isort' #1273

Closed
Corfucinas opened this issue Jul 6, 2020 · 13 comments
Closed

Cannot import name 'SortImports' from 'isort' #1273

Corfucinas opened this issue Jul 6, 2020 · 13 comments
Labels
documentation Improvements or additions to documentation integration question Further information is requested

Comments

@Corfucinas
Copy link

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"

@timothycrosley
Copy link
Member

timothycrosley commented Jul 6, 2020

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: gforcada/flake8-isort#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.

@timothycrosley timothycrosley added the question Further information is requested label Jul 6, 2020
@hughsie
Copy link

hughsie commented Jul 6, 2020

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'

@jiasli
Copy link

jiasli commented Jul 7, 2020

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 pylint-dev/pylint#2773. Updating Pylint to the latest version fixes this issue for me.

@limburgher
Copy link

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.

@limburgher
Copy link

Pylint's solution pylint-dev/pylint#3725

tych0 added a commit to tych0/qtile that referenced this issue Jul 10, 2020
This pins isort to a < 5.0 version, since it's no longer API compatible and
flake8 is broken with >= 5.0.

See PyCQA/isort#1273

Signed-off-by: Tycho Andersen <[email protected]>
tych0 added a commit to qtile/qtile that referenced this issue Jul 10, 2020
This pins isort to a < 5.0 version, since it's no longer API compatible and
flake8 is broken with >= 5.0.

See PyCQA/isort#1273

Signed-off-by: Tycho Andersen <[email protected]>
@timothycrosley timothycrosley added the documentation Improvements or additions to documentation label Jul 15, 2020
ahawker added a commit to ahawker/ulid that referenced this issue Aug 10, 2020
@iamsaurabhc
Copy link

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

@timothycrosley
Copy link
Member

@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.

@timothycrosley
Copy link
Member

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

@adzenith
Copy link
Contributor

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.

@timothycrosley
Copy link
Member

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


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

@adzenith
Copy link
Contributor

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!

mristin added a commit to Parquery/pyicontract-lint that referenced this issue Aug 22, 2020
The pylint had to be updated to 2.6.0 since its dependencies
got broken (such as isort).

See also:
PyCQA/isort#1273
conda-forge/pylint-feedstock#29
@timothycrosley
Copy link
Member

🎉 Pylint has now released an update with isort 5 support!

mristin added a commit to Parquery/pyicontract-lint that referenced this issue Aug 22, 2020
The pylint had to be updated to 2.6.0 since its dependencies
got broken (such as isort).

See also:
PyCQA/isort#1273
conda-forge/pylint-feedstock#29
@timothycrosley
Copy link
Member

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

openstack-mirroring pushed a commit to openstack/python-cinderclient that referenced this issue Mar 3, 2021
The current version does not work anymore with the current
testing stack.
Due to the bump, the wrapper needed a few changes
(maybe it could be dropped at some point?

Version 2.3.0 (used by cinder right now) was considered too,
but it requires a specific version of isort:
PyCQA/isort#1273

Change-Id: I07fa32e7f49bde041d101a2d09860d0bc27acda0
openstack-mirroring pushed a commit to openstack/openstack that referenced this issue Mar 3, 2021
* Update python-cinderclient from branch 'master'
  to df32f94add3b2dfcb84c63d7e7bfc146f8f0313c
  - Merge "Bump pylint to 2.6.0"
  - Bump pylint to 2.6.0
    
    The current version does not work anymore with the current
    testing stack.
    Due to the bump, the wrapper needed a few changes
    (maybe it could be dropped at some point?
    
    Version 2.3.0 (used by cinder right now) was considered too,
    but it requires a specific version of isort:
    PyCQA/isort#1273
    
    Change-Id: I07fa32e7f49bde041d101a2d09860d0bc27acda0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation integration question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants