{"id":26730,"library":"aspy.refactor_imports","title":"aspy.refactor_imports","description":"Utilities for refactoring imports in Python-like syntax. Provides functions to categorize, modify, and reformat import statements. Current version 3.0.2, requires Python >=3.7. Maintained by asottile with regular updates.","status":"active","version":"3.0.2","language":"python","source_language":"en","source_url":"https://github.com/asottile/aspy.refactor_imports","tags":["refactoring","imports","linter","code-quality","python"],"install":[{"cmd":"pip install aspy.refactor_imports","lang":"bash","label":"Latest PyPI release"}],"dependencies":[{"reason":"Core package","package":"aspy.refactor_imports","optional":false}],"imports":[{"note":"Package name uses dot notation, not underscore.","wrong":"from aspy_refactor_imports import import_module","symbol":"import_module","correct":"from aspy.refactor_imports import import_module"},{"note":"","symbol":"categorize_imports","correct":"from aspy.refactor_imports import categorize_imports"}],"quickstart":{"code":"from aspy.refactor_imports import categorize_imports, ImportType, classify_import\n\n# Example: categorize imports in a source string\nsource = '''import os\nimport sys\nfrom collections import OrderedDict\n'''\nimport_lines = source.splitlines()\ncategorized = categorize_imports(import_lines, lambda x: ImportType.THIRD_PARTY)\nprint(categorized)","lang":"python","description":"Demonstrates categorizing import lines into types using aspy.refactor_imports."},"warnings":[{"fix":"Replace calls to removed functions with new API.","message":"Version 3.0.0 removed deprecated functions: `import_type`, `import_descriptions`, etc. Use `categorize_imports` and `classify_import` instead.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure the classifier takes a single import line string and returns an `ImportType` enum member.","message":"The function `categorize_imports` expects a list of import strings and a classifier function returning `ImportType`. Misunderstanding the classifier signature leads to errors.","severity":"gotcha","affected_versions":"all"},{"fix":"Use `categorize_imports` directly.","message":"The `classify_import` function is deprecated in favor of using `categorize_imports` with a custom classifier.","severity":"deprecated","affected_versions":"<3.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Install as `pip install aspy.refactor_imports` and import as `from aspy.refactor_imports import ...`","cause":"Incorrect package name with underscores instead of dots.","error":"ModuleNotFoundError: No module named 'aspy_refactor_imports'"},{"fix":"Check documentation for current API; use `categorize_imports` instead.","cause":"Function may have been renamed or removed.","error":"ImportError: cannot import name 'import_module' from 'aspy.refactor_imports'"},{"fix":"Provide a classifier function: `categorize_imports(lines, my_classifier)` where `my_classifier` takes a string and returns an ImportType.","cause":"Missing classifier function argument.","error":"TypeError: categorize_imports() missing 1 required positional argument: 'classify'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}