Skip to content

Instantly share code, notes, and snippets.

@ruxi
Last active October 18, 2016 21:33
Show Gist options
  • Save ruxi/53c4b8f58dab0a020415d617582d389e to your computer and use it in GitHub Desktop.
Save ruxi/53c4b8f58dab0a020415d617582d389e to your computer and use it in GitHub Desktop.
NBEnvKernels/Env-specific Notebook Kernel.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# Env-specific Notebook Kernels"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "**author**: github.com/ruxi\n<br>**created**: 2016-03-06 19:36:28 "
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Problem"
},
{
"metadata": {},
"cell_type": "markdown",
"source": " - anaconda environments prevent namespace pollution\n - packages installed in ENVs are accessed by kernels\n - jupyter notebook do not install ENV-specific python kernels, but reuse common paths\n - A simple [solution](https://github.com/jupyter/notebook/issues/541) is to create a new kernelspec that points to $PYTHON_PATH\n "
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Intention\n\n**Before:**\n```python\nimport sys; sys.executable\n```\n> '/usr/bin/python'\n\n**After:**\n\n```python\nimport sys; sys.executable\n```\n> ~/anaconda3/<**your env name**>/bin/python"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# Method\n\n- create new kernel\n- drop in '/usr/local/share/jupyter/kernels/'\n- F5!"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Create new kernel"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "**Anatomy of existing kernelspec**\n\n```python\n!cat ~/.local/share/jupyter/kernels/python3/kernel.json\n```\n\n```bash\n{\n \"display_name\": \"Python 3 (anaconda3/bin/python)\",\n \"argv\": [\n \"~/anaconda3/bin/python\",\n \"-m\",\n \"ipykernel\",\n \"-f\",\n \"{connection_file}\"\n ],\n \"language\": \"python\"\n}\n```\nReplace <font color='blue'>\"~/anaconda3/bin/python\"</font> with <font color='blue'>python</font>\n"
},
{
"metadata": {
"collapsed": true,
"trusted": false
},
"cell_type": "code",
"source": "import os.path\nos.makedirs('env_python_kernel')",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "%%writefile env_python_kernel/kernel.json\n{\n \"display_name\": \"PythonPath (python)\",\n \"argv\": [\n \"python\",\n \"-m\",\n \"ipykernel\",\n \"-f\",\n \"{connection_file}\"\n ],\n \"language\": \"python\"\n}\n",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"collapsed": false
},
"cell_type": "markdown",
"source": "## List currently kernel list"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"ExecuteTime": {
"start_time": "2016-10-07T15:35:46.037502",
"end_time": "2016-10-07T15:35:47.219658"
}
},
"cell_type": "code",
"source": " !jupyter-kernelspec list",
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": "Available kernels:\r\n ruby /home/lewis/.ipython/kernels/ruby\r\n ir /home/lewis/anaconda3/envs/phd/share/jupyter/kernels/ir\r\n python2 /usr/local/share/jupyter/kernels/python2\r\n python3 /usr/local/share/jupyter/kernels/python3\r\n pythonpathkernel /usr/local/share/jupyter/kernels/pythonpathkernel\r\n",
"name": "stdout"
}
]
},
{
"metadata": {
"collapsed": true
},
"cell_type": "markdown",
"source": "## Copy/Paste/Replace kernel.json + F5"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Check if it worked"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "```\nsource activate <envname>\njupyter notebook\n```"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"ExecuteTime": {
"start_time": "2016-10-07T15:30:47.772707",
"end_time": "2016-10-07T15:30:47.801360"
}
},
"cell_type": "code",
"source": "import sys; sys.executable #did it work?",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "'/home/lewis/anaconda3/envs/phd/bin/python'"
},
"metadata": {},
"execution_count": 2
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "---\n\n\n# To install new kernels to global\n\n**Python 2.7 example**\n\n```bash\nconda create env -n py27 python==2.7\nsource activate env # for windows: activate py27\njupyter-kernelspec install-self # adds current kernel location to jupyter configs (available to global)\n```\n\n**Note:** \n\n```jupyter kernelspec install-self``` was **DEPRECATED as of jupyter 4.0**. But... still worked\n\nHaven't tried, but error prompt suggests proper method is to use \n```bash\nipython kernel install```"
}
],
"metadata": {
"kernelspec": {
"name": "pythonpathkernel",
"display_name": "pythonpath (python)",
"language": "python"
},
"toc": {
"threshold": 6,
"number_sections": true,
"toc_cell": false,
"toc_window_display": true,
"toc_section_display": "block",
"sideBar": true,
"navigate_menu": true
},
"_draft": {
"nbviewer_url": "https://gist.github.com/53c4b8f58dab0a020415d617582d389e"
},
"language_info": {
"nbconvert_exporter": "python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"file_extension": ".py",
"version": "3.5.2",
"mimetype": "text/x-python",
"pygments_lexer": "ipython3",
"name": "python"
},
"gist": {
"id": "53c4b8f58dab0a020415d617582d389e",
"data": {
"description": "NBEnvKernels/Env-specific Notebook Kernel.ipynb",
"public": false
}
},
"latex_envs": {
"eqNumInitial": 0,
"eqLabelWithNumbers": true,
"current_citInitial": 1,
"cite_by": "apalike",
"bibliofile": "biblio.bib"
},
"nav_menu": {}
},
"nbformat": 4,
"nbformat_minor": 0
}
@gitrytub
Copy link

jupyter --paths

@ruxi
Copy link
Author

ruxi commented Oct 18, 2016

conda install -c conda-forge nb_conda_kernels

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment