jupyter没有使用pyenv设置的版本

时间:2017-05-11 11:48:01

标签: python jupyter-notebook pyenv

所以这一切在不同的机器上都很完美。必须让它在我的桌面上工作。 r(2,3)安装在那里,我刚刚添加了pyenv的不同版本,它在提示中很好地显示(下面的目录中有一个miniconda3-latest文件)然后在该提示后运行.python-version 。当我在该提示符下运行python时,它导入正常:

pip install pymysql

...但是如果我启动jupyter笔记本

(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
>>> 

并尝试导入我得到

(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).

正如我所说,它在另一台机器上完美运行..有没有人知道如何让jupyter笔记本一旦启动,就能正确选取pyenv从其ImportError Traceback (most recent call last) <ipython-input-1-a4103d9b2333> in <module>() ----> 1 import pymysql ImportError: No module named 'pymysql' 文件中读取的版本?

4 个答案:

答案 0 :(得分:4)

我发现this gist很简单:

#!/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 

一旦我知道那里发生了什么,就继续安装pyenv插件pyenv-jupyter-kernel,我非常喜欢。

this blog中的第3步非常有用,尽管我没有遵循这些步骤。我读了他们,并了解了关于该要旨的另一种观点。在您盲目复制粘贴之前,征求一些意见总是很好的。那里有很多过时的指示!

答案 1 :(得分:1)

与此同时,我有90%的人解决了它。从关于该主题的this文章中我发现了这个命令以前从未尝试过的pyenv。所以可以看出有两件事可以调用。

(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笔记本似乎打电话给第一个,但是如果我用“笔记本”这个词进入第二条路径。在if之后,它启动正常并且只有一个内核可用于我的pymysql模块:

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)]

只需要弄清楚如何在没有整个路径的情况下直接调用它。

更新15.05.17

好吧,我修好了它,或帮助它自行修复 -

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

然后关闭并重新启动终端。

当然不是最干净的方式,希望它没有破坏别的东西,但现在至少只是运行jupyter notebook正在启动一个笔记本,其中包含我的目录中的活动内核miniconda3-latest 。上面的两个which命令仍然返回相同的内容,但现在如果我在第一个目录中列出了jupyter的东西:

(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 $ 

...您可以看到所有这些文件只是在几分钟前重新创建。

答案 2 :(得分:0)

请检查一下python版本的Notebook内核正在使用,大多数情况下会出现这样的错误,因为为python3安装了库,而运行的笔记本内核版本可能是Python2。

答案 3 :(得分:0)

检查是否在.local/share/jupyter/kernels/python3/kernel.json中指定了特定版本的python。删除此文件有助于我jupyter-notebook使用通过pyenv设置的python版本。