{"id":7083,"library":"classify-imports","title":"classify-imports","description":"classify-imports is a Python utility library designed for refactoring imports in Python-like syntax. It provides tools to parse import statements, classify them into categories (e.g., built-in, third-party, application), and sort them according to defined rules. The library is currently at version 4.2.0 and is actively maintained with a focus on import organization.","status":"active","version":"4.2.0","language":"en","source_language":"en","source_url":"https://github.com/asottile/classify-imports","tags":["refactoring","python","imports","code-quality","linting","sorting"],"install":[{"cmd":"pip install classify-imports","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"Used to parse a string representation of an import statement into an import object.","symbol":"import_obj_from_str","correct":"from classify_imports import import_obj_from_str"},{"note":"Used to sort a list of import objects, typically partitioning them into blocks.","symbol":"sort","correct":"from classify_imports import sort"},{"note":"Provides a function to determine the base classification (e.g., 'BUILTIN', 'THIRD_PARTY', 'APPLICATION') for a module name.","symbol":"classify_base","correct":"from classify_imports import classify_base"},{"note":"An Enum-like object containing predefined classification types and their default order.","symbol":"Classified","correct":"from classify_imports import Classified"}],"quickstart":{"code":"from classify_imports import import_obj_from_str, sort, classify_base, Classified\nimport pprint\n\n# Example 1: Splitting an import object\nobj = import_obj_from_str('import foo, bar, baz')\nsplit_imports = [str(i) for i in obj.split()]\nprint('Split Imports:')\nprint(split_imports)\n\n# Example 2: Sorting and partitioning import objects\nimports_to_sort = [\n    import_obj_from_str('from classify_imports import sort'),\n    import_obj_from_str('import sys'),\n    import_obj_from_str('from pyramid.view import view_config'),\n    import_obj_from_str('import cached_property'),\n]\n\npartitioned = sort(imports_to_sort)\nprint('\\nSorted and Partitioned Imports:')\npprint.pprint(partitioned)\n\n# Example 3: Classifying a module\nprint('\\nModule Classification:')\nprint(f\"'__future__' is: {classify_base('__future__')}\")\nprint(f\"'classify_imports' is: {classify_base('classify_imports')}\")\nprint(f\"'pyramid' is: {classify_base('pyramid')}\")\nprint(f\"'os' is: {classify_base('os')}\")\nprint(f\"Order of classifications: {Classified.order}\")","lang":"python","description":"This quickstart demonstrates the core functionalities of `classify-imports`: parsing a single import string into multiple, sorting a list of import objects into classified blocks, and determining the classification type of a module name. The `sort` function will arrange imports into 'FUTURE', 'BUILTIN', 'THIRD_PARTY', and 'APPLICATION' categories by default."},"warnings":[{"fix":"Update `pip install aspy.refactor-imports` to `pip install classify-imports` and change all import statements from `from aspy.refactor_imports import ...` to `from classify_imports import ...`.","message":"The library was rewritten and renamed from `aspy.refactor-imports` to `classify-imports` starting with version 4. Users upgrading from version 3.x or older of `aspy.refactor-imports` must update their import paths and potentially their usage patterns.","severity":"breaking","affected_versions":"<4.0.0"},{"fix":"Review the documentation for configuring application import paths and names if using in a larger project or with tools like `reorder-python-imports` that rely on its classification. Explicitly configure known application prefixes or modules if default heuristics are insufficient.","message":"The `classify_base` function and `sort` utility classify modules based on heuristics (e.g., stdlib list, known third-party patterns, or application imports relative to the project). Incorrectly configured application import names or paths can lead to modules being misclassified as third-party instead of application-specific, or vice-versa.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"The package was renamed. Use `from classify_imports import ...` instead. If you haven't installed `classify-imports`, install it with `pip install classify-imports`.","cause":"Attempting to import the library using its old package name after upgrading to version 4.x or installing the new package name.","error":"ModuleNotFoundError: No module named 'aspy.refactor_imports'"},{"fix":"Understand the default `Classified.order` ('FUTURE', 'BUILTIN', 'THIRD_PARTY', 'APPLICATION'). If a module is misclassified, ensure your environment or calling tool (like `reorder-python-imports`) correctly identifies application vs. third-party modules. Customization options might be available in tools integrating `classify-imports` to override the default order or classification logic.","cause":"The default sorting order of `classify-imports` might not align with user expectations or project-specific style guides, or a module is misclassified.","error":"Imports are not sorted as expected, e.g., application imports appearing before built-ins or third-party."}]}