Skip to main content

Automagically import missing modules in IPython.

Project description

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

  • Avoid erroring when exiting IPython≥8.15.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipython_autoimport-0.5.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

ipython_autoimport-0.5-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file ipython_autoimport-0.5.tar.gz.

File metadata

  • Download URL: ipython_autoimport-0.5.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for ipython_autoimport-0.5.tar.gz
Algorithm Hash digest
SHA256 9499ed45b62e047f02e9542d04ec70101b5ef310a22ce310307c1d065fa4be65
MD5 19511869b587f665dee0cd9cc351d2d6
BLAKE2b-256 b7c60f2930e198cb67016227cb92be66e9ea888cfba5f13451ead14b26c7e376

See more details on using hashes here.

File details

Details for the file ipython_autoimport-0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for ipython_autoimport-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 701b41483a85ecf55fb572d4d677f96c80c34f70ae07d18e4f7f26480d52aea0
MD5 b918828981e800856a5e0ad1f25b7f83
BLAKE2b-256 f0b56af9634b96ec72d0863afa1410841e0f6c1aabdfff2f46e0574bbbce2bb7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page