isort Documentation

repository·main·Indexed 27 days ago

https://github.com/pycqa/isort

isort is a Python utility and library used to sort imports alphabetically and automatically separate them into sections and by type. It can be used as a CLI tool, integrated into text editors and pre-commit hooks, or used as a Python library via isort.file() and isort.code(). The tool supports extensive configuration via pyproject.toml, .isort.cfg, setup.cfg, and other formats, offering features like Black compatibility, custom import sections, and action comments to skip or split specific imports.

Tokens
18.1K
Snippets
66
Records
188
Agent score
92%

What's inside isort

  1. Try isort in a browser-based editor

    main

    You can test isort's import sorting capabilities without installing it locally by using the live isort editor. This editor runs a complete Python 3 installation with isort inside your browser using Pyodide, ensuring that no code is transmitted to any server.

    To use the live tester:

    1. Paste your Python imports into the input editor.
    2. Provide configuration settings in the configuration editor using JSON format.
    3. View the formatted output in the output editor.
  2. Create new sort groupings with isort: split

    main

    Use # isort: split to signal that the current sort section is finished. All subsequent imports will be placed in a new sort grouping. You can also use it inline to prevent imports from swapping positions with those above or below them.

    import e
    import f
    
    # isort: split
    
    import a
    import b
    import c
    import d
    
    # Inline usage example:
    import c
    import b  # isort: split
    import a
  3. Run isort for multiple projects

    main

    Because isort creates an immutable configuration for each CLI instance, you should not run a single command across multiple independent projects. Instead, execute a separate isort command for each project to ensure the correct configuration is applied to each.

    # RECOMMENDED: Run separately
    isort project1
    isort project2
    
    # RECOMMENDED: Run per project subdirectories
    isort project1/src project1/test
    isort project2/src project2/test
    
    # NOT RECOMMENDED: Do not mix projects in one command
    isort project1 project2
  4. Skip processing specific imports or files

    main

    You can prevent isort from touching specific parts of your code using comments:

    • Single import: Add # isort:skip to the end of the import line.
    • Multi-line import: Add # isort:skip inside the parentheses.
    • Entire file: Add isort:skip_file to the module's docstring.
    import module  # isort:skip
    
    from xyz import (abc,  # isort:skip
                     yo,
                     hey)
    
    """ my_module.py
        Best module ever
    
       isort:skip_file
    """
    import b
    import a
  5. Use custom configuration files

    main

    You can use a custom-named configuration file by specifying the path with the --settings-path flag.

    Important: When using custom config files, always place settings inside an [isort] section rather than a [settings] section to avoid conflicts with other tools that might use the same file.

  6. Migrate to isort 5.0.0

    main

    isort 5.0.0 is a major release with breaking changes. Key changes include:

    • Import Placement: By default, isort no longer moves imports to the top of the file to allow for side effects between imports. To restore the old behavior, use the --float-to-top CLI flag or float_to_top=true in your config.
    • Config Loading: isort 5 no longer merges multiple configuration files. You must use exactly one configuration file per project. Config files are loaded relative to the file being sorted. To specify a manual location, use the --settings-path flag.
    • Pre-commit: If using pre-commit, remove any seed-isort-config steps as they are unnecessary and can cause conflicts.
  7. Supported isort configuration files

    main

    isort searches for configuration files by traversing up to 25 parent directories from the target file. It stops at the first suitable file it finds and does not merge multiple configuration files. The search is relative to the current directory if isort . is used, or relative to the first path passed if multiple paths are provided. isort will not leave a git or Mercurial repository (it stops at .git or .hg directories).

    To see which configuration file is being used and what settings are applied, run: isort . --show-config

    isort . --show-config
  8. Integrate isort into a Git pre-commit hook

    main

    You can use isort.hooks.git_hook within your .git/hooks/pre-commit script to automatically check or format Python imports before a commit is finalized.

    Strict Mode (Fail on errors)

    To prevent a commit if isort finds errors (strict mode), use the following configuration in your .git/hooks/pre-commit file:

    #!/usr/bin/env python
    import sys
    from isort.hooks import git_hook
    
    sys.exit(git_hook(strict=True, modify=True, lazy=True, settings_file=""))

    Warning Mode (Allow commit despite errors)

    • Display warnings only: Call git_hook without the strict parameter.
    • Display warnings without fixing code: Call git_hook without the modify parameter.

    Argument Reference

    • strict: If True, the commit fails if imports are not correctly sorted.
    • modify: If True, isort will automatically fix the files. If False, it only checks them.
    • lazy: If True, it ensures all tracked files are properly isorted (useful for users who use git commit -a). If False or omitted, it only checks files currently added to the index.
    • settings_file: The path to a specific configuration file. If empty (""), isort searches upward from the directory of the first staged file until a valid config is found.
    #!/usr/bin/env python
    import sys
    from isort.hooks import git_hook
    
    sys.exit(git_hook(strict=True, modify=True, lazy=True, settings_file=""))