Skip to main content

ipython-autoimport

GitHub PyPI Build

Automagically import missing modules in IPython: instead of

In [1]: plt.plot([1, 2], [3, 4])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-994ba2bf13c0> in <module>()
----> 1 plt.plot([1, 2], [3, 4])

NameError: name 'plt' is not defined

In [2]: from matplotlib import pyplot as plt

In [3]: plt.plot([1, 2], [3, 4])
Out[3]: [<matplotlib.lines.Line2D at 0x7f73f0179198>]

do what I mean:

In [1]: plt.plot([1, 2], [3, 4])
Autoimport: from matplotlib import pyplot as plt
Out[1]: [<matplotlib.lines.Line2D at 0x7f7e253552b0>]

Inspired from @OrangeFlash81’s version, with many improvements:

  • Does not rely on re-execution, but instead hooks the user namespace; thus, safe even in the presence of side effects, and works for tab completion and magics too.

  • Learns your preferred aliases from the history – plt is not hardcoded to alias matplotlib.pyplot, just found because you previously imported pyplot under this alias.

  • Suppresses irrelevant chained tracebacks.

  • Auto-imports submodules.

  • pip-installable.

To see auto imports from the current session: %autoimport -l

To clear the cache for a symbol with multiple possible imports: %autoimport -c SYMBOL

Installation

As usual, install using pip:

$ pip install ipython-autoimport  # from PyPI
$ pip install git+https://github.com/anntzer/ipython-autoimport  # from Github

Then, append the output of python -m ipython_autoimport to the ipython_config.py file in the directory printed by ipython profile locate (typically ~/.ipython/profile_default/). If you don’t have such a file at all, first create it with ipython profile create.

When using Spyder, the above registration method will not work; instead, add %load_ext ipython_autoimport to the Preferences → IPython console → Startup → Run code option.

Note that upon loading, ipython_autoimport will register its submodule auto-importer to IPython’s “limited evalutation” completer policy (on IPython versions that support it).

Run tests with pytest.

Limitations

Constructs such as

class C:
   auto_imported_value

will not work, because they are run using the class locals (rather than the patched locals); patching globals would not work because LOAD_NAME queries globals using PyDict_GetItem exactly (note that it queries locals using PyObject_GetItem; also, LOAD_GLOBALS queries both globals and builtins using PyObject_GetItem so we could possibly get away with patching the builtins dict instead, but that seems a bit too invasive…).

When using Jedi autocompletion (the default if Jedi is installed as of IPython 7.2), trying to tab-complete not-yet-imported global names to trigger an import failure, because Jedi purposefully converts the global dict to a namespace object and looks up attributes using getattr_static. Jedi can be disabled by adding c.Completer.use_jedi = False to the ipython_config.py file.

Changelog

v0.5.1 (2025-03-11)

  • Fix compatibility with IPython 9’s new theme system.

v0.5 (2024-08-20)

  • Avoid erroring when exiting IPython≥8.15.

Metadata

Release files for ipython-autoimport 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipython-autoimport 0.5.1
File Size Uploaded Provenance
ipython_autoimport-0.5.1.tar.gz 9.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ipython-autoimport 0.5.1
File Interpreter ABI Platform
ipython_autoimport-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.6 kB

Release files / ipython_autoimport-0.5.1.tar.gz

Download URL ipython_autoimport-0.5.1.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7187bf26b3ae00c0f2da56af85e6f36821abe57b266846232ab8985e4fe7a2ab
BLAKE2b-256 checksum
How to use checksums
7433740d3884ed0eaa602172083b78dedc1d87121abdd74985667491a656b654
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.13.0

Release files / ipython_autoimport-0.5.1-py3-none-any.whl

Download URL ipython_autoimport-0.5.1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
235cd59c7b576cd3f8593c6fdf149d388662cc8efa2baa59030fefee463443c8
BLAKE2b-256 checksum
How to use checksums
caa902f79756107ad99225743303f6c454e6c3886d6777f863900ae6128c076a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.13.0

Security for ipython-autoimport 0.5.1

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page