##// END OF EJS Templates
Add '-m mod : run library module as a script' option....
Bradley M. Froehle -
Show More
@@ -26,6 +26,7 b' import codeop'
26 import inspect
26 import inspect
27 import os
27 import os
28 import re
28 import re
29 import runpy
29 import sys
30 import sys
30 import tempfile
31 import tempfile
31 import types
32 import types
@@ -2356,6 +2357,28 b' class InteractiveShell(SingletonConfigurable, Magic):'
2356 self.showtraceback()
2357 self.showtraceback()
2357 warn('Unknown failure executing file: <%s>' % fname)
2358 warn('Unknown failure executing file: <%s>' % fname)
2358
2359
2360 def safe_run_module(self, mod_name, where):
2361 """A safe version of runpy.run_module().
2362
2363 This version will never throw an exception, but instead print
2364 helpful error messages to the screen.
2365
2366 Parameters
2367 ----------
2368 mod_name : string
2369 The name of the module to be executed.
2370 where : dict
2371 The globals namespace.
2372 """
2373 try:
2374 where.update(
2375 runpy.run_module(str(mod_name), run_name="__main__",
2376 alter_sys=True)
2377 )
2378 except:
2379 self.showtraceback()
2380 warn('Unknown failure executing module: <%s>' % mod_name)
2381
2359 def run_cell(self, raw_cell, store_history=False):
2382 def run_cell(self, raw_cell, store_history=False):
2360 """Run a complete IPython cell.
2383 """Run a complete IPython cell.
2361
2384
@@ -103,6 +103,7 b' shell_aliases = dict('
103 logfile='InteractiveShell.logfile',
103 logfile='InteractiveShell.logfile',
104 logappend='InteractiveShell.logappend',
104 logappend='InteractiveShell.logappend',
105 c='InteractiveShellApp.code_to_run',
105 c='InteractiveShellApp.code_to_run',
106 m='InteractiveShellApp.module_to_run',
106 ext='InteractiveShellApp.extra_extension',
107 ext='InteractiveShellApp.extra_extension',
107 )
108 )
108 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
109 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
@@ -146,6 +147,9 b' class InteractiveShellApp(Configurable):'
146 code_to_run = Unicode('', config=True,
147 code_to_run = Unicode('', config=True,
147 help="Execute the given command string."
148 help="Execute the given command string."
148 )
149 )
150 module_to_run = Unicode('', config=True,
151 help="Run the module as a script."
152 )
149 pylab_import_all = Bool(True, config=True,
153 pylab_import_all = Bool(True, config=True,
150 help="""If true, an 'import *' is done from numpy and pylab,
154 help="""If true, an 'import *' is done from numpy and pylab,
151 when using pylab"""
155 when using pylab"""
@@ -183,6 +187,7 b' class InteractiveShellApp(Configurable):'
183 self._run_exec_lines()
187 self._run_exec_lines()
184 self._run_exec_files()
188 self._run_exec_files()
185 self._run_cmd_line_code()
189 self._run_cmd_line_code()
190 self._run_module()
186
191
187 # flush output, so itwon't be attached to the first cell
192 # flush output, so itwon't be attached to the first cell
188 sys.stdout.flush()
193 sys.stdout.flush()
@@ -294,3 +299,7 b' class InteractiveShellApp(Configurable):'
294 fname)
299 fname)
295 self.shell.showtraceback()
300 self.shell.showtraceback()
296
301
302 def _run_module(self):
303 """Run module specified at the command-line."""
304 if self.module_to_run:
305 self.shell.safe_run_module(self.module_to_run, self.shell.user_ns)
General Comments 0
You need to be logged in to leave comments. Login now