Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anntzer/ipython-autoimport
Automagically import missing modules in IPython.
https://github.com/anntzer/ipython-autoimport
ipython
Last synced: 2 months ago
JSON representation
Automagically import missing modules in IPython.
- Host: GitHub
- URL: https://github.com/anntzer/ipython-autoimport
- Owner: anntzer
- License: zlib
- Created: 2016-08-05T05:16:28.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2023-10-19T07:26:25.000Z (about 1 year ago)
- Last Synced: 2023-10-20T06:12:08.301Z (about 1 year ago)
- Topics: ipython
- Language: Python
- Homepage:
- Size: 114 KB
- Stars: 56
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- License: LICENSE.txt
Awesome Lists containing this project
README
ipython-autoimport
==================| |GitHub| |PyPI| |Build|
.. |GitHub|
image:: https://img.shields.io/badge/github-anntzer%2Fdefopt-brightgreen
:target: https://github.com/anntzer/ipython-autoimport
.. |PyPI|
image:: https://img.shields.io/pypi/v/ipython-autoimport.svg?color=brightgreen
:target: https://pypi.python.org/pypi/ipython-autoimport
.. |Build|
image:: https://img.shields.io/github/actions/workflow/status/anntzer/ipython-autoimport/build.yml?branch=main
:target: https://github.com/anntzer/ipython-autoimport/actionsAutomagically import missing modules in IPython: instead of ::
In [1]: plt.plot([1, 2], [3, 4])
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
in ()
----> 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]: []do what I mean::
In [1]: plt.plot([1, 2], [3, 4])
Autoimport: from matplotlib import pyplot as plt
Out[1]: []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:
.. code-block:: sh
$ pip install ipython-autoimport # from PyPI
$ pip install git+https://github.com/anntzer/ipython-autoimport # from GithubThen, 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_valuewill 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.