MCPcopy Create free account
hub / github.com/ipython/ipython / init_user_ns

Method init_user_ns

IPython/core/interactiveshell.py:1344–1400  ·  view source on GitHub ↗

Initialize all user-visible namespaces to their minimum defaults. Certain history lists are also initialized here, as they effectively act as user namespaces. Notes ----- All data structures here are only filled in, they are NOT reset by this method.

(self)

Source from the content-addressed store, hash-verified

1342 sys.modules[main_name] = self.user_module
1343
1344 def init_user_ns(self):
1345 """Initialize all user-visible namespaces to their minimum defaults.
1346
1347 Certain history lists are also initialized here, as they effectively
1348 act as user namespaces.
1349
1350 Notes
1351 -----
1352 All data structures here are only filled in, they are NOT reset by this
1353 method. If they were not empty before, data will simply be added to
1354 them.
1355 """
1356 # This function works in two parts: first we put a few things in
1357 # user_ns, and we sync that contents into user_ns_hidden so that these
1358 # initial variables aren't shown by %who. After the sync, we add the
1359 # rest of what we *do* want the user to see with %who even on a new
1360 # session (probably nothing, so they really only see their own stuff)
1361
1362 # The user dict must *always* have a __builtin__ reference to the
1363 # Python standard __builtin__ namespace, which must be imported.
1364 # This is so that certain operations in prompt evaluation can be
1365 # reliably executed with builtins. Note that we can NOT use
1366 # __builtins__ (note the 's'), because that can either be a dict or a
1367 # module, and can even mutate at runtime, depending on the context
1368 # (Python makes no guarantees on it). In contrast, __builtin__ is
1369 # always a module object, though it must be explicitly imported.
1370
1371 # For more details:
1372 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1373 ns = {}
1374
1375 # make global variables for user access to the histories
1376 ns['_ih'] = self.history_manager.input_hist_parsed
1377 ns['_oh'] = self.history_manager.output_hist
1378 ns['_dh'] = self.history_manager.dir_hist
1379
1380 # user aliases to input and output histories. These shouldn't show up
1381 # in %who, as they can have very large reprs.
1382 ns['In'] = self.history_manager.input_hist_parsed
1383 ns['Out'] = self.history_manager.output_hist
1384
1385 # Store myself as the public api!!!
1386 ns['get_ipython'] = self.get_ipython
1387
1388 ns['exit'] = self.exiter
1389 ns['quit'] = self.exiter
1390
1391 # Sync what we've added so far to user_ns_hidden so these aren't seen
1392 # by %who
1393 self.user_ns_hidden.update(ns)
1394
1395 # Anything put into ns now would show up in %who. Think twice before
1396 # putting anything here, as we really want %who to show the user their
1397 # stuff, not our variables.
1398
1399 # Finally, update the real user's namespace
1400 self.user_ns.update(ns)
1401

Callers 4

__init__Method · 0.95
resetMethod · 0.95
mainloopMethod · 0.80
_user_ns_changedMethod · 0.80

Calls 1

updateMethod · 0.45

Tested by

no test coverage detected