jupyter 不使用 pyenv 设置的版本

jupyter not using version set by pyenv

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

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

...但是如果我启动 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).

并尝试导入我得到

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

ImportError: No module named 'pymysql'

正如我所说,它在不同的机器上完美运行。有谁知道如何让 jupyter notebook 在启动后正确地获取 pyenv 从其 .python-version 文件中读取的版本?

请检查 python 笔记本内核使用的是什么版本,大多数时候会出现这样的错误,因为为 python3 安装的库和 运行 笔记本内核版本可能是 Python2.

同时我已经解决了 90%。从关于该主题的 this 文章中,我发现了这个 pyenv which command 以前从未尝试过。所以可以看出有两个东西可以调用。

(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 似乎调用了第一个,但是如果我在 if 之后输入带有单词 'notebook' 的第二个路径,它会正常启动并且只有一个内核可用,即其中包含我的 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 中激活的内核-最新的。上面的两个 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 $ 

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

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

我发现 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 步非常有用,但我没有按照这些步骤操作。我阅读了它们并理解了关于该要点的另一种观点。在盲目复制意大利面之前,听取一些意见总是好的。那里有很多过时的说明!