{"id":14235379,"url":"https://github.com/alecthomas/importmagic","last_synced_at":"2025-04-05T23:11:03.877Z","repository":{"id":14858547,"uuid":"17581811","full_name":"alecthomas/importmagic","owner":"alecthomas","description":"A Python library for finding unresolved symbols in Python code, and the corresponding imports","archived":false,"fork":false,"pushed_at":"2023-12-02T07:10:26.000Z","size":1116,"stargazers_count":120,"open_issues_count":19,"forks_count":21,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-29T22:11:13.300Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"tsurupin/portfolio","license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alecthomas.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-03-10T04:34:00.000Z","updated_at":"2024-04-11T04:06:32.000Z","dependencies_parsed_at":"2024-06-18T18:37:34.837Z","dependency_job_id":null,"html_url":"https://github.com/alecthomas/importmagic","commit_stats":{"total_commits":89,"total_committers":14,"mean_commits":6.357142857142857,"dds":0.5280898876404494,"last_synced_commit":"a6cde18d44a3f40211f222cc2c7e5436876d2277"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fimportmagic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fimportmagic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fimportmagic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fimportmagic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alecthomas","download_url":"https://codeload.github.com/alecthomas/importmagic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411235,"owners_count":20934653,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-20T21:01:50.351Z","updated_at":"2025-04-05T23:11:03.853Z","avatar_url":"https://github.com/alecthomas.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Import Magic [![image](https://secure.travis-ci.org/alecthomas/importmagic.png?branch=master)](https://travis-ci.org/alecthomas/importmagic)\n\nThe goal of this package is to be able to automatically manage imports in Python. To that end it can:\n\n- Build an index of all known symbols in all packages.\n- Find unresolved references in source, and resolve them against the index, effectively automating imports.\n- Automatically arrange imports according to PEP8.\n\nIt was originally written for the Sublime Text 2 [Python Import Magic](https://github.com/alecthomas/SublimePythonImportMagic) plugin.\n\n## Example of use in Sublime Text 2 plugin\n\n![Example of Import Magic at work](importmagic.gif)\n\n\n## Using the library\n\nGetting index from cache:\n\n```python\nindex = importmagic.SymbolIndex()\nindex.get_or_create_index(name='foo', paths=sys.path)\n```\n\nBuild an index:\n\n```python\nindex = importmagic.SymbolIndex()\nindex.build_index(sys.path)\nwith open('index.json') as fd:\n    index.serialize(fd)\n```\n\nLoad an existing index:\n\n```python\nwith open('index.json') as fd:\n    index = SymbolIndex.deserialize(fd)\n```\n\nFind unresolved and unreferenced symbols:\n\n```python\nscope = importmagic.Scope.from_source(python_source)\nunresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()\n```\n\nPrint new import block:\n\n```python\nstart_line, end_line, import_block = importmagic.get_update(python_source, index, unresolved, unreferenced)\n```\n\nUpdate source code with new import blocks:\n\n```\npython_source = importmagic.update_imports(python_source, index, unresolved, unreferenced)\n```\n\nFor more fine-grained control over what symbols are imported, the index can be queried directly:\n\n```python\nimports = importmagic.Imports(index, python_source)\nimports.remove(unreferenced)\n\nfor symbol in unresolved:\n    for score, module, variable in index.symbol_scores(symbol):\n        if variable is None:\n            imports.add_import(module)\n        else:\n            imports.add_import_from(module, variable)\n        break\n\npython_source = imports.update_source()\n```\n\n\n### Configuration\n\nConfiguring import styles\n\n1. Using `importmagic.Imports`.\n\n```python\nimports = importmagic.Imports.set_style(multiline='backslash', max_columns=80, indent_with_tabs=True)\n```\n\n`multiline` takes `backlslash` or `parentheses`.\n\n\n2. From `setup.cfg`\n\nAdd configuration to setup.cfg\n\n```python\n[importmagic]\nmultiline = 'parentheses'\nmax_columns = 120\nindent_with_tabs = 1\n```\n\nand pass root directory to importmagic\n\n```python\nimports = importmagic.Imports(root_dir='/foo/bar/')\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fimportmagic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falecthomas%2Fimportmagic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fimportmagic/lists"}