8000 Merge pull request #5038 from tacaswell/prf_fix_import_time · pelson/matplotlib@e62d9fa · GitHub
[go: up one dir, main page]

Skip to content

Commit e62d9fa

Browse files
committed
Merge pull request matplotlib#5038 from tacaswell/prf_fix_import_time
PRF: only try IPython if it is already imported
2 parents e01241c + bc20fe2 commit e62d9fa

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def _backend_selection():
112112
_IP_REGISTERED = None
113113
_INSTALL_FIG_OBSERVER = False
114114

115+
115116
def install_repl_displayhook():
116117
"""
117118
Install a repl display hook so that any stale figure are automatically
@@ -128,27 +129,30 @@ class _NotIPython(Exception):
128129
# see if we have IPython hooks around, if use them
129130

130131
try:
131-
from IPython import get_ipython
132-
ip = get_ipython()
133-
if ip is None:
134-
raise _NotIPython()
135-
136-
if _IP_REGISTERED:
137-
return
138-
139-
def post_execute():
140-
if matplotlib.is_interactive():
141-
draw_all()
142-
143-
# IPython >= 2
144-
try:
145-
ip.events.register('post_execute', post_execute)
146-
except AttributeError:
147-
# IPython 1.x
148-
ip.register_post_execute(post_execute)
149-
150-
_IP_REGISTERED = post_execute
151-
_INSTALL_FIG_OBSERVER = False
132+
if 'IPython' in sys.modules:
133+
from IPython import get_ipython
134+
ip = get_ipython()
135+
if ip is None:
136+
raise _NotIPython()
137+
138+
if _IP_REGISTERED:
139+
return
140+
141+
def post_execute():
142+
if matplotlib.is_interactive():
143+
draw_all()
144+
145+
# IPython >= 2
146+
try:
147+
ip.events.register('post_execute', post_execute)
148+
except AttributeError:
149+
# IPython 1.x
150+
ip.register_post_execute(post_execute)
151+
152+
_IP_REGISTERED = post_execute
153+
_INSTALL_FIG_OBSERVER = False
154+
else:
155+
_INSTALL_FIG_OBSERVER = True
152156

153157
# import failed or ipython is not running
154158
except (ImportError, _NotIPython):