簡體   English   中英

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

[英]How to run Jupyter Notebook with a different version of Python?

我希望能夠在我的 Jupyter Notebook 中同時運行Python 3.8 (當前版本)和Python 3.7 我知道從虛擬環境創建不同的 IPython 內核是一種方法。 於是我下載了 Python 3.7 並將其本地安裝在我的主目錄中。 使用此 python 二進制文件通過以下方式創建虛擬環境

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

這完美地工作,並在檢查python --versionsys.version時正確給出“Python 3.7”。 然后用於創建 IPython kernel,

(py37) > ipython kernel install --user --name py37 --display-name "Python 3.7"
(py37) > jupyter notebook

這也可以正常運行,並且可以確認 kernel 已添加到筆記本中。 但是它不像虛擬環境那樣運行 Python 3.7,而是像默認的 kernel 那樣運行 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"

所以很自然地,我嘗試編輯/usr/bin/python3以指向我的 Python 3.7 二進制文件路徑,即~/Python3.7/bin/python3 ,但是即使 kernel 在筆記本中也無法正常工作。

我能做什么?

注意:我使用Arch Linux ,所以我安裝了jupytervirtualenv ,...通過pacman而不是pip作為 Arch 推薦的。

自己找的,好難。 無論如何,讓我分享一下,以防這對任何人有幫助。

我猜,問題在於,通過pacman安裝的 jupyter notebook 在 PATH 變量中搜索 python 二進制文件,而不是在虛擬環境指定的路徑中。 由於我在我的主目錄中本地安裝了 Python 3.7,所以 Jupyter 找不到它,它可能默認為默認的 python 版本。

所以可能的解決方案是:

  1. Install Jupyter Notebook through pip (instead of pacman ) within the virtual environment set on Python 3.7 (This is not at all recommended for Arch Linux users, as installing packages through pip can probably cause issues in future)
 > 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
  1. 在默認目錄中安裝 Python 3.7(而不是指定其他位置)。 使用合適的虛擬環境創建一個新的 IPython kernel 並使用通過pacman安裝的jupyter-notebook (推薦給Arch Linux用戶)
    注 1: > python指向更新的全局 Python 3.8 版本> python3> python3.7指向新安裝的 Python 37。
    注意 2:創建所需的 kernel 后,您甚至可以在虛擬環境之外使用該 python 版本。
 > 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
  1. 將本地安裝新 Python 版本的目錄路徑添加到$PATH變量,創建 IPython kernel 並在合適的虛擬環境中運行 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

另一種方法是直接使用您需要的 Python 版本運行筆記本應用程序 - 前提是它已為該版本的 Python 安裝(例如,在 Mac 上安裝了 Python3.8 的 brew 版本):

/usr/local/opt/python@3.8/bin/python3 -m notebook

此外,如果您想為該版本安裝軟件包:

/usr/local/opt/python@3.8/bin/pip3 install that_package

在官方的jupyter 文檔中,現在有一個描述來做,當自定義 jupyter 堆棧時。 我認為 mamba 命令會解決這個問題。 以 RUN 開頭的命令也可以在 linux 系統上執行。

# Choose your desired base image
FROM jupyter/minimal-notebook:latest

# name your environment and choose the python version
ARG conda_env=python37
ARG py_ver=3.7

# you can add additional libraries you want mamba to install by listing them below the first line and ending with "&& \"
RUN mamba create --quiet --yes -p "${CONDA_DIR}/envs/${conda_env}" python=${py_ver} ipython ipykernel && \
    mamba clean --all -f -y

# alternatively, you can comment out the lines above and uncomment those below
# if you'd prefer to use a YAML file present in the docker build context

# COPY --chown=${NB_UID}:${NB_GID} environment.yml "/home/${NB_USER}/tmp/"
# RUN cd "/home/${NB_USER}/tmp/" && \
#     mamba env create -p "${CONDA_DIR}/envs/${conda_env}" -f environment.yml && \
#     mamba clean --all -f -y

# create Python kernel and link it to jupyter
RUN "${CONDA_DIR}/envs/${conda_env}/bin/python" -m ipykernel install --user --name="${conda_env}" && \
    fix-permissions "${CONDA_DIR}" && \
    fix-permissions "/home/${NB_USER}"

# any additional pip installs can be added by uncommenting the following line
# RUN "${CONDA_DIR}/envs/${conda_env}/bin/pip" install --quiet --no-cache-dir

# if you want this environment to be the default one, uncomment the following line:
# RUN echo "conda activate ${conda_env}" >> "${HOME}/.bashrc"

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM