##// END OF EJS Templates
add PYTHONSTARTUP to startup files...
MinRK -
Show More
@@ -1,363 +1,366 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 A mixin for :class:`~IPython.core.application.Application` classes that
3 A mixin for :class:`~IPython.core.application.Application` classes that
4 launch InteractiveShell instances, load extensions, etc.
4 launch InteractiveShell instances, load extensions, etc.
5
5
6 Authors
6 Authors
7 -------
7 -------
8
8
9 * Min Ragan-Kelley
9 * Min Ragan-Kelley
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2011 The IPython Development Team
13 # Copyright (C) 2008-2011 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 from __future__ import absolute_import
23 from __future__ import absolute_import
24
24
25 import glob
25 import glob
26 import os
26 import os
27 import sys
27 import sys
28
28
29 from IPython.config.application import boolean_flag
29 from IPython.config.application import boolean_flag
30 from IPython.config.configurable import Configurable
30 from IPython.config.configurable import Configurable
31 from IPython.config.loader import Config
31 from IPython.config.loader import Config
32 from IPython.core import pylabtools
32 from IPython.core import pylabtools
33 from IPython.utils import py3compat
33 from IPython.utils import py3compat
34 from IPython.utils.contexts import preserve_keys
34 from IPython.utils.contexts import preserve_keys
35 from IPython.utils.path import filefind
35 from IPython.utils.path import filefind
36 from IPython.utils.traitlets import (
36 from IPython.utils.traitlets import (
37 Unicode, Instance, List, Bool, CaselessStrEnum
37 Unicode, Instance, List, Bool, CaselessStrEnum
38 )
38 )
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Aliases and Flags
41 # Aliases and Flags
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44 shell_flags = {}
44 shell_flags = {}
45
45
46 addflag = lambda *args: shell_flags.update(boolean_flag(*args))
46 addflag = lambda *args: shell_flags.update(boolean_flag(*args))
47 addflag('autoindent', 'InteractiveShell.autoindent',
47 addflag('autoindent', 'InteractiveShell.autoindent',
48 'Turn on autoindenting.', 'Turn off autoindenting.'
48 'Turn on autoindenting.', 'Turn off autoindenting.'
49 )
49 )
50 addflag('automagic', 'InteractiveShell.automagic',
50 addflag('automagic', 'InteractiveShell.automagic',
51 """Turn on the auto calling of magic commands. Type %%magic at the
51 """Turn on the auto calling of magic commands. Type %%magic at the
52 IPython prompt for more information.""",
52 IPython prompt for more information.""",
53 'Turn off the auto calling of magic commands.'
53 'Turn off the auto calling of magic commands.'
54 )
54 )
55 addflag('pdb', 'InteractiveShell.pdb',
55 addflag('pdb', 'InteractiveShell.pdb',
56 "Enable auto calling the pdb debugger after every exception.",
56 "Enable auto calling the pdb debugger after every exception.",
57 "Disable auto calling the pdb debugger after every exception."
57 "Disable auto calling the pdb debugger after every exception."
58 )
58 )
59 # pydb flag doesn't do any config, as core.debugger switches on import,
59 # pydb flag doesn't do any config, as core.debugger switches on import,
60 # which is before parsing. This just allows the flag to be passed.
60 # which is before parsing. This just allows the flag to be passed.
61 shell_flags.update(dict(
61 shell_flags.update(dict(
62 pydb = ({},
62 pydb = ({},
63 """Use the third party 'pydb' package as debugger, instead of pdb.
63 """Use the third party 'pydb' package as debugger, instead of pdb.
64 Requires that pydb is installed."""
64 Requires that pydb is installed."""
65 )
65 )
66 ))
66 ))
67 addflag('pprint', 'PlainTextFormatter.pprint',
67 addflag('pprint', 'PlainTextFormatter.pprint',
68 "Enable auto pretty printing of results.",
68 "Enable auto pretty printing of results.",
69 "Disable auto auto pretty printing of results."
69 "Disable auto auto pretty printing of results."
70 )
70 )
71 addflag('color-info', 'InteractiveShell.color_info',
71 addflag('color-info', 'InteractiveShell.color_info',
72 """IPython can display information about objects via a set of func-
72 """IPython can display information about objects via a set of func-
73 tions, and optionally can use colors for this, syntax highlighting
73 tions, and optionally can use colors for this, syntax highlighting
74 source code and various other elements. However, because this
74 source code and various other elements. However, because this
75 information is passed through a pager (like 'less') and many pagers get
75 information is passed through a pager (like 'less') and many pagers get
76 confused with color codes, this option is off by default. You can test
76 confused with color codes, this option is off by default. You can test
77 it and turn it on permanently in your ipython_config.py file if it
77 it and turn it on permanently in your ipython_config.py file if it
78 works for you. Test it and turn it on permanently if it works with
78 works for you. Test it and turn it on permanently if it works with
79 your system. The magic function %%color_info allows you to toggle this
79 your system. The magic function %%color_info allows you to toggle this
80 interactively for testing.""",
80 interactively for testing.""",
81 "Disable using colors for info related things."
81 "Disable using colors for info related things."
82 )
82 )
83 addflag('deep-reload', 'InteractiveShell.deep_reload',
83 addflag('deep-reload', 'InteractiveShell.deep_reload',
84 """Enable deep (recursive) reloading by default. IPython can use the
84 """Enable deep (recursive) reloading by default. IPython can use the
85 deep_reload module which reloads changes in modules recursively (it
85 deep_reload module which reloads changes in modules recursively (it
86 replaces the reload() function, so you don't need to change anything to
86 replaces the reload() function, so you don't need to change anything to
87 use it). deep_reload() forces a full reload of modules whose code may
87 use it). deep_reload() forces a full reload of modules whose code may
88 have changed, which the default reload() function does not. When
88 have changed, which the default reload() function does not. When
89 deep_reload is off, IPython will use the normal reload(), but
89 deep_reload is off, IPython will use the normal reload(), but
90 deep_reload will still be available as dreload(). This feature is off
90 deep_reload will still be available as dreload(). This feature is off
91 by default [which means that you have both normal reload() and
91 by default [which means that you have both normal reload() and
92 dreload()].""",
92 dreload()].""",
93 "Disable deep (recursive) reloading by default."
93 "Disable deep (recursive) reloading by default."
94 )
94 )
95 nosep_config = Config()
95 nosep_config = Config()
96 nosep_config.InteractiveShell.separate_in = ''
96 nosep_config.InteractiveShell.separate_in = ''
97 nosep_config.InteractiveShell.separate_out = ''
97 nosep_config.InteractiveShell.separate_out = ''
98 nosep_config.InteractiveShell.separate_out2 = ''
98 nosep_config.InteractiveShell.separate_out2 = ''
99
99
100 shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
100 shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
101 shell_flags['pylab'] = (
101 shell_flags['pylab'] = (
102 {'InteractiveShellApp' : {'pylab' : 'auto'}},
102 {'InteractiveShellApp' : {'pylab' : 'auto'}},
103 """Pre-load matplotlib and numpy for interactive use with
103 """Pre-load matplotlib and numpy for interactive use with
104 the default matplotlib backend."""
104 the default matplotlib backend."""
105 )
105 )
106
106
107 # it's possible we don't want short aliases for *all* of these:
107 # it's possible we don't want short aliases for *all* of these:
108 shell_aliases = dict(
108 shell_aliases = dict(
109 autocall='InteractiveShell.autocall',
109 autocall='InteractiveShell.autocall',
110 colors='InteractiveShell.colors',
110 colors='InteractiveShell.colors',
111 logfile='InteractiveShell.logfile',
111 logfile='InteractiveShell.logfile',
112 logappend='InteractiveShell.logappend',
112 logappend='InteractiveShell.logappend',
113 c='InteractiveShellApp.code_to_run',
113 c='InteractiveShellApp.code_to_run',
114 m='InteractiveShellApp.module_to_run',
114 m='InteractiveShellApp.module_to_run',
115 ext='InteractiveShellApp.extra_extension',
115 ext='InteractiveShellApp.extra_extension',
116 gui='InteractiveShellApp.gui',
116 gui='InteractiveShellApp.gui',
117 pylab='InteractiveShellApp.pylab',
117 pylab='InteractiveShellApp.pylab',
118 )
118 )
119 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
119 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
120
120
121 #-----------------------------------------------------------------------------
121 #-----------------------------------------------------------------------------
122 # Main classes and functions
122 # Main classes and functions
123 #-----------------------------------------------------------------------------
123 #-----------------------------------------------------------------------------
124
124
125 class InteractiveShellApp(Configurable):
125 class InteractiveShellApp(Configurable):
126 """A Mixin for applications that start InteractiveShell instances.
126 """A Mixin for applications that start InteractiveShell instances.
127
127
128 Provides configurables for loading extensions and executing files
128 Provides configurables for loading extensions and executing files
129 as part of configuring a Shell environment.
129 as part of configuring a Shell environment.
130
130
131 The following methods should be called by the :meth:`initialize` method
131 The following methods should be called by the :meth:`initialize` method
132 of the subclass:
132 of the subclass:
133
133
134 - :meth:`init_path`
134 - :meth:`init_path`
135 - :meth:`init_shell` (to be implemented by the subclass)
135 - :meth:`init_shell` (to be implemented by the subclass)
136 - :meth:`init_gui_pylab`
136 - :meth:`init_gui_pylab`
137 - :meth:`init_extensions`
137 - :meth:`init_extensions`
138 - :meth:`init_code`
138 - :meth:`init_code`
139 """
139 """
140 extensions = List(Unicode, config=True,
140 extensions = List(Unicode, config=True,
141 help="A list of dotted module names of IPython extensions to load."
141 help="A list of dotted module names of IPython extensions to load."
142 )
142 )
143 extra_extension = Unicode('', config=True,
143 extra_extension = Unicode('', config=True,
144 help="dotted module name of an IPython extension to load."
144 help="dotted module name of an IPython extension to load."
145 )
145 )
146 def _extra_extension_changed(self, name, old, new):
146 def _extra_extension_changed(self, name, old, new):
147 if new:
147 if new:
148 # add to self.extensions
148 # add to self.extensions
149 self.extensions.append(new)
149 self.extensions.append(new)
150
150
151 # Extensions that are always loaded (not configurable)
151 # Extensions that are always loaded (not configurable)
152 default_extensions = List(Unicode, [u'storemagic'], config=False)
152 default_extensions = List(Unicode, [u'storemagic'], config=False)
153
153
154 exec_files = List(Unicode, config=True,
154 exec_files = List(Unicode, config=True,
155 help="""List of files to run at IPython startup."""
155 help="""List of files to run at IPython startup."""
156 )
156 )
157 file_to_run = Unicode('', config=True,
157 file_to_run = Unicode('', config=True,
158 help="""A file to be run""")
158 help="""A file to be run""")
159
159
160 exec_lines = List(Unicode, config=True,
160 exec_lines = List(Unicode, config=True,
161 help="""lines of code to run at IPython startup."""
161 help="""lines of code to run at IPython startup."""
162 )
162 )
163 code_to_run = Unicode('', config=True,
163 code_to_run = Unicode('', config=True,
164 help="Execute the given command string."
164 help="Execute the given command string."
165 )
165 )
166 module_to_run = Unicode('', config=True,
166 module_to_run = Unicode('', config=True,
167 help="Run the module as a script."
167 help="Run the module as a script."
168 )
168 )
169 gui = CaselessStrEnum(('qt', 'wx', 'gtk', 'glut', 'pyglet', 'osx'), config=True,
169 gui = CaselessStrEnum(('qt', 'wx', 'gtk', 'glut', 'pyglet', 'osx'), config=True,
170 help="Enable GUI event loop integration ('qt', 'wx', 'gtk', 'glut', 'pyglet', 'osx')."
170 help="Enable GUI event loop integration ('qt', 'wx', 'gtk', 'glut', 'pyglet', 'osx')."
171 )
171 )
172 pylab = CaselessStrEnum(['tk', 'qt', 'wx', 'gtk', 'osx', 'inline', 'auto'],
172 pylab = CaselessStrEnum(['tk', 'qt', 'wx', 'gtk', 'osx', 'inline', 'auto'],
173 config=True,
173 config=True,
174 help="""Pre-load matplotlib and numpy for interactive use,
174 help="""Pre-load matplotlib and numpy for interactive use,
175 selecting a particular matplotlib backend and loop integration.
175 selecting a particular matplotlib backend and loop integration.
176 """
176 """
177 )
177 )
178 pylab_import_all = Bool(True, config=True,
178 pylab_import_all = Bool(True, config=True,
179 help="""If true, an 'import *' is done from numpy and pylab,
179 help="""If true, an 'import *' is done from numpy and pylab,
180 when using pylab"""
180 when using pylab"""
181 )
181 )
182 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
182 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
183
183
184 def init_path(self):
184 def init_path(self):
185 """Add current working directory, '', to sys.path"""
185 """Add current working directory, '', to sys.path"""
186 if sys.path[0] != '':
186 if sys.path[0] != '':
187 sys.path.insert(0, '')
187 sys.path.insert(0, '')
188
188
189 def init_shell(self):
189 def init_shell(self):
190 raise NotImplementedError("Override in subclasses")
190 raise NotImplementedError("Override in subclasses")
191
191
192 def init_gui_pylab(self):
192 def init_gui_pylab(self):
193 """Enable GUI event loop integration, taking pylab into account."""
193 """Enable GUI event loop integration, taking pylab into account."""
194 if self.gui or self.pylab:
194 if self.gui or self.pylab:
195 shell = self.shell
195 shell = self.shell
196 try:
196 try:
197 if self.pylab:
197 if self.pylab:
198 gui, backend = pylabtools.find_gui_and_backend(self.pylab)
198 gui, backend = pylabtools.find_gui_and_backend(self.pylab)
199 self.log.info("Enabling GUI event loop integration, "
199 self.log.info("Enabling GUI event loop integration, "
200 "toolkit=%s, pylab=%s" % (gui, self.pylab))
200 "toolkit=%s, pylab=%s" % (gui, self.pylab))
201 shell.enable_pylab(gui, import_all=self.pylab_import_all, welcome_message=True)
201 shell.enable_pylab(gui, import_all=self.pylab_import_all, welcome_message=True)
202 else:
202 else:
203 self.log.info("Enabling GUI event loop integration, "
203 self.log.info("Enabling GUI event loop integration, "
204 "toolkit=%s" % self.gui)
204 "toolkit=%s" % self.gui)
205 shell.enable_gui(self.gui)
205 shell.enable_gui(self.gui)
206 except ImportError:
206 except ImportError:
207 self.log.warn("pylab mode doesn't work as matplotlib could not be found." + \
207 self.log.warn("pylab mode doesn't work as matplotlib could not be found." + \
208 "\nIs it installed on the system?")
208 "\nIs it installed on the system?")
209 self.shell.showtraceback()
209 self.shell.showtraceback()
210 except Exception:
210 except Exception:
211 self.log.warn("GUI event loop or pylab initialization failed")
211 self.log.warn("GUI event loop or pylab initialization failed")
212 self.shell.showtraceback()
212 self.shell.showtraceback()
213
213
214 def init_extensions(self):
214 def init_extensions(self):
215 """Load all IPython extensions in IPythonApp.extensions.
215 """Load all IPython extensions in IPythonApp.extensions.
216
216
217 This uses the :meth:`ExtensionManager.load_extensions` to load all
217 This uses the :meth:`ExtensionManager.load_extensions` to load all
218 the extensions listed in ``self.extensions``.
218 the extensions listed in ``self.extensions``.
219 """
219 """
220 try:
220 try:
221 self.log.debug("Loading IPython extensions...")
221 self.log.debug("Loading IPython extensions...")
222 extensions = self.default_extensions + self.extensions
222 extensions = self.default_extensions + self.extensions
223 for ext in extensions:
223 for ext in extensions:
224 try:
224 try:
225 self.log.info("Loading IPython extension: %s" % ext)
225 self.log.info("Loading IPython extension: %s" % ext)
226 self.shell.extension_manager.load_extension(ext)
226 self.shell.extension_manager.load_extension(ext)
227 except:
227 except:
228 self.log.warn("Error in loading extension: %s" % ext +
228 self.log.warn("Error in loading extension: %s" % ext +
229 "\nCheck your config files in %s" % self.profile_dir.location
229 "\nCheck your config files in %s" % self.profile_dir.location
230 )
230 )
231 self.shell.showtraceback()
231 self.shell.showtraceback()
232 except:
232 except:
233 self.log.warn("Unknown error in loading extensions:")
233 self.log.warn("Unknown error in loading extensions:")
234 self.shell.showtraceback()
234 self.shell.showtraceback()
235
235
236 def init_code(self):
236 def init_code(self):
237 """run the pre-flight code, specified via exec_lines"""
237 """run the pre-flight code, specified via exec_lines"""
238 self._run_startup_files()
238 self._run_startup_files()
239 self._run_exec_lines()
239 self._run_exec_lines()
240 self._run_exec_files()
240 self._run_exec_files()
241 self._run_cmd_line_code()
241 self._run_cmd_line_code()
242 self._run_module()
242 self._run_module()
243
243
244 # flush output, so itwon't be attached to the first cell
244 # flush output, so itwon't be attached to the first cell
245 sys.stdout.flush()
245 sys.stdout.flush()
246 sys.stderr.flush()
246 sys.stderr.flush()
247
247
248 # Hide variables defined here from %who etc.
248 # Hide variables defined here from %who etc.
249 self.shell.user_ns_hidden.update(self.shell.user_ns)
249 self.shell.user_ns_hidden.update(self.shell.user_ns)
250
250
251 def _run_exec_lines(self):
251 def _run_exec_lines(self):
252 """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
252 """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
253 if not self.exec_lines:
253 if not self.exec_lines:
254 return
254 return
255 try:
255 try:
256 self.log.debug("Running code from IPythonApp.exec_lines...")
256 self.log.debug("Running code from IPythonApp.exec_lines...")
257 for line in self.exec_lines:
257 for line in self.exec_lines:
258 try:
258 try:
259 self.log.info("Running code in user namespace: %s" %
259 self.log.info("Running code in user namespace: %s" %
260 line)
260 line)
261 self.shell.run_cell(line, store_history=False)
261 self.shell.run_cell(line, store_history=False)
262 except:
262 except:
263 self.log.warn("Error in executing line in user "
263 self.log.warn("Error in executing line in user "
264 "namespace: %s" % line)
264 "namespace: %s" % line)
265 self.shell.showtraceback()
265 self.shell.showtraceback()
266 except:
266 except:
267 self.log.warn("Unknown error in handling IPythonApp.exec_lines:")
267 self.log.warn("Unknown error in handling IPythonApp.exec_lines:")
268 self.shell.showtraceback()
268 self.shell.showtraceback()
269
269
270 def _exec_file(self, fname):
270 def _exec_file(self, fname):
271 try:
271 try:
272 full_filename = filefind(fname, [u'.', self.ipython_dir])
272 full_filename = filefind(fname, [u'.', self.ipython_dir])
273 except IOError as e:
273 except IOError as e:
274 self.log.warn("File not found: %r"%fname)
274 self.log.warn("File not found: %r"%fname)
275 return
275 return
276 # Make sure that the running script gets a proper sys.argv as if it
276 # Make sure that the running script gets a proper sys.argv as if it
277 # were run from a system shell.
277 # were run from a system shell.
278 save_argv = sys.argv
278 save_argv = sys.argv
279 sys.argv = [full_filename] + self.extra_args[1:]
279 sys.argv = [full_filename] + self.extra_args[1:]
280 # protect sys.argv from potential unicode strings on Python 2:
280 # protect sys.argv from potential unicode strings on Python 2:
281 if not py3compat.PY3:
281 if not py3compat.PY3:
282 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
282 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
283 try:
283 try:
284 if os.path.isfile(full_filename):
284 if os.path.isfile(full_filename):
285 self.log.info("Running file in user namespace: %s" %
285 self.log.info("Running file in user namespace: %s" %
286 full_filename)
286 full_filename)
287 # Ensure that __file__ is always defined to match Python
287 # Ensure that __file__ is always defined to match Python
288 # behavior.
288 # behavior.
289 with preserve_keys(self.shell.user_ns, '__file__'):
289 with preserve_keys(self.shell.user_ns, '__file__'):
290 self.shell.user_ns['__file__'] = fname
290 self.shell.user_ns['__file__'] = fname
291 if full_filename.endswith('.ipy'):
291 if full_filename.endswith('.ipy'):
292 self.shell.safe_execfile_ipy(full_filename)
292 self.shell.safe_execfile_ipy(full_filename)
293 else:
293 else:
294 # default to python, even without extension
294 # default to python, even without extension
295 self.shell.safe_execfile(full_filename,
295 self.shell.safe_execfile(full_filename,
296 self.shell.user_ns)
296 self.shell.user_ns)
297 finally:
297 finally:
298 sys.argv = save_argv
298 sys.argv = save_argv
299
299
300 def _run_startup_files(self):
300 def _run_startup_files(self):
301 """Run files from profile startup directory"""
301 """Run files from profile startup directory"""
302 startup_dir = self.profile_dir.startup_dir
302 startup_dir = self.profile_dir.startup_dir
303 startup_files = glob.glob(os.path.join(startup_dir, '*.py'))
303 startup_files = []
304 if os.environ.get('PYTHONSTARTUP', False):
305 startup_files.append(os.environ['PYTHONSTARTUP'])
306 startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
304 startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
307 startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
305 if not startup_files:
308 if not startup_files:
306 return
309 return
307
310
308 self.log.debug("Running startup files from %s...", startup_dir)
311 self.log.debug("Running startup files from %s...", startup_dir)
309 try:
312 try:
310 for fname in sorted(startup_files):
313 for fname in sorted(startup_files):
311 self._exec_file(fname)
314 self._exec_file(fname)
312 except:
315 except:
313 self.log.warn("Unknown error in handling startup files:")
316 self.log.warn("Unknown error in handling startup files:")
314 self.shell.showtraceback()
317 self.shell.showtraceback()
315
318
316 def _run_exec_files(self):
319 def _run_exec_files(self):
317 """Run files from IPythonApp.exec_files"""
320 """Run files from IPythonApp.exec_files"""
318 if not self.exec_files:
321 if not self.exec_files:
319 return
322 return
320
323
321 self.log.debug("Running files in IPythonApp.exec_files...")
324 self.log.debug("Running files in IPythonApp.exec_files...")
322 try:
325 try:
323 for fname in self.exec_files:
326 for fname in self.exec_files:
324 self._exec_file(fname)
327 self._exec_file(fname)
325 except:
328 except:
326 self.log.warn("Unknown error in handling IPythonApp.exec_files:")
329 self.log.warn("Unknown error in handling IPythonApp.exec_files:")
327 self.shell.showtraceback()
330 self.shell.showtraceback()
328
331
329 def _run_cmd_line_code(self):
332 def _run_cmd_line_code(self):
330 """Run code or file specified at the command-line"""
333 """Run code or file specified at the command-line"""
331 if self.code_to_run:
334 if self.code_to_run:
332 line = self.code_to_run
335 line = self.code_to_run
333 try:
336 try:
334 self.log.info("Running code given at command line (c=): %s" %
337 self.log.info("Running code given at command line (c=): %s" %
335 line)
338 line)
336 self.shell.run_cell(line, store_history=False)
339 self.shell.run_cell(line, store_history=False)
337 except:
340 except:
338 self.log.warn("Error in executing line in user namespace: %s" %
341 self.log.warn("Error in executing line in user namespace: %s" %
339 line)
342 line)
340 self.shell.showtraceback()
343 self.shell.showtraceback()
341
344
342 # Like Python itself, ignore the second if the first of these is present
345 # Like Python itself, ignore the second if the first of these is present
343 elif self.file_to_run:
346 elif self.file_to_run:
344 fname = self.file_to_run
347 fname = self.file_to_run
345 try:
348 try:
346 self._exec_file(fname)
349 self._exec_file(fname)
347 except:
350 except:
348 self.log.warn("Error in executing file in user namespace: %s" %
351 self.log.warn("Error in executing file in user namespace: %s" %
349 fname)
352 fname)
350 self.shell.showtraceback()
353 self.shell.showtraceback()
351
354
352 def _run_module(self):
355 def _run_module(self):
353 """Run module specified at the command-line."""
356 """Run module specified at the command-line."""
354 if self.module_to_run:
357 if self.module_to_run:
355 # Make sure that the module gets a proper sys.argv as if it were
358 # Make sure that the module gets a proper sys.argv as if it were
356 # run using `python -m`.
359 # run using `python -m`.
357 save_argv = sys.argv
360 save_argv = sys.argv
358 sys.argv = [sys.executable] + self.extra_args
361 sys.argv = [sys.executable] + self.extra_args
359 try:
362 try:
360 self.shell.safe_run_module(self.module_to_run,
363 self.shell.safe_run_module(self.module_to_run,
361 self.shell.user_ns)
364 self.shell.user_ns)
362 finally:
365 finally:
363 sys.argv = save_argv
366 sys.argv = save_argv
General Comments 0
You need to be logged in to leave comments. Login now