Skip to content

Commit

Permalink
Lazy import IPython
Browse files Browse the repository at this point in the history
And if IPython isn't available for IPython.embed, use `import code` instead

Copybara generated commit for Python Fire.


PiperOrigin-RevId: 151825310
Change-Id: Iaafc12bf061b1252ddec3919ede22b86cef046b8
Reviewed-on: https://team-review.git.corp.google.com/66281
Reviewed-by: David Bieber <[email protected]>
  • Loading branch information
dbieber committed Mar 31, 2017
1 parent 5e65a31 commit 3db47f8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion fire/inspectutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import inspect

import IPython
import six


Expand Down Expand Up @@ -132,6 +131,7 @@ def Info(component):
Returns:
A dict with information about the component.
"""
import IPython # pylint: disable=g-import-not-at-top
inspector = IPython.core.oinspect.Inspector()
info = inspector.info(component)

Expand Down
11 changes: 7 additions & 4 deletions fire/interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
from __future__ import division
from __future__ import print_function

import code
import inspect

import IPython


def Embed(variables, verbose=False):
"""Drops into a Python REPL with variables available as local variables.
Expand All @@ -33,7 +30,11 @@ def Embed(variables, verbose=False):
verbose: Whether to include 'hidden' members, those keys starting with _.
"""
print(_AvailableString(variables, verbose))
_EmbedIPython(variables)

try:
_EmbedIPython(variables)
except ImportError:
_EmbedCode(variables)


def _AvailableString(variables, verbose=False):
Expand Down Expand Up @@ -82,9 +83,11 @@ def _EmbedIPython(variables, argv=None):
Values are variable values.
argv: The argv to use for starting ipython. Defaults to an empty list.
"""
import IPython # pylint: disable=g-import-not-at-top
argv = argv or []
IPython.start_ipython(argv=argv, user_ns=variables)


def _EmbedCode(variables):
import code # pylint: disable=g-import-not-at-top
code.InteractiveConsole(variables).interact()

0 comments on commit 3db47f8

Please sign in to comment.