如何使用不同版本的 Python 运行 Jupyter Notebook?

Dar*_*4GE 3 python linux archlinux jupyter jupyter-notebook

我希望能够在我的 Jupyter Notebook 中同时运行Python 3.8(当前版本)和Python 3.7。我知道从虚拟环境创建不同的 IPython 内核是一种方法。所以我下载了 Python 3.7 并将其本地安装在我的主目录中。使用这个 python 二进制文件来创建一个虚拟环境

> virtualenv -p ~/Python3.7/bin/python3 py37
> source py37/bin/activate
Run Code Online (Sandbox Code Playgroud)

这完美地工作,并在检查python --version和 时正确给出“Python 3.7” sys.version。然后为了创建 IPython 内核,

(py37) > ipython kernel install --user --name py37 --display-name "Python 3.7"
(py37) > jupyter notebook
Run Code Online (Sandbox Code Playgroud)

这也没有错误地运行,并且可以确认内核已添加到笔记本中。然而,它不像虚拟环境那样运行 Python 3.7,而是像默认内核那样运行 Python 3.8。(确认sys.version

我检查~/.local/share/jupyter/kernels/py37/kernel.json并看到它的内容为

{
 "argv": [
  "/usr/bin/python3",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3.7",
 "language": "python"
Run Code Online (Sandbox Code Playgroud)

所以很自然地,我尝试编辑/usr/bin/python3指向我的 Python 3.7 二进制文件路径~/Python3.7/bin/python3,但是即使内核在笔记本中也无法正常工作。

我能做什么?

注意:我使用Arch Linux,所以我安装了jupytervirtualenv,...通过pacman而不是pip作为它在 Arch 中的推荐。

Dar*_*4GE 5

我自己找到了,很难。无论如何,让我分享一下,以防这对任何人有帮助。

我想,问题在于,通过pacman安装的 jupyter notebook在 PATH 变量中搜索 python 二进制文件,而不是在虚拟环境指定的路径中。由于我在主目录中本地安装了 Python 3.7,因此 Jupyter 找不到它,它可能默认为默认的 Python 版本。

所以可能的解决方案是:

  1. 在 Python 3.7 上设置的虚拟环境中通过pip(而不是pacman)安装 Jupyter Notebook (对于Arch Linux用户完全不建议这样做,因为通过 pip 安装软件包可能会导致将来出现问题)
 > wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz  
 > tar -xvf Python-3.7.4.tgz    
 > cd Python-3.5.1/     
 > ./configure --prefix=$HOME/Python37     
 > make     
 > make install   

 > virtualenv -p ~/Python3.7/bin/python3 py37   
 > source py37/bin/activate 

 (py37) > pip install notebook   
 (py37) > python -m notebook
Run Code Online (Sandbox Code Playgroud)
  1. 在默认目录中安装 Python 3.7(而不是在其他地方指定)。使用合适的虚拟环境创建一个新的 IPython 内核,并使用通过pacman安装的jupyter-notebook(推荐给Arch Linux用户)注 1:指向更新的全局 Python 3.8 版本指向新安装的 Python 3.7注 2:创建所需的内核后,您甚至可以在虚拟环境之外使用该 Python 版本.
    > python> python3> python3.7
 > wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz  
 > tar -xvf Python-3.7.4.tgz    
 > cd Python-3.5.1/     
 > ./configure   
 > make     
 > sudo make install   

 > virtualenv -p $(which python3.7) py37   
 > source py37/bin/activate 

 (py37) > ipython kernel install --user --name py37 --display-name "Python 3.7"
 (py37) > jupyter notebook
Run Code Online (Sandbox Code Playgroud)
  1. 将本地安装新 Python 版本的目录路径添加到$PATH变量中,创建 IPython 内核并在合适的虚拟环境中运行 Jupyter Notebook。(还没有亲自尝试过这个。只是觉得这应该可行。所以不能保证。我也不认为这是一个好的解决方案)
 > wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz  
 > tar -xvf Python-3.7.4.tgz    
 > cd Python-3.5.1/     
 > ./configure --prefix=$HOME/Python37 
 > make     
 > make install   
 > export PATH="$HOME/Python37/bin:$PATH"          

 > virtualenv -p  py37   
 > source py37/bin/activate 

 (py37) > ipython kernel install --user --name py37 --display-name "Python 3.7"
 (py37) > jupyter notebook
Run Code Online (Sandbox Code Playgroud)