1+ from __future__ import print_function
2+
3+ from contextlib import contextmanager
4+ import os
5+ import sys
6+ import traceback
7+
18from .core import get_cached_api
29
310try :
@@ -53,6 +60,43 @@ def julia_inputhook(context):
5360 jl_sleep (0.05 )
5461
5562
63+ @contextmanager
64+ def init_julia_message_on_failure ():
65+ try :
66+ yield
67+ except Exception :
68+ traceback .print_exc ()
69+ print (file = sys .stderr )
70+ print ("Executing `julia.Julia(init_julia=False)` failed." ,
71+ "It is safe to ignore this exception unless you are" ,
72+ "going to use PyJulia." ,
73+ file = sys .stderr )
74+ print ("To suppress automatic PyJulia initialization," ,
75+ "set environment variable `IPYTHON_JL_SETUP_PYJULIA`" ,
76+ 'to "no".' ,
77+ file = sys .stderr )
78+
79+
80+ def maybe_load_pyjulia ():
81+ """
82+ Execute ``julia.Julia(init_julia=False)`` if appropriate.
83+
84+ It is useful since it skips initialization when creating the
85+ global "cached" API. This makes PyJuli initialization slightly
86+ faster and also makes sure to not load incompatible `libjulia`
87+ when the name of the julia command of this process is not `julia`.
88+ """
89+ if (os .environ .get ("IPYTHON_JL_SETUP_PYJULIA" , "yes" ).lower ()
90+ in ("yes" , "t" , "true" )):
91+ try :
92+ from julia import Julia
93+ except ImportError :
94+ pass
95+ else :
96+ with init_julia_message_on_failure ():
97+ Julia (init_julia = False )
98+
99+
56100def load_ipython_extension (ip ):
57101 global _unregister_key_bindings
58102 _unregister_key_bindings = register_key_bindings (ip )
@@ -62,6 +106,8 @@ def load_ipython_extension(ip):
62106 if not ip .active_eventloop :
63107 ip .enable_gui ("julia" )
64108
109+ maybe_load_pyjulia ()
110+
65111 ip .set_hook ("complete_command" , _julia_completer ,
66112 re_key = r""".*\bMain\.eval\(["']""" )
67113# See:
0 commit comments