@@ -220,14 +220,14 @@ def embed(
220
220
self .save_state ()
221
221
self .show_animation_progress = show_animation_progress
222
222
223
- # Create embedded IPython terminal to be configured
224
- shell = InteractiveShellEmbed .instance ()
225
-
226
- # Use the locals namespace of the caller
223
+ # Create embedded IPython terminal configured to have access to
224
+ # the local namespace of the caller
227
225
caller_frame = inspect .currentframe ().f_back
228
- local_ns = dict (caller_frame .f_locals )
226
+ module = get_module (caller_frame .f_globals ["__file__" ])
227
+ shell = InteractiveShellEmbed (user_module = module )
229
228
230
- # Add a few custom shortcuts
229
+ # Add a few custom shortcuts to that local namespace
230
+ local_ns = dict (caller_frame .f_locals )
231
231
local_ns .update (
232
232
play = self .play ,
233
233
wait = self .wait ,
@@ -244,6 +244,9 @@ def embed(
244
244
notouch = lambda : shell .enable_gui (None ),
245
245
)
246
246
247
+ # Update the shell module with the caller's locals + shortcuts
248
+ module .__dict__ .update (local_ns )
249
+
247
250
# Enables gui interactions during the embed
248
251
def inputhook (context ):
249
252
while not context .input_is_ready ():
@@ -278,13 +281,7 @@ def custom_exc(shell, etype, evalue, tb, tb_offset=None):
278
281
shell .magic (f"xmode { self .embed_exception_mode } " )
279
282
280
283
# Launch shell
281
- shell (
282
- local_ns = local_ns ,
283
- # Pretend like we're embeding in the caller function, not here
284
- stack_depth = 2 ,
285
- # Specify that the present module is the caller's, not here
286
- module = get_module (caller_frame .f_globals ["__file__" ])
287
- )
284
+ shell ()
288
285
289
286
# End scene when exiting an embed
290
287
if close_scene_on_exit :
0 commit comments