Import Magic 
The goal of this package is to be able to automatically manage imports in Python. To that end it can:
- Build an index of all known symbols in all packages.
- Find unresolved references in source, and resolve them against the index, effectively automating imports.
- Automatically arrange imports according to PEP8.
It was originally written for the Sublime Text 2 Python Import Magic plugin.
Example of use in Sublime Text 2 plugin
Using the library
Getting index from cache:
index = importmagic.SymbolIndex()
index.get_or_create_index(name='foo', paths=sys.path)
Build an index:
index = importmagic.SymbolIndex()
index.build_index(sys.path)
with open('index.json') as fd:
index.serialize(fd)
Load an existing index:
with open('index.json') as fd:
index = SymbolIndex.deserialize(fd)
Find unresolved and unreferenced symbols:
scope = importmagic.Scope.from_source(python_source)
unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
Print new import block:
start_line, end_line, import_block = importmagic.get_update(python_source, index, unresolved, unreferenced)
Update source code with new import blocks:
python_source = importmagic.update_imports(python_source, index, unresolved, unreferenced)
For more fine-grained control over what symbols are imported, the index can be queried directly:
imports = importmagic.Imports(index, python_source)
imports.remove(unreferenced)
for symbol in unresolved:
for score, module, variable in index.symbol_scores(symbol):
if variable is None:
imports.add_import(module)
else:
imports.add_import_from(module, variable)
break
python_source = imports.update_source()
Configuration
Configuring import styles
- Using
importmagic.Imports.
imports = importmagic.Imports.set_style(multiline='backslash', max_columns=80, indent_with_tabs=True)
multiline takes backlslash or parentheses.
- From
setup.cfg
Add configuration to setup.cfg
[importmagic]
multiline = 'parentheses'
max_columns = 120
indent_with_tabs = 1
and pass root directory to importmagic
imports = importmagic.Imports(root_dir='/foo/bar/')
Metadata
Release files for importmagic 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| importmagic-0.2.0.tar.gz | 678.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| importmagic-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 860.3 kB
Release files / importmagic-0.2.0.tar.gz
| Download URL | importmagic-0.2.0.tar.gz |
|---|---|
| Size | 678.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b6e5f0639153091f3749a41b71ee27e234e1a491cf76f611aef10db6d14a2ca
|
|
BLAKE2b-256 checksum How to use checksums |
c31fdc64fcee3c586712a839f48a5536c9f270f543e3d016dc97cb4680418406
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / importmagic-0.2.0-py3-none-any.whl
| Download URL | importmagic-0.2.0-py3-none-any.whl |
|---|---|
| Size | 181.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c96784da12c422f6abe1c36e36ca2d1a993e669e9ac8700acf50f43ee1c6902
|
|
BLAKE2b-256 checksum How to use checksums |
8efdcae6fafdadd18ae795ba4da2e2a79147558ce722a2898053d25fedc9d5ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for importmagic 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.