You are viewing an older version (9.10.0). Go to latest (9.13.0)
{ } Raw JSON

bundles / IPython 9.10.0 / IPython / core / interactiveshell / InteractiveShell / new_main_mod

function

IPython.core.interactiveshell:InteractiveShell.new_main_mod

source: /IPython/core/interactiveshell.py :1112

Signature

def   new_main_mod ( self filename modname )

Summary

Return a new 'main' module object for user code execution.

Extended Summary

filename should be the path of the script which will be run in the module. Requests with the same filename will get the same module, with its namespace cleared.

modname should be the module name - normally either '__main__' or the basename of the file without the extension.

When scripts are executed via %run, we must keep a reference to their __main__ module around so that Python doesn't clear it, rendering references to module globals useless.

This method keeps said reference in a private dict, keyed by the absolute path of the script. This way, for multiple executions of the same script we only keep one copy of the namespace (the last one), thus preventing memory leaks from old references while allowing the objects from the last execution to be accessible.

Aliases

  • IPython.InteractiveShell.new_main_mod

Referenced by