MCPcopy Index your code
hub / github.com/ipython/ipython / init_user_ns

Method init_user_ns

IPython/core/interactiveshell.py:1388–1446  ·  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

1386 sys.modules[main_name] = self.user_module
1387
1388 def init_user_ns(self):
1389 """Initialize all user-visible namespaces to their minimum defaults.
1390
1391 Certain history lists are also initialized here, as they effectively
1392 act as user namespaces.
1393
1394 Notes
1395 -----
1396 All data structures here are only filled in, they are NOT reset by this
1397 method. If they were not empty before, data will simply be added to
1398 them.
1399 """
1400 # This function works in two parts: first we put a few things in
1401 # user_ns, and we sync that contents into user_ns_hidden so that these
1402 # initial variables aren't shown by %who. After the sync, we add the
1403 # rest of what we *do* want the user to see with %who even on a new
1404 # session (probably nothing, so they really only see their own stuff)
1405
1406 # The user dict must *always* have a __builtin__ reference to the
1407 # Python standard __builtin__ namespace, which must be imported.
1408 # This is so that certain operations in prompt evaluation can be
1409 # reliably executed with builtins. Note that we can NOT use
1410 # __builtins__ (note the 's'), because that can either be a dict or a
1411 # module, and can even mutate at runtime, depending on the context
1412 # (Python makes no guarantees on it). In contrast, __builtin__ is
1413 # always a module object, though it must be explicitly imported.
1414
1415 # For more details:
1416 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
1417 ns = {}
1418
1419 # make global variables for user access to the histories
1420 if self.history_manager is not None:
1421 ns["_ih"] = self.history_manager.input_hist_parsed
1422 ns["_oh"] = self.history_manager.output_hist
1423 ns["_dh"] = self.history_manager.dir_hist
1424
1425 # user aliases to input and output histories. These shouldn't show up
1426 # in %who, as they can have very large reprs.
1427 ns["In"] = self.history_manager.input_hist_parsed
1428 ns["Out"] = self.history_manager.output_hist
1429
1430 # Store myself as the public api!!!
1431 ns['get_ipython'] = self.get_ipython
1432
1433 ns['exit'] = self.exiter
1434 ns['quit'] = self.exiter
1435 ns["open"] = _modified_open
1436
1437 # Sync what we've added so far to user_ns_hidden so these aren't seen
1438 # by %who
1439 self.user_ns_hidden.update(ns)
1440
1441 # Anything put into ns now would show up in %who. Think twice before
1442 # putting anything here, as we really want %who to show the user their
1443 # stuff, not our variables.
1444
1445 # Finally, update the real user's namespace

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