Skip to main content
 首页 » 编程设计

python之jupyter 不使用 pyenv 设置的版本

2024年10月01日7wuhuacong

所以这一切都在不同的机器上完美运行。必须让它在我的桌面上工作。 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 读取的版本> 文件?

请您参考如下方法:

我找到了 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我非常喜欢它。

第 3 步来自 this blog信息量很大,尽管我没有按照步骤操作。我阅读了它们并理解了关于该要点的另一种观点。在盲目复制意大利面之前,听取一些意见总是好的。那里有很多过时的说明!