jupyter 不使用 pyenv 设置的版本

car*_*mom 7 python pyenv jupyter-notebook

所以这一切都在不同的机器上完美运行。不得不让它在我的桌面上工作。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
>>> 
Run Code Online (Sandbox Code Playgroud)

...但是如果我启动 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).
Run Code Online (Sandbox Code Playgroud)

并尝试导入我得到

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

ImportError: No module named 'pymysql'
Run Code Online (Sandbox Code Playgroud)

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

car*_*mom 7

与此同时,我已经解决了 90% 的问题。从这个文章的话题我发现这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
Run Code Online (Sandbox Code Playgroud)

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)]
Run Code Online (Sandbox Code Playgroud)

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

15.05.17 更新

好吧,我修复了它,或者帮助它修复了它自己——

rm -rf /home/cardamom/.pyenv/shims/jupyter*
Run Code Online (Sandbox Code Playgroud)

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

当然不是最干净的方法,希望它没有破坏其他东西,但现在至少只是运行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 $ 
Run Code Online (Sandbox Code Playgroud)

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


Bru*_*sky 5

我发现这个要点很简单:

#!/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 
Run Code Online (Sandbox Code Playgroud)

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

这个博客的第 3 步非常有用,但我没有按照这些步骤操作。我阅读了它们并理解了关于该要点的另一种观点。在盲目复制意大利面之前接受一些意见总是好的。那里有很多过时的说明!