Hide imported modules to interpreter

2019-04-06 17:29发布

问题:

I have built a module which is using a few different modules for various tasks. When I'm importing my module in IPython and listing the available functions for autocompletion, these external modules are included in that list. Is it possible to hide them in some way?

回答1:

In Python, modules can define an __all__ variable, which is a list of the names that should be imported when someone does:

from module import *

IPython can use the same variable to limit completions, though it does not do this by default. To enable this at runtime, set:

get_ipython().Completer.limit_to__all__ = True

Or to set it permanently, add to your ipython_config.py:

c.IPCompleter.limit_to__all__ = True