Skip to content

Instantly share code, notes, and snippets.

@pelson
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pelson/ca05c73f4027371f6de4 to your computer and use it in GitHub Desktop.
Save pelson/ca05c73f4027371f6de4 to your computer and use it in GitHub Desktop.
Changes to get IPython 3 to list kernels based on conda environments (requires conda on the path, and for the post-link.sh to be executed for each IPython enabled environment). This is a hack, and I expect there is a better way of doing this...
diff --git IPython/kernel/kernelspec.py IPython/kernel/kernelspec.py
index 0df16c7..d2df218 100644
--- IPython/kernel/kernelspec.py
+++ IPython/kernel/kernelspec.py
@@ -1,7 +1,9 @@
import io
import json
+import glob
import os
import shutil
+import subprocess
import sys
pjoin = os.path.join
@@ -22,9 +24,26 @@
SYSTEM_KERNEL_DIRS = ["/usr/share/jupyter/kernels",
"/usr/local/share/jupyter/kernels",
]
-
+
+def conda_envs():
+ # Get conda off the path.
+ conda = subprocess.check_output(['which', 'conda']).strip()
+ if not os.path.exists(conda):
+ return []
+
+ envs = subprocess.check_output([conda, 'env', 'list', '--json'])
+
+ envs = json.loads(envs)['envs']
+ return [os.path.join(env, 'share', 'jupyter', 'kernels') for env in envs]
+
+
+# Put all centrally installed environments in the kernel path.
+_CENTRAL_ENVS = '/opt/ukmo/iris/envs/*/share/jupyter/kernels/'
+
+
NATIVE_KERNEL_NAME = 'python3' if PY3 else 'python2'
+
def _pythonfirst(s):
"Sort key function that will put strings starting with 'python' first."
if s == NATIVE_KERNEL_NAME:
@@ -131,10 +150,14 @@ def _native_kernel_resource_dir(self):
def find_kernel_specs(self):
"""Returns a dict mapping kernel names to resource directories."""
d = {}
- for kernel_dir in self.kernel_dirs:
+ # Dynamically update the potential kernel directories.
+ self.kernel_dirs.extend(conda_envs())
+ self.kernel_dirs.extend(glob.glob(_CENTRAL_ENVS))
+ for kernel_dir in set(self.kernel_dirs):
d.update(_list_kernels_in(kernel_dir))
- d[NATIVE_KERNEL_NAME] = self._native_kernel_resource_dir
+ # Disable the "Python 2" or "Python 3" spec.
+ #d[NATIVE_KERNEL_NAME] = self._native_kernel_resource_dir
if self.whitelist:
# filter if there's a whitelist
d = {name:spec for name,spec in d.items() if name in self.whitelist}
#!/usr/bin/env bash
# Create a kernel for this env
ENV_NAME=$(basename ${PREFIX})
echo "Creating kernel for the \"${ENV_NAME}\" env." > $PREFIX/.messages.txt
mkdir -p $PREFIX/share/jupyter/kernels/env_${ENV_NAME}
cat > $PREFIX/share/jupyter/kernels/env_${ENV_NAME}/kernel.json <<EOF
{
"display_name": "ENV: ${ENV_NAME}",
"language": "python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"argv": [
"${PREFIX}/bin/python",
"-c",
"from IPython.kernel.zmq.kernelapp import main; main()",
"-f",
"{connection_file}"
]
}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment