Skip to content

Commit c272470

Browse files
committed
Change register-kernel to manual registration
* Why: Automatic registration has an incorrect assumption that jupyter must be installed globally * Remove batch script for adding jupyter kernels * Remove after_install hook because it is redundant now
1 parent 4066bff commit c272470

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pyenv-jupyter-kernel
22

3-
Pyenv plugin to create a jupyter kernel for every installed pyenv version. Inspiration from [this gist](https://gist.github.com/thvitt/9072336288921f57ec8741eb4b8b024e)
3+
Pyenv plugin to create a jupyter kernel for installed pyenv version. Inspiration from [this gist](https://gist.github.com/thvitt/9072336288921f57ec8741eb4b8b024e)
44

55
## Installation
66

@@ -10,7 +10,7 @@ $ git clone https://github.com/aiguofer/pyenv-jupyter-kernel $(pyenv root)/plugi
1010

1111
## Usage
1212

13-
New kernels are automatically installed for every new version and virtualenv that you install. However, if you want to install the kernel for the current version (if using multiple versions, the top one) you can run:
13+
If you want to install the kernel for the current version (if using multiple versions, the top one) you run:
1414

1515
```shell
1616
$ pyenv register-kernel

bin/pyenv-register-kernel

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
# it will use the current active version. If there is more than one
99
# active version, it will use the first one.
1010

11+
# Function to check if a Python package is installed
12+
check_python_package_installed() {
13+
local package=$1
14+
PYENV_VERSION=$name pyenv exec pip list --disable-pip-version-check | grep -F "$package" > /dev/null
15+
if [ $? -eq 0 ]; then
16+
return 0
17+
else
18+
return 1
19+
fi
20+
}
21+
1122
set -e
1223
[ -n "$PYENV_DEBUG" ] && set -x
1324

@@ -19,25 +30,20 @@ fi
1930

2031
python=$(PYENV_VERSION=$name pyenv which python)
2132

22-
jupyter_dir=$(jupyter --data-dir)
23-
kernel_dir=${jupyter_dir}/kernels/pyenv_${name}
24-
25-
echo "Installing jupyter kernel file $name for $python to $kernel_dir ..."
26-
27-
echo "Upgrading (or installing) the ipykernel package for $name ..."
28-
PYENV_VERSION=$name pyenv exec pip install -U ipykernel
29-
PYENV_VERSION=$name pyenv exec pip install -U --no-deps ipywidgets
33+
# Check if Jupyter is installed
34+
if check_python_package_installed "jupyter"; then
35+
jupyter_dir=$(PYENV_VERSION=$name pyenv exec jupyter --data-dir)
36+
kernel_dir=${jupyter_dir}/kernels/pyenv_${name}
3037

31-
mkdir -p $kernel_dir
38+
echo "Installing jupyter kernel file $name for $python to $kernel_dir ..."
3239

33-
kernel_file=${kernel_dir}/kernel.json
40+
echo "Upgrading (or installing) the ipykernel package for $name ..."
41+
PYENV_VERSION=$name pyenv exec pip install -U --disable-pip-version-check ipykernel
42+
PYENV_VERSION=$name pyenv exec pip install -U --no-deps --disable-pip-version-check ipywidgets
3443

35-
cat > $kernel_file <<EOF
36-
{
37-
"argv": [ "$python", "-m", "ipykernel", "-f", "{connection_file}" ],
38-
"display_name": "$name",
39-
"language": "python"
40-
}
41-
EOF
42-
43-
echo "Kernel file created."
44+
echo "Creating kernel..."
45+
PYENV_VERSION=$name pyenv exec python -m ipykernel install --user --name pyenv_$name --display-name "pyenv_$name"
46+
echo "Kernel file created."
47+
else
48+
echo "Jupyter is not installed in the pyenv environment '$name'."
49+
fi

etc/pyenv.d/install/register-jupyter-kernel.bash

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
#!/usr/bin/env bash
2+
env_name=$1
23

3-
if declare -Ff after_uninstall >/dev/null; then
4-
after_uninstall 'rm -rf $(jupyter --data-dir)/kernels/${VERSION_NAME}'
4+
check_python_package_installed() {
5+
local package=$1
6+
PYENV_VERSION=$env_name pyenv exec pip list --disable-pip-version-check | grep -F "$package" > /dev/null
7+
if [ $? -eq 0 ]; then
8+
return 0
9+
else
10+
return 1
11+
fi
12+
}
13+
14+
if declare -Ff before_uninstall >/dev/null; then
15+
if check_python_package_installed "jupyter"; then
16+
jupyter_data_dir=$(PYENV_VERSION=$env_name pyenv exec jupyter --data-dir)
17+
before_uninstall 'rm -rf ${jupyter_data_dir}/kernels/pyenv_${env_name}'
18+
else
19+
echo "Jupyter is not installed in the pyenv environment '${env_name}'."
20+
fi
521
else
622
echo "pyenv: pyenv-jupyter-kernel plugin requires pyenv v0.1.0 or later" >&2
723
fi

etc/pyenv.d/virtualenv/register-jupyter-kernel.bash

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)