classify-imports
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.
Common errors
-
ModuleNotFoundError: No module named 'aspy.refactor_imports'
cause Attempting to import the library using its old package name after upgrading to version 4.x or installing the new package name.fixThe package was renamed. Use `from classify_imports import ...` instead. If you haven't installed `classify-imports`, install it with `pip install classify-imports`. -
Imports are not sorted as expected, e.g., application imports appearing before built-ins or third-party.
cause The default sorting order of `classify-imports` might not align with user expectations or project-specific style guides, or a module is misclassified.fixUnderstand 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.
Warnings
- breaking 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.
- gotcha 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.
Install
-
pip install classify-imports
Imports
- import_obj_from_str
from classify_imports import import_obj_from_str
- sort
from classify_imports import sort
- classify_base
from classify_imports import classify_base
- Classified
from classify_imports import Classified
Quickstart
from classify_imports import import_obj_from_str, sort, classify_base, Classified
import pprint
# Example 1: Splitting an import object
obj = import_obj_from_str('import foo, bar, baz')
split_imports = [str(i) for i in obj.split()]
print('Split Imports:')
print(split_imports)
# Example 2: Sorting and partitioning import objects
imports_to_sort = [
import_obj_from_str('from classify_imports import sort'),
import_obj_from_str('import sys'),
import_obj_from_str('from pyramid.view import view_config'),
import_obj_from_str('import cached_property'),
]
partitioned = sort(imports_to_sort)
print('\nSorted and Partitioned Imports:')
pprint.pprint(partitioned)
# Example 3: Classifying a module
print('\nModule Classification:')
print(f"'__future__' is: {classify_base('__future__')}")
print(f"'classify_imports' is: {classify_base('classify_imports')}")
print(f"'pyramid' is: {classify_base('pyramid')}")
print(f"'os' is: {classify_base('os')}")
print(f"Order of classifications: {Classified.order}")