blob: b463f2b1299c26dd4e7f0614ad1a62b375bec37f [file] [log] [blame]
Thomas Woutersa9773292006-04-21 09:43:23 +00001"""runpy.py - locating and running Python code using the module namespace
2
3Provides support for locating and running Python scripts using the Python
4module namespace instead of the native filesystem.
5
6This allows Python code to play nicely with non-filesystem based PEP 302
7importers when locating support scripts as well as when importing modules.
8"""
9# Written by Nick Coghlan <ncoghlan at gmail.com>
10# to implement PEP 338 (Executing Modules as Scripts)
11
12import sys
13import imp
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014try:
15 from imp import get_loader
16except ImportError:
17 from pkgutil import get_loader
Thomas Woutersa9773292006-04-21 09:43:23 +000018
19__all__ = [
20 "run_module",
21]
22
Thomas Woutersa9773292006-04-21 09:43:23 +000023
24def _run_code(code, run_globals, init_globals,
25 mod_name, mod_fname, mod_loader):
26 """Helper for _run_module_code"""
27 if init_globals is not None:
28 run_globals.update(init_globals)
29 run_globals.update(__name__ = mod_name,
30 __file__ = mod_fname,
31 __loader__ = mod_loader)
Georg Brandl7cae87c2006-09-06 06:51:57 +000032 exec(code, run_globals)
Thomas Woutersa9773292006-04-21 09:43:23 +000033 return run_globals
34
35def _run_module_code(code, init_globals=None,
Guido van Rossum806c2462007-08-06 23:33:07 +000036 mod_name=None, mod_fname=None,
37 mod_loader=None, alter_sys=False):
Thomas Woutersa9773292006-04-21 09:43:23 +000038 """Helper for run_module"""
39 # Set up the top level namespace dictionary
40 if alter_sys:
Guido van Rossum806c2462007-08-06 23:33:07 +000041 # Modify sys.argv[0] and sys.modules[mod_name]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042 sys.argv[0] = mod_fname
Guido van Rossum806c2462007-08-06 23:33:07 +000043 module = imp.new_module(mod_name)
44 sys.modules[mod_name] = module
45 mod_globals = module.__dict__
Thomas Woutersa9773292006-04-21 09:43:23 +000046 else:
47 # Leave the sys module alone
Guido van Rossum806c2462007-08-06 23:33:07 +000048 mod_globals = {}
49 return _run_code(code, mod_globals, init_globals,
50 mod_name, mod_fname, mod_loader)
Thomas Woutersa9773292006-04-21 09:43:23 +000051
52
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053# This helper is needed due to a missing component in the PEP 302
54# loader protocol (specifically, "get_filename" is non-standard)
55def _get_filename(loader, mod_name):
56 try:
57 get_filename = loader.get_filename
58 except AttributeError:
59 return None
60 else:
61 return get_filename(mod_name)
62
63
Thomas Woutersa9773292006-04-21 09:43:23 +000064def run_module(mod_name, init_globals=None,
65 run_name=None, alter_sys=False):
66 """Execute a module's code without importing it
67
68 Returns the resulting top level namespace dictionary
69 """
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070 loader = get_loader(mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +000071 if loader is None:
Guido van Rossum806c2462007-08-06 23:33:07 +000072 raise ImportError("No module named %s" % mod_name)
73 if loader.is_package(mod_name):
74 raise ImportError(("%s is a package and cannot " +
75 "be directly executed") % mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +000076 code = loader.get_code(mod_name)
77 if code is None:
Guido van Rossum806c2462007-08-06 23:33:07 +000078 raise ImportError("No code object available for %s" % mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +000079 filename = _get_filename(loader, mod_name)
80 if run_name is None:
81 run_name = mod_name
82 return _run_module_code(code, init_globals, run_name,
83 filename, loader, alter_sys)
84
85
86if __name__ == "__main__":
87 # Run the module specified as the next command line argument
88 if len(sys.argv) < 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000089 print("No module specified for execution", file=sys.stderr)
Thomas Woutersa9773292006-04-21 09:43:23 +000090 else:
91 del sys.argv[0] # Make the requested module sys.argv[0]
92 run_module(sys.argv[0], run_name="__main__", alter_sys=True)