##// END OF EJS Templates
Add mechanism to specify environment variables in kernel spec
Thomas Kluyver -
Show More
@@ -7,7 +7,7 b' pjoin = os.path.join'
7
7
8 from IPython.utils.path import get_ipython_dir
8 from IPython.utils.path import get_ipython_dir
9 from IPython.utils.py3compat import PY3
9 from IPython.utils.py3compat import PY3
10 from IPython.utils.traitlets import HasTraits, List, Unicode
10 from IPython.utils.traitlets import HasTraits, List, Unicode, Dict
11
11
12 USER_KERNEL_DIR = pjoin(get_ipython_dir(), 'kernels')
12 USER_KERNEL_DIR = pjoin(get_ipython_dir(), 'kernels')
13
13
@@ -33,6 +33,7 b' class KernelSpec(HasTraits):'
33 display_name = Unicode()
33 display_name = Unicode()
34 language = Unicode()
34 language = Unicode()
35 codemirror_mode = None
35 codemirror_mode = None
36 env = Dict()
36
37
37 resource_dir = Unicode()
38 resource_dir = Unicode()
38
39
@@ -1,21 +1,8 b''
1 """Utilities for launching kernels
1 """Utilities for launching kernels
2
3 Authors:
4
5 * Min Ragan-Kelley
6
7 """
2 """
8
3
9 #-----------------------------------------------------------------------------
4 # Copyright (c) IPython Development Team.
10 # Copyright (C) 2013 The IPython Development Team
5 # Distributed under the terms of the Modified BSD License.
11 #
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
19
6
20 import os
7 import os
21 import sys
8 import sys
@@ -24,9 +11,6 b' from subprocess import Popen, PIPE'
24 from IPython.utils.encoding import getdefaultencoding
11 from IPython.utils.encoding import getdefaultencoding
25 from IPython.utils.py3compat import cast_bytes_py2
12 from IPython.utils.py3compat import cast_bytes_py2
26
13
27 #-----------------------------------------------------------------------------
28 # Launching Kernels
29 #-----------------------------------------------------------------------------
30
14
31 def swallow_argv(argv, aliases=None, flags=None):
15 def swallow_argv(argv, aliases=None, flags=None):
32 """strip frontend-specific aliases and flags from an argument list
16 """strip frontend-specific aliases and flags from an argument list
@@ -136,7 +120,7 b' def make_ipkernel_cmd(code, executable=None, extra_arguments=[], **kw):'
136 return arguments
120 return arguments
137
121
138
122
139 def launch_kernel(cmd, stdin=None, stdout=None, stderr=None,
123 def launch_kernel(cmd, stdin=None, stdout=None, stderr=None, env=None,
140 independent=False,
124 independent=False,
141 cwd=None, ipython_kernel=True,
125 cwd=None, ipython_kernel=True,
142 **kw
126 **kw
@@ -221,7 +205,7 b' def launch_kernel(cmd, stdin=None, stdout=None, stderr=None,'
221 if independent:
205 if independent:
222 proc = Popen(cmd,
206 proc = Popen(cmd,
223 creationflags=512, # CREATE_NEW_PROCESS_GROUP
207 creationflags=512, # CREATE_NEW_PROCESS_GROUP
224 stdin=_stdin, stdout=_stdout, stderr=_stderr, env=os.environ)
208 stdin=_stdin, stdout=_stdout, stderr=_stderr, env=env)
225 else:
209 else:
226 if ipython_kernel:
210 if ipython_kernel:
227 try:
211 try:
@@ -238,7 +222,7 b' def launch_kernel(cmd, stdin=None, stdout=None, stderr=None,'
238
222
239
223
240 proc = Popen(cmd,
224 proc = Popen(cmd,
241 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=os.environ)
225 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=env)
242
226
243 # Attach the interrupt event to the Popen objet so it can be used later.
227 # Attach the interrupt event to the Popen objet so it can be used later.
244 proc.win32_interrupt_event = interrupt_event
228 proc.win32_interrupt_event = interrupt_event
@@ -246,12 +230,12 b' def launch_kernel(cmd, stdin=None, stdout=None, stderr=None,'
246 else:
230 else:
247 if independent:
231 if independent:
248 proc = Popen(cmd, preexec_fn=lambda: os.setsid(),
232 proc = Popen(cmd, preexec_fn=lambda: os.setsid(),
249 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=os.environ)
233 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=env)
250 else:
234 else:
251 if ipython_kernel:
235 if ipython_kernel:
252 cmd += ['--parent=1']
236 cmd += ['--parent=1']
253 proc = Popen(cmd,
237 proc = Popen(cmd,
254 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=os.environ)
238 stdin=_stdin, stdout=_stdout, stderr=_stderr, cwd=cwd, env=env)
255
239
256 # Clean up pipes created to work around Popen bug.
240 # Clean up pipes created to work around Popen bug.
257 if redirect_in:
241 if redirect_in:
@@ -1,19 +1,12 b''
1 """Base class to manage a running kernel"""
1 """Base class to manage a running kernel"""
2
2
3 #-----------------------------------------------------------------------------
3 # Copyright (c) IPython Development Team.
4 # Copyright (C) 2013 The IPython Development Team
4 # Distributed under the terms of the Modified BSD License.
5 #
6 # Distributed under the terms of the BSD License. The full license is in
7 # the file COPYING, distributed as part of this software.
8 #-----------------------------------------------------------------------------
9
10 #-----------------------------------------------------------------------------
11 # Imports
12 #-----------------------------------------------------------------------------
13
5
14 from __future__ import absolute_import
6 from __future__ import absolute_import
15
7
16 # Standard library imports
8 # Standard library imports
9 import os
17 import re
10 import re
18 import signal
11 import signal
19 import sys
12 import sys
@@ -40,9 +33,6 b' from .managerabc import ('
40 KernelManagerABC
33 KernelManagerABC
41 )
34 )
42
35
43 #-----------------------------------------------------------------------------
44 # Main kernel manager class
45 #-----------------------------------------------------------------------------
46
36
47 class KernelManager(LoggingConfigurable, ConnectionFileMixin):
37 class KernelManager(LoggingConfigurable, ConnectionFileMixin):
48 """Manages a single kernel in a subprocess on this host.
38 """Manages a single kernel in a subprocess on this host.
@@ -232,8 +222,15 b' class KernelManager(LoggingConfigurable, ConnectionFileMixin):'
232 self._launch_args = kw.copy()
222 self._launch_args = kw.copy()
233 # build the Popen cmd
223 # build the Popen cmd
234 kernel_cmd = self.format_kernel_cmd(**kw)
224 kernel_cmd = self.format_kernel_cmd(**kw)
225 if self.kernel_cmd:
226 # If kernel_cmd has been set manually, don't refer to a kernel spec
227 env = os.environ
228 else:
229 # Environment variables from kernel spec are added to os.environ
230 env = os.environ.copy()
231 env.update(self.kernel_spec.env or {})
235 # launch the kernel subprocess
232 # launch the kernel subprocess
236 self.kernel = self._launch_kernel(kernel_cmd,
233 self.kernel = self._launch_kernel(kernel_cmd, env=env,
237 ipython_kernel=self.ipython_kernel,
234 ipython_kernel=self.ipython_kernel,
238 **kw)
235 **kw)
239 self.start_restarter()
236 self.start_restarter()
@@ -402,9 +399,5 b' class KernelManager(LoggingConfigurable, ConnectionFileMixin):'
402 return False
399 return False
403
400
404
401
405 #-----------------------------------------------------------------------------
406 # ABC Registration
407 #-----------------------------------------------------------------------------
408
409 KernelManagerABC.register(KernelManager)
402 KernelManagerABC.register(KernelManager)
410
403
General Comments 0
You need to be logged in to leave comments. Login now