##// END OF EJS Templates
add CWD to sys.path *after* stdlib...
Min RK -
Show More
@@ -1,408 +1,425 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
6
7 # Copyright (c) IPython Development Team.
7 # Copyright (c) IPython Development Team.
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9
9
10 import glob
10 import glob
11 from itertools import chain
11 from itertools import chain
12 import os
12 import os
13 import sys
13 import sys
14
14
15 from traitlets.config.application import boolean_flag
15 from traitlets.config.application import boolean_flag
16 from traitlets.config.configurable import Configurable
16 from traitlets.config.configurable import Configurable
17 from traitlets.config.loader import Config
17 from traitlets.config.loader import Config
18 from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS
18 from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS
19 from IPython.core import pylabtools
19 from IPython.core import pylabtools
20 from IPython.utils.contexts import preserve_keys
20 from IPython.utils.contexts import preserve_keys
21 from IPython.utils.path import filefind
21 from IPython.utils.path import filefind
22 from traitlets import (
22 from traitlets import (
23 Unicode, Instance, List, Bool, CaselessStrEnum, observe,
23 Unicode, Instance, List, Bool, CaselessStrEnum, observe,
24 )
24 )
25 from IPython.terminal import pt_inputhooks
25 from IPython.terminal import pt_inputhooks
26
26
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Aliases and Flags
28 # Aliases and Flags
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
30
31 gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases))
31 gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases))
32
32
33 backend_keys = sorted(pylabtools.backends.keys())
33 backend_keys = sorted(pylabtools.backends.keys())
34 backend_keys.insert(0, 'auto')
34 backend_keys.insert(0, 'auto')
35
35
36 shell_flags = {}
36 shell_flags = {}
37
37
38 addflag = lambda *args: shell_flags.update(boolean_flag(*args))
38 addflag = lambda *args: shell_flags.update(boolean_flag(*args))
39 addflag('autoindent', 'InteractiveShell.autoindent',
39 addflag('autoindent', 'InteractiveShell.autoindent',
40 'Turn on autoindenting.', 'Turn off autoindenting.'
40 'Turn on autoindenting.', 'Turn off autoindenting.'
41 )
41 )
42 addflag('automagic', 'InteractiveShell.automagic',
42 addflag('automagic', 'InteractiveShell.automagic',
43 """Turn on the auto calling of magic commands. Type %%magic at the
43 """Turn on the auto calling of magic commands. Type %%magic at the
44 IPython prompt for more information.""",
44 IPython prompt for more information.""",
45 'Turn off the auto calling of magic commands.'
45 'Turn off the auto calling of magic commands.'
46 )
46 )
47 addflag('pdb', 'InteractiveShell.pdb',
47 addflag('pdb', 'InteractiveShell.pdb',
48 "Enable auto calling the pdb debugger after every exception.",
48 "Enable auto calling the pdb debugger after every exception.",
49 "Disable auto calling the pdb debugger after every exception."
49 "Disable auto calling the pdb debugger after every exception."
50 )
50 )
51 addflag('pprint', 'PlainTextFormatter.pprint',
51 addflag('pprint', 'PlainTextFormatter.pprint',
52 "Enable auto pretty printing of results.",
52 "Enable auto pretty printing of results.",
53 "Disable auto pretty printing of results."
53 "Disable auto pretty printing of results."
54 )
54 )
55 addflag('color-info', 'InteractiveShell.color_info',
55 addflag('color-info', 'InteractiveShell.color_info',
56 """IPython can display information about objects via a set of functions,
56 """IPython can display information about objects via a set of functions,
57 and optionally can use colors for this, syntax highlighting
57 and optionally can use colors for this, syntax highlighting
58 source code and various other elements. This is on by default, but can cause
58 source code and various other elements. This is on by default, but can cause
59 problems with some pagers. If you see such problems, you can disable the
59 problems with some pagers. If you see such problems, you can disable the
60 colours.""",
60 colours.""",
61 "Disable using colors for info related things."
61 "Disable using colors for info related things."
62 )
62 )
63 nosep_config = Config()
63 nosep_config = Config()
64 nosep_config.InteractiveShell.separate_in = ''
64 nosep_config.InteractiveShell.separate_in = ''
65 nosep_config.InteractiveShell.separate_out = ''
65 nosep_config.InteractiveShell.separate_out = ''
66 nosep_config.InteractiveShell.separate_out2 = ''
66 nosep_config.InteractiveShell.separate_out2 = ''
67
67
68 shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
68 shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
69 shell_flags['pylab'] = (
69 shell_flags['pylab'] = (
70 {'InteractiveShellApp' : {'pylab' : 'auto'}},
70 {'InteractiveShellApp' : {'pylab' : 'auto'}},
71 """Pre-load matplotlib and numpy for interactive use with
71 """Pre-load matplotlib and numpy for interactive use with
72 the default matplotlib backend."""
72 the default matplotlib backend."""
73 )
73 )
74 shell_flags['matplotlib'] = (
74 shell_flags['matplotlib'] = (
75 {'InteractiveShellApp' : {'matplotlib' : 'auto'}},
75 {'InteractiveShellApp' : {'matplotlib' : 'auto'}},
76 """Configure matplotlib for interactive use with
76 """Configure matplotlib for interactive use with
77 the default matplotlib backend."""
77 the default matplotlib backend."""
78 )
78 )
79
79
80 # it's possible we don't want short aliases for *all* of these:
80 # it's possible we don't want short aliases for *all* of these:
81 shell_aliases = dict(
81 shell_aliases = dict(
82 autocall='InteractiveShell.autocall',
82 autocall='InteractiveShell.autocall',
83 colors='InteractiveShell.colors',
83 colors='InteractiveShell.colors',
84 logfile='InteractiveShell.logfile',
84 logfile='InteractiveShell.logfile',
85 logappend='InteractiveShell.logappend',
85 logappend='InteractiveShell.logappend',
86 c='InteractiveShellApp.code_to_run',
86 c='InteractiveShellApp.code_to_run',
87 m='InteractiveShellApp.module_to_run',
87 m='InteractiveShellApp.module_to_run',
88 ext='InteractiveShellApp.extra_extension',
88 ext='InteractiveShellApp.extra_extension',
89 gui='InteractiveShellApp.gui',
89 gui='InteractiveShellApp.gui',
90 pylab='InteractiveShellApp.pylab',
90 pylab='InteractiveShellApp.pylab',
91 matplotlib='InteractiveShellApp.matplotlib',
91 matplotlib='InteractiveShellApp.matplotlib',
92 )
92 )
93 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
93 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
94
94
95 #-----------------------------------------------------------------------------
95 #-----------------------------------------------------------------------------
96 # Main classes and functions
96 # Main classes and functions
97 #-----------------------------------------------------------------------------
97 #-----------------------------------------------------------------------------
98
98
99 class InteractiveShellApp(Configurable):
99 class InteractiveShellApp(Configurable):
100 """A Mixin for applications that start InteractiveShell instances.
100 """A Mixin for applications that start InteractiveShell instances.
101
101
102 Provides configurables for loading extensions and executing files
102 Provides configurables for loading extensions and executing files
103 as part of configuring a Shell environment.
103 as part of configuring a Shell environment.
104
104
105 The following methods should be called by the :meth:`initialize` method
105 The following methods should be called by the :meth:`initialize` method
106 of the subclass:
106 of the subclass:
107
107
108 - :meth:`init_path`
108 - :meth:`init_path`
109 - :meth:`init_shell` (to be implemented by the subclass)
109 - :meth:`init_shell` (to be implemented by the subclass)
110 - :meth:`init_gui_pylab`
110 - :meth:`init_gui_pylab`
111 - :meth:`init_extensions`
111 - :meth:`init_extensions`
112 - :meth:`init_code`
112 - :meth:`init_code`
113 """
113 """
114 extensions = List(Unicode(),
114 extensions = List(Unicode(),
115 help="A list of dotted module names of IPython extensions to load."
115 help="A list of dotted module names of IPython extensions to load."
116 ).tag(config=True)
116 ).tag(config=True)
117 extra_extension = Unicode('',
117 extra_extension = Unicode('',
118 help="dotted module name of an IPython extension to load."
118 help="dotted module name of an IPython extension to load."
119 ).tag(config=True)
119 ).tag(config=True)
120
120
121 reraise_ipython_extension_failures = Bool(False,
121 reraise_ipython_extension_failures = Bool(False,
122 help="Reraise exceptions encountered loading IPython extensions?",
122 help="Reraise exceptions encountered loading IPython extensions?",
123 ).tag(config=True)
123 ).tag(config=True)
124
124
125 # Extensions that are always loaded (not configurable)
125 # Extensions that are always loaded (not configurable)
126 default_extensions = List(Unicode(), [u'storemagic']).tag(config=False)
126 default_extensions = List(Unicode(), [u'storemagic']).tag(config=False)
127
127
128 hide_initial_ns = Bool(True,
128 hide_initial_ns = Bool(True,
129 help="""Should variables loaded at startup (by startup files, exec_lines, etc.)
129 help="""Should variables loaded at startup (by startup files, exec_lines, etc.)
130 be hidden from tools like %who?"""
130 be hidden from tools like %who?"""
131 ).tag(config=True)
131 ).tag(config=True)
132
132
133 exec_files = List(Unicode(),
133 exec_files = List(Unicode(),
134 help="""List of files to run at IPython startup."""
134 help="""List of files to run at IPython startup."""
135 ).tag(config=True)
135 ).tag(config=True)
136 exec_PYTHONSTARTUP = Bool(True,
136 exec_PYTHONSTARTUP = Bool(True,
137 help="""Run the file referenced by the PYTHONSTARTUP environment
137 help="""Run the file referenced by the PYTHONSTARTUP environment
138 variable at IPython startup."""
138 variable at IPython startup."""
139 ).tag(config=True)
139 ).tag(config=True)
140 file_to_run = Unicode('',
140 file_to_run = Unicode('',
141 help="""A file to be run""").tag(config=True)
141 help="""A file to be run""").tag(config=True)
142
142
143 exec_lines = List(Unicode(),
143 exec_lines = List(Unicode(),
144 help="""lines of code to run at IPython startup."""
144 help="""lines of code to run at IPython startup."""
145 ).tag(config=True)
145 ).tag(config=True)
146 code_to_run = Unicode('',
146 code_to_run = Unicode('',
147 help="Execute the given command string."
147 help="Execute the given command string."
148 ).tag(config=True)
148 ).tag(config=True)
149 module_to_run = Unicode('',
149 module_to_run = Unicode('',
150 help="Run the module as a script."
150 help="Run the module as a script."
151 ).tag(config=True)
151 ).tag(config=True)
152 gui = CaselessStrEnum(gui_keys, allow_none=True,
152 gui = CaselessStrEnum(gui_keys, allow_none=True,
153 help="Enable GUI event loop integration with any of {0}.".format(gui_keys)
153 help="Enable GUI event loop integration with any of {0}.".format(gui_keys)
154 ).tag(config=True)
154 ).tag(config=True)
155 matplotlib = CaselessStrEnum(backend_keys, allow_none=True,
155 matplotlib = CaselessStrEnum(backend_keys, allow_none=True,
156 help="""Configure matplotlib for interactive use with
156 help="""Configure matplotlib for interactive use with
157 the default matplotlib backend."""
157 the default matplotlib backend."""
158 ).tag(config=True)
158 ).tag(config=True)
159 pylab = CaselessStrEnum(backend_keys, allow_none=True,
159 pylab = CaselessStrEnum(backend_keys, allow_none=True,
160 help="""Pre-load matplotlib and numpy for interactive use,
160 help="""Pre-load matplotlib and numpy for interactive use,
161 selecting a particular matplotlib backend and loop integration.
161 selecting a particular matplotlib backend and loop integration.
162 """
162 """
163 ).tag(config=True)
163 ).tag(config=True)
164 pylab_import_all = Bool(True,
164 pylab_import_all = Bool(True,
165 help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
165 help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
166 and an ``import *`` is done from numpy and pylab, when using pylab mode.
166 and an ``import *`` is done from numpy and pylab, when using pylab mode.
167
167
168 When False, pylab mode should not import any names into the user namespace.
168 When False, pylab mode should not import any names into the user namespace.
169 """
169 """
170 ).tag(config=True)
170 ).tag(config=True)
171 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
171 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
172 allow_none=True)
172 allow_none=True)
173 # whether interact-loop should start
173 # whether interact-loop should start
174 interact = Bool(True)
174 interact = Bool(True)
175
175
176 user_ns = Instance(dict, args=None, allow_none=True)
176 user_ns = Instance(dict, args=None, allow_none=True)
177 @observe('user_ns')
177 @observe('user_ns')
178 def _user_ns_changed(self, change):
178 def _user_ns_changed(self, change):
179 if self.shell is not None:
179 if self.shell is not None:
180 self.shell.user_ns = change['new']
180 self.shell.user_ns = change['new']
181 self.shell.init_user_ns()
181 self.shell.init_user_ns()
182
182
183 def init_path(self):
183 def init_path(self):
184 """Add current working directory, '', to sys.path"""
184 """Add current working directory, '', to sys.path
185 if sys.path[0] != '':
185
186 sys.path.insert(0, '')
186 Unlike Python's default, we insert before the first `site-packages`
187 or `dist-packages` directory,
188 so that it is after the standard library.
189
190 .. versionchanged:: 7.2
191 Try to insert after the standard library, instead of first.
192 """
193 if '' in sys.path:
194 return
195 for idx, path in enumerate(sys.path):
196 parent, last_part = os.path.split(path)
197 if last_part in {'site-packages', 'dist-packages'}:
198 break
199 else:
200 # no site-packages or dist-packages found (?!)
201 # back to original behavior of inserting at the front
202 idx = 0
203 sys.path.insert(idx, '')
187
204
188 def init_shell(self):
205 def init_shell(self):
189 raise NotImplementedError("Override in subclasses")
206 raise NotImplementedError("Override in subclasses")
190
207
191 def init_gui_pylab(self):
208 def init_gui_pylab(self):
192 """Enable GUI event loop integration, taking pylab into account."""
209 """Enable GUI event loop integration, taking pylab into account."""
193 enable = False
210 enable = False
194 shell = self.shell
211 shell = self.shell
195 if self.pylab:
212 if self.pylab:
196 enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all)
213 enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all)
197 key = self.pylab
214 key = self.pylab
198 elif self.matplotlib:
215 elif self.matplotlib:
199 enable = shell.enable_matplotlib
216 enable = shell.enable_matplotlib
200 key = self.matplotlib
217 key = self.matplotlib
201 elif self.gui:
218 elif self.gui:
202 enable = shell.enable_gui
219 enable = shell.enable_gui
203 key = self.gui
220 key = self.gui
204
221
205 if not enable:
222 if not enable:
206 return
223 return
207
224
208 try:
225 try:
209 r = enable(key)
226 r = enable(key)
210 except ImportError:
227 except ImportError:
211 self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?")
228 self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?")
212 self.shell.showtraceback()
229 self.shell.showtraceback()
213 return
230 return
214 except Exception:
231 except Exception:
215 self.log.warning("GUI event loop or pylab initialization failed")
232 self.log.warning("GUI event loop or pylab initialization failed")
216 self.shell.showtraceback()
233 self.shell.showtraceback()
217 return
234 return
218
235
219 if isinstance(r, tuple):
236 if isinstance(r, tuple):
220 gui, backend = r[:2]
237 gui, backend = r[:2]
221 self.log.info("Enabling GUI event loop integration, "
238 self.log.info("Enabling GUI event loop integration, "
222 "eventloop=%s, matplotlib=%s", gui, backend)
239 "eventloop=%s, matplotlib=%s", gui, backend)
223 if key == "auto":
240 if key == "auto":
224 print("Using matplotlib backend: %s" % backend)
241 print("Using matplotlib backend: %s" % backend)
225 else:
242 else:
226 gui = r
243 gui = r
227 self.log.info("Enabling GUI event loop integration, "
244 self.log.info("Enabling GUI event loop integration, "
228 "eventloop=%s", gui)
245 "eventloop=%s", gui)
229
246
230 def init_extensions(self):
247 def init_extensions(self):
231 """Load all IPython extensions in IPythonApp.extensions.
248 """Load all IPython extensions in IPythonApp.extensions.
232
249
233 This uses the :meth:`ExtensionManager.load_extensions` to load all
250 This uses the :meth:`ExtensionManager.load_extensions` to load all
234 the extensions listed in ``self.extensions``.
251 the extensions listed in ``self.extensions``.
235 """
252 """
236 try:
253 try:
237 self.log.debug("Loading IPython extensions...")
254 self.log.debug("Loading IPython extensions...")
238 extensions = self.default_extensions + self.extensions
255 extensions = self.default_extensions + self.extensions
239 if self.extra_extension:
256 if self.extra_extension:
240 extensions.append(self.extra_extension)
257 extensions.append(self.extra_extension)
241 for ext in extensions:
258 for ext in extensions:
242 try:
259 try:
243 self.log.info("Loading IPython extension: %s" % ext)
260 self.log.info("Loading IPython extension: %s" % ext)
244 self.shell.extension_manager.load_extension(ext)
261 self.shell.extension_manager.load_extension(ext)
245 except:
262 except:
246 if self.reraise_ipython_extension_failures:
263 if self.reraise_ipython_extension_failures:
247 raise
264 raise
248 msg = ("Error in loading extension: {ext}\n"
265 msg = ("Error in loading extension: {ext}\n"
249 "Check your config files in {location}".format(
266 "Check your config files in {location}".format(
250 ext=ext,
267 ext=ext,
251 location=self.profile_dir.location
268 location=self.profile_dir.location
252 ))
269 ))
253 self.log.warning(msg, exc_info=True)
270 self.log.warning(msg, exc_info=True)
254 except:
271 except:
255 if self.reraise_ipython_extension_failures:
272 if self.reraise_ipython_extension_failures:
256 raise
273 raise
257 self.log.warning("Unknown error in loading extensions:", exc_info=True)
274 self.log.warning("Unknown error in loading extensions:", exc_info=True)
258
275
259 def init_code(self):
276 def init_code(self):
260 """run the pre-flight code, specified via exec_lines"""
277 """run the pre-flight code, specified via exec_lines"""
261 self._run_startup_files()
278 self._run_startup_files()
262 self._run_exec_lines()
279 self._run_exec_lines()
263 self._run_exec_files()
280 self._run_exec_files()
264
281
265 # Hide variables defined here from %who etc.
282 # Hide variables defined here from %who etc.
266 if self.hide_initial_ns:
283 if self.hide_initial_ns:
267 self.shell.user_ns_hidden.update(self.shell.user_ns)
284 self.shell.user_ns_hidden.update(self.shell.user_ns)
268
285
269 # command-line execution (ipython -i script.py, ipython -m module)
286 # command-line execution (ipython -i script.py, ipython -m module)
270 # should *not* be excluded from %whos
287 # should *not* be excluded from %whos
271 self._run_cmd_line_code()
288 self._run_cmd_line_code()
272 self._run_module()
289 self._run_module()
273
290
274 # flush output, so itwon't be attached to the first cell
291 # flush output, so itwon't be attached to the first cell
275 sys.stdout.flush()
292 sys.stdout.flush()
276 sys.stderr.flush()
293 sys.stderr.flush()
277
294
278 def _run_exec_lines(self):
295 def _run_exec_lines(self):
279 """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
296 """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
280 if not self.exec_lines:
297 if not self.exec_lines:
281 return
298 return
282 try:
299 try:
283 self.log.debug("Running code from IPythonApp.exec_lines...")
300 self.log.debug("Running code from IPythonApp.exec_lines...")
284 for line in self.exec_lines:
301 for line in self.exec_lines:
285 try:
302 try:
286 self.log.info("Running code in user namespace: %s" %
303 self.log.info("Running code in user namespace: %s" %
287 line)
304 line)
288 self.shell.run_cell(line, store_history=False)
305 self.shell.run_cell(line, store_history=False)
289 except:
306 except:
290 self.log.warning("Error in executing line in user "
307 self.log.warning("Error in executing line in user "
291 "namespace: %s" % line)
308 "namespace: %s" % line)
292 self.shell.showtraceback()
309 self.shell.showtraceback()
293 except:
310 except:
294 self.log.warning("Unknown error in handling IPythonApp.exec_lines:")
311 self.log.warning("Unknown error in handling IPythonApp.exec_lines:")
295 self.shell.showtraceback()
312 self.shell.showtraceback()
296
313
297 def _exec_file(self, fname, shell_futures=False):
314 def _exec_file(self, fname, shell_futures=False):
298 try:
315 try:
299 full_filename = filefind(fname, [u'.', self.ipython_dir])
316 full_filename = filefind(fname, [u'.', self.ipython_dir])
300 except IOError:
317 except IOError:
301 self.log.warning("File not found: %r"%fname)
318 self.log.warning("File not found: %r"%fname)
302 return
319 return
303 # Make sure that the running script gets a proper sys.argv as if it
320 # Make sure that the running script gets a proper sys.argv as if it
304 # were run from a system shell.
321 # were run from a system shell.
305 save_argv = sys.argv
322 save_argv = sys.argv
306 sys.argv = [full_filename] + self.extra_args[1:]
323 sys.argv = [full_filename] + self.extra_args[1:]
307 try:
324 try:
308 if os.path.isfile(full_filename):
325 if os.path.isfile(full_filename):
309 self.log.info("Running file in user namespace: %s" %
326 self.log.info("Running file in user namespace: %s" %
310 full_filename)
327 full_filename)
311 # Ensure that __file__ is always defined to match Python
328 # Ensure that __file__ is always defined to match Python
312 # behavior.
329 # behavior.
313 with preserve_keys(self.shell.user_ns, '__file__'):
330 with preserve_keys(self.shell.user_ns, '__file__'):
314 self.shell.user_ns['__file__'] = fname
331 self.shell.user_ns['__file__'] = fname
315 if full_filename.endswith('.ipy'):
332 if full_filename.endswith('.ipy'):
316 self.shell.safe_execfile_ipy(full_filename,
333 self.shell.safe_execfile_ipy(full_filename,
317 shell_futures=shell_futures)
334 shell_futures=shell_futures)
318 else:
335 else:
319 # default to python, even without extension
336 # default to python, even without extension
320 self.shell.safe_execfile(full_filename,
337 self.shell.safe_execfile(full_filename,
321 self.shell.user_ns,
338 self.shell.user_ns,
322 shell_futures=shell_futures,
339 shell_futures=shell_futures,
323 raise_exceptions=True)
340 raise_exceptions=True)
324 finally:
341 finally:
325 sys.argv = save_argv
342 sys.argv = save_argv
326
343
327 def _run_startup_files(self):
344 def _run_startup_files(self):
328 """Run files from profile startup directory"""
345 """Run files from profile startup directory"""
329 startup_dirs = [self.profile_dir.startup_dir] + [
346 startup_dirs = [self.profile_dir.startup_dir] + [
330 os.path.join(p, 'startup') for p in chain(ENV_CONFIG_DIRS, SYSTEM_CONFIG_DIRS)
347 os.path.join(p, 'startup') for p in chain(ENV_CONFIG_DIRS, SYSTEM_CONFIG_DIRS)
331 ]
348 ]
332 startup_files = []
349 startup_files = []
333
350
334 if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \
351 if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \
335 not (self.file_to_run or self.code_to_run or self.module_to_run):
352 not (self.file_to_run or self.code_to_run or self.module_to_run):
336 python_startup = os.environ['PYTHONSTARTUP']
353 python_startup = os.environ['PYTHONSTARTUP']
337 self.log.debug("Running PYTHONSTARTUP file %s...", python_startup)
354 self.log.debug("Running PYTHONSTARTUP file %s...", python_startup)
338 try:
355 try:
339 self._exec_file(python_startup)
356 self._exec_file(python_startup)
340 except:
357 except:
341 self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup)
358 self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup)
342 self.shell.showtraceback()
359 self.shell.showtraceback()
343 for startup_dir in startup_dirs[::-1]:
360 for startup_dir in startup_dirs[::-1]:
344 startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
361 startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
345 startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
362 startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
346 if not startup_files:
363 if not startup_files:
347 return
364 return
348
365
349 self.log.debug("Running startup files from %s...", startup_dir)
366 self.log.debug("Running startup files from %s...", startup_dir)
350 try:
367 try:
351 for fname in sorted(startup_files):
368 for fname in sorted(startup_files):
352 self._exec_file(fname)
369 self._exec_file(fname)
353 except:
370 except:
354 self.log.warning("Unknown error in handling startup files:")
371 self.log.warning("Unknown error in handling startup files:")
355 self.shell.showtraceback()
372 self.shell.showtraceback()
356
373
357 def _run_exec_files(self):
374 def _run_exec_files(self):
358 """Run files from IPythonApp.exec_files"""
375 """Run files from IPythonApp.exec_files"""
359 if not self.exec_files:
376 if not self.exec_files:
360 return
377 return
361
378
362 self.log.debug("Running files in IPythonApp.exec_files...")
379 self.log.debug("Running files in IPythonApp.exec_files...")
363 try:
380 try:
364 for fname in self.exec_files:
381 for fname in self.exec_files:
365 self._exec_file(fname)
382 self._exec_file(fname)
366 except:
383 except:
367 self.log.warning("Unknown error in handling IPythonApp.exec_files:")
384 self.log.warning("Unknown error in handling IPythonApp.exec_files:")
368 self.shell.showtraceback()
385 self.shell.showtraceback()
369
386
370 def _run_cmd_line_code(self):
387 def _run_cmd_line_code(self):
371 """Run code or file specified at the command-line"""
388 """Run code or file specified at the command-line"""
372 if self.code_to_run:
389 if self.code_to_run:
373 line = self.code_to_run
390 line = self.code_to_run
374 try:
391 try:
375 self.log.info("Running code given at command line (c=): %s" %
392 self.log.info("Running code given at command line (c=): %s" %
376 line)
393 line)
377 self.shell.run_cell(line, store_history=False)
394 self.shell.run_cell(line, store_history=False)
378 except:
395 except:
379 self.log.warning("Error in executing line in user namespace: %s" %
396 self.log.warning("Error in executing line in user namespace: %s" %
380 line)
397 line)
381 self.shell.showtraceback()
398 self.shell.showtraceback()
382 if not self.interact:
399 if not self.interact:
383 self.exit(1)
400 self.exit(1)
384
401
385 # Like Python itself, ignore the second if the first of these is present
402 # Like Python itself, ignore the second if the first of these is present
386 elif self.file_to_run:
403 elif self.file_to_run:
387 fname = self.file_to_run
404 fname = self.file_to_run
388 if os.path.isdir(fname):
405 if os.path.isdir(fname):
389 fname = os.path.join(fname, "__main__.py")
406 fname = os.path.join(fname, "__main__.py")
390 try:
407 try:
391 self._exec_file(fname, shell_futures=True)
408 self._exec_file(fname, shell_futures=True)
392 except:
409 except:
393 self.shell.showtraceback(tb_offset=4)
410 self.shell.showtraceback(tb_offset=4)
394 if not self.interact:
411 if not self.interact:
395 self.exit(1)
412 self.exit(1)
396
413
397 def _run_module(self):
414 def _run_module(self):
398 """Run module specified at the command-line."""
415 """Run module specified at the command-line."""
399 if self.module_to_run:
416 if self.module_to_run:
400 # Make sure that the module gets a proper sys.argv as if it were
417 # Make sure that the module gets a proper sys.argv as if it were
401 # run using `python -m`.
418 # run using `python -m`.
402 save_argv = sys.argv
419 save_argv = sys.argv
403 sys.argv = [sys.executable] + self.extra_args
420 sys.argv = [sys.executable] + self.extra_args
404 try:
421 try:
405 self.shell.safe_run_module(self.module_to_run,
422 self.shell.safe_run_module(self.module_to_run,
406 self.shell.user_ns)
423 self.shell.user_ns)
407 finally:
424 finally:
408 sys.argv = save_argv
425 sys.argv = save_argv
General Comments 0
You need to be logged in to leave comments. Login now