![](/img/trans.png)
[英]Is there any way to use jupyter notebook to run different python version in the same 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 --version
和sys.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 ,所以我安装了jupyter , virtualenv ,...通过pacman而不是pip作为 Arch 推荐的。
自己找的,好难。 无论如何,让我分享一下,以防这对任何人有帮助。
我猜,问题在于,通过pacman安装的 jupyter notebook 在 PATH 变量中搜索 python 二进制文件,而不是在虚拟环境指定的路径中。 由于我在我的主目录中本地安装了 Python 3.7,所以 Jupyter 找不到它,它可能默认为默认的 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 --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
> python
指向更新的全局 Python 3.8 版本> python3
或> python3.7
指向新安装的 Python 37。> 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
> 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.