Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jupyter not using version set by pyenv

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?

like image 469
cardamom Avatar asked May 11 '17 11:05

cardamom


People also ask

Can I change Python version in Jupyter Notebook?

To change the kernel version in Jupyter Python Notebooks follow the steps below : Open the Python Notebook and click on " Kernel " from the menu bar located on top of the python notebook. Click on " Change kernel " from the drop down box that appears and chose the version that is required.

How do I enable autocompletion in Jupyter?

To enable code autocomplete in Jupyter Notebook or JupyterLab, you just need to hit the Tab key while writing code. Jupyter will suggest a few completion options. Navigate to the one you want with the arrow keys, and hit Enter to choose the suggestion.


2 Answers

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!

like image 120
Bruno Bronosky Avatar answered Nov 10 '22 01:11

Bruno Bronosky


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.

like image 30
cardamom Avatar answered Nov 10 '22 00:11

cardamom