jupyter not using version set by pyenv
Asked Answered
A

4

12

So this all worked perfect on a different machine. Have had to get it to work on my desktop. pyenv is installed there with different versions I just added miniconda3-latest which displays nicely in the prompt (there is a .python-version file in the directory below) Then ran pip install pymysql after that prompt. When I run python now at that prompt it imports fine:

(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ python
Python 3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql
>>> 

...but if I launch jupyter notebook

(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ jupyter notebook
[I 13:38:28.714 NotebookApp] [nb_conda_kernels] enabled, 2 kernels found
[I 13:38:29.216 NotebookApp] The port 8888 is already in use, trying another port.
[I 13:38:29.224 NotebookApp] [nb_conda] enabled
[I 13:38:29.278 NotebookApp] [nb_anacondacloud] enabled
[I 13:38:29.337 NotebookApp] ✓ nbpresent HTML export ENABLED
[W 13:38:29.337 NotebookApp] ✗ nbpresent PDF export DISABLED: No module named 'nbbrowserpdf'
[I 13:38:29.340 NotebookApp] Serving notebooks from local directory: /home/cardamom/Desktop/Project
[I 13:38:29.340 NotebookApp] 0 active kernels 
[I 13:38:29.341 NotebookApp] The Jupyter Notebook is running at: http://localhost:8889/
[I 13:38:29.341 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

and try to import I get

ImportError                               Traceback (most recent call last)
<ipython-input-1-a4103d9b2333> in <module>()
----> 1 import pymysql

ImportError: No module named 'pymysql'

As I said, it works perfectly on a different machine.. Does anyone know how to get jupyter notebook, once launched, to correctly pick up the version which pyenv is reading from its .python-version file?

Assimilation answered 11/5, 2017 at 11:48 Comment(2)
You have 2 kernels in jupyter. Did you select the correct one for this notebook?Scalenus
@MaartenFabré thanks for the suggestion, I tried them both and neither of them worked, neither of them seemed to contain the pymysql module in miniconda-3-latestAssimilation
A
8

In the mean time I have 90% solved it. From this article on the topic I found this pyenv which command had never tried before. So can see that there are two things that can be called.

(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ which jupyter
/home/cardamom/.pyenv/shims/jupyter

(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ pyenv which jupyter
/home/cardamom/.pyenv/versions/miniconda3-latest/bin/jupyter

Jupyter notebook seems to call the first one but if I enter the second path with the word 'notebook' after if, it launches fine and there is only one kernel available being the one with my pymysql module in it:

import sys
print (sys.version)

3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

Just need to work out how to call that directly without the whole path..

Update 15.05.17

Well, I fixed it, or helped it to fix itself -

rm -rf /home/cardamom/.pyenv/shims/jupyter*

Then closed and relaunched the terminal.

Surely wasn't the cleanest way, hope it hasn't broken something else, but now at least just running jupyter notebook is launching a notebook which contains the kernel active in my directory miniconda3-latest. The two which commands as per above are still returning the same thing, but now if I list the jupyter things in the first directory:

(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ ll /home/cardamom/.pyenv/shims/jupyter*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-bundlerextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-console*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-kernelspec*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-migrate*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-nbconvert*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-nbextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-notebook*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-qtconsole*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-run*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-serverextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-troubleshoot*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-trust*
(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ 

...you can see that all those files just recreated themselves just a few minutes ago.

Assimilation answered 11/5, 2017 at 14:44 Comment(1)
you can also register a kernel in your jupyter environment with this answerScalenus
H
11

I found this gist which is pretty simple:

#!/bin/sh

if [ "$PYENV_VERSION" -ne "" ]
then
    name=`pyenv version-name`
    python=`pyenv which python`
else
    name=`basename "$VIRTUAL_ENV"`
    python="$VIRTUALENV/bin/python"
fi
jupyterdir=$(jupyter --data-dir)
kerneldir="${jupyterdir}/kernels/${name}"

echo "Installing jupyter kernel file $name for $python to $kerneldir ..."

pip install ipykernel
mkdir -p "$kerneldir"
cat > "$kerneldir"/kernel.json <<EOF
{
    "argv": [ "$python", "-m", "ipykernel", "-f", "{connection_file}" ],
    "display_name": "$name", 
    "language": "python"
}
EOF

cat "$kerneldir"/kernel.json 

Once I understood what was happening there, I went ahead and installed the pyenv plugin pyenv-jupyter-kernel and I like it very much.

Step 3 from this blog was pretty informative, though I didn't follow the steps. I read them and understood another perspective on that gist. It's always good to take in a few opinions before you blindly copy-pasta. There are a lot of outdated instructions out there!

Heida answered 28/3, 2020 at 4:31 Comment(1)
Thanks - this is nice. But it seems out-of-date. With pyenv 2.3.4-19-g1f526edb I didn't have $PYENV_VERSION set, and had to use this test: if [ "$PYENV_ROOT" != "" ]Greggs
A
8

In the mean time I have 90% solved it. From this article on the topic I found this pyenv which command had never tried before. So can see that there are two things that can be called.

(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ which jupyter
/home/cardamom/.pyenv/shims/jupyter

(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ pyenv which jupyter
/home/cardamom/.pyenv/versions/miniconda3-latest/bin/jupyter

Jupyter notebook seems to call the first one but if I enter the second path with the word 'notebook' after if, it launches fine and there is only one kernel available being the one with my pymysql module in it:

import sys
print (sys.version)

3.6.0 |Continuum Analytics, Inc.| (default, Dec 23 2016, 12:22:00) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

Just need to work out how to call that directly without the whole path..

Update 15.05.17

Well, I fixed it, or helped it to fix itself -

rm -rf /home/cardamom/.pyenv/shims/jupyter*

Then closed and relaunched the terminal.

Surely wasn't the cleanest way, hope it hasn't broken something else, but now at least just running jupyter notebook is launching a notebook which contains the kernel active in my directory miniconda3-latest. The two which commands as per above are still returning the same thing, but now if I list the jupyter things in the first directory:

(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ ll /home/cardamom/.pyenv/shims/jupyter*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-bundlerextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-console*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-kernelspec*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-migrate*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-nbconvert*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-nbextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-notebook*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-qtconsole*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-run*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-serverextension*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-troubleshoot*
-rwxr-xr-x 1 cardamom cardamom 402 May 15 15:55 /home/cardamom/.pyenv/shims/jupyter-trust*
(miniconda3-latest) cardamom@pegasus ~/Desktop/Project $ 

...you can see that all those files just recreated themselves just a few minutes ago.

Assimilation answered 11/5, 2017 at 14:44 Comment(1)
you can also register a kernel in your jupyter environment with this answerScalenus
S
0

Kindly check what python version Notebook kernel is using, Most of the time like this error will come because of library installed for python3 and the running notebook kernel version may be Python2.

Suggestive answered 11/5, 2017 at 11:56 Comment(0)
L
0

Check whether a specific version of python is specified in .local/share/jupyter/kernels/python3/kernel.json. Deleting this file helped me to have jupyter-notebook use the python version being set with pyenv.

Lucielucien answered 14/2, 2020 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.