如何在 conda 环境中使用 Jupyter 笔记本?
- 2025-03-05 09:18:00
- admin 原创
- 75
问题描述:
通常,人们在终端中运行jupyter notebook
或jupyter-notebook
或以在本地启动 Jupyter 笔记本网络服务器(并在浏览器中打开 URL)。当使用conda
和conda 环境时,运行允许导入安装在 conda 环境中的 Python 模块的 Jupyter 笔记本的最佳方法是什么?ipython notebook
看起来,这并不是 很 简单 , 许多用户都遇到 了类似的麻烦。
最常见的错误消息似乎是:在 conda 环境中安装包 XYZ 后,my-env
可以import XYZ
在启动的 python 控制台中运行my-env
,但在 Jupyter 笔记本中运行相同的代码将导致 ImportError。
这个问题已经被问过很多次了,但是没有好的地方来回答它,大多数问答和 Github 票都很混乱,所以让我们在这里开始一个新的问答。
解决方案 1:
免责声明:ATM 仅在 Ubuntu 和 Windows 中测试(请参阅此答案的评论)。
Jupyter 在名为kernel的单独进程中运行用户代码。内核可以是不同的 Python 安装(在不同的 conda 环境或虚拟环境或 Python 2 而不是 Python 3 中),甚至可以是不同语言的解释器(例如 Julia 或 R)。通过指定解释器和名称以及一些其他参数来配置内核(请参阅Jupyter 文档),并且可以将配置存储在系统范围内、活动环境(或虚拟环境)或每个用户中。如果使用,除了静态配置的内核之外, Jupyter 笔记本中还将提供已安装nb_conda_kernels
的每个 conda 环境的单独内核。ipykernel
简而言之,使用 conda 环境和 Jupyter 有三种选择:
选项 1:在 conda 环境中运行 Jupyter 服务器和内核
做类似的事情:
conda create -n my-conda-env # creates new virtual env
conda activate my-conda-env # activate environment in terminal
conda install jupyter # install jupyter + notebook
jupyter notebook # start server + kernel inside my-conda-env
Jupyter 将完全安装在 conda 环境中。不同版本的 Jupyter 可用于不同的 conda 环境,但此选项可能有点过头。只需在环境中包含内核即可,内核是包装运行代码的 Python 的组件。Jupyter 笔记本的其余部分可视为编辑器或查看器,无需为每个环境单独安装并将其包含在每个env.yml
文件中。因此,接下来的两个选项之一可能更可取,但这个选项最简单,绝对没问题。
选项 2:为 conda 环境创建特殊内核
做类似的事情:
conda create -n my-conda-env # creates new virtual env
conda activate my-conda-env # activate environment in terminal
conda install ipykernel # install Python kernel in new conda env
ipython kernel install --user --name=my-conda-env-kernel # configure Jupyter to use Python kernel
然后从系统安装或不同的 conda 环境运行 jupyter:
conda deactivate # this step can be omitted by using a different terminal window than before
conda install jupyter # optional, might be installed already in system e.g. by 'apt install jupyter' on debian-based systems
jupyter notebook # run jupyter from system
内核的名称和 conda 环境彼此独立,但使用类似的名称可能有意义。
只有 Python 内核将在 conda 环境中运行,将使用系统中的 Jupyter 或其他 conda 环境 - 它未安装在 conda 环境中。通过调用ipython kernel install
jupyter 配置为使用 conda 环境作为内核,有关更多信息,请参阅Jupyter 文档和IPython 文档。在大多数 Linux 安装中,此配置是一个*.json
文件~/.local/share/jupyter/kernels/my-conda-env-kernel/kernel.json
:
{
"argv": [
"/opt/miniconda3/envs/my-conda-env/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "my-conda-env-kernel",
"language": "python"
}
选项 3:使用 nb_conda_kernels 在 conda 环境中使用内核
安装该包nb_conda_kernels
时,每个包含 conda 包的 conda 环境都会自动获得一个单独的内核ipykernel
或不同的内核(R、Julia 等)。
conda activate my-conda-env # this is the environment for your project and code
conda install ipykernel
conda deactivate
conda activate base # could be also some other environment
conda install nb_conda_kernels
jupyter notebook
您应该能够选择内核Python [conda env:my-conda-env]
。请注意,这nb_conda_kernels
似乎只能通过 conda 使用,而不能通过 pip 或其他包管理器(如 apt)使用。
故障排除
使用 Linux/Mac,命令行上的命令which
将告诉您使用了哪个 jupyter,如果您使用选项 1(从 conda 环境内部运行 Jupyter),它应该是来自 conda 环境的可执行文件:
$ which jupyter
/opt/miniconda3/envs/my-conda-env/bin/jupyter
$ which jupyter-notebook # this might be different than 'which jupyter'! (see below)
/opt/miniconda3/envs/my-conda-env/bin/jupyter-notebook
在笔记本中你应该看到 Python 使用来自 conda 环境的 Python 路径:
[1] !which python
/opt/miniconda3/envs/my-conda-env/bin/python
[2] import sys; sys.executable
'/opt/miniconda3/envs/my-conda-env/bin/python'
['/home/my_user',
'/opt/miniconda3/envs/my-conda-env/lib/python37.zip',
'/opt/miniconda3/envs/my-conda-env/lib/python3.7',
'/opt/miniconda3/envs/my-conda-env/lib/python3.7/lib-dynload',
'',
'/opt/miniconda3/envs/my-conda-env/lib/python3.7/site-packages',
'/opt/miniconda3/envs/my-conda-env/lib/python3.7/site-packages/IPython/extensions',
'/home/my_user/.ipython']
Jupyter 提供了命令jupyter-troubleshoot
或在 Jupyter 笔记本中:
!jupyter-troubleshoot
这将打印大量有用的信息,包括上述输出以及已安装的库和其他内容。当寻求有关 Jupyter 安装问题的帮助时,最好在错误报告或问题中提供这些信息。
要列出所有配置的 Jupyter 内核,请运行:
jupyter kernelspec list
常见错误和陷阱
conda 环境中未安装 Jupyter Notebook
注意:症状并非此处描述的问题所特有。
症状:在 Jupyter 笔记本中,对于在 conda 环境中安装的模块(但不是系统范围内安装的模块)出现 ImportError,但在 Python 终端中导入时没有错误
解释:您尝试从 conda 环境中运行 jupyter notebook(选项 1,参见上文),此 conda 环境没有内核配置(这将是选项 2)并且未安装 nb_conda_kernels(选项 3),但 jupyter notebook 未(完全)安装在 conda 环境中,即使which jupyter
您可能相信它已经安装。
在 GNU/Linux 中,您可以键入which jupyter
来检查正在运行的 Jupyter 可执行文件。
这说明使用了系统的Jupyter,可能是因为没有安装Jupyter:
(my-conda-env) $ which jupyter-notebook
/usr/bin/jupyter
如果路径指向 conda 环境中的文件,则 Jupyter 将从 Jupyter 内部运行:
(my-conda-env) $ which jupyter-notebook
/opt/miniconda3/envs/my-conda-env/bin/jupyter-notebook
ipykernel
请注意,安装conda 包时,jupyter
会附带一个可执行文件,但没有可执行文件jupyter-notebook
。这意味着which jupyter
将返回 conda 环境的路径,但jupyter notebook
将启动系统jupyter-nootebook
(另请参阅此处):
$ conda create -n my-conda-env
$ conda activate my-conda-env
$ conda install ipykernel
$ which jupyter # this looks good, but is misleading!
/opt/miniconda3/envs/my-conda-env/bin/jupyter
$ which jupyter-notebook # jupyter simply runs jupyter-notebook from system...
/usr/bin/jupyter-notebook
发生这种情况是因为jupyter notebook
搜索jupyter-notebook
,找到/usr/bin/jupyter-notebook
并
调用它
启动一个新的 Python 进程。中的 shebang/usr/bin/jupyter-notebook
是#!/usr/bin/python3
而不是动态的#!/usr/bin/env python
。因此 Python 设法摆脱了 conda 环境。我猜 jupyter 可以调用python /usr/bin/jupyter-notebook
来推翻 shebang,但混合系统的 bin 文件和环境的 python 路径无论如何都无法正常工作。
解决方案:在 conda 环境中安装 jupyter notebook:
conda activate my-conda-env
conda install jupyter
jupyter notebook
错误的内核配置:内核配置为使用系统 Python
注意:症状并非此处描述的问题所特有。
症状:在 Jupyter 笔记本中,对于在 conda 环境中安装的模块(但不是系统范围内安装的模块)出现 ImportError,但在 Python 终端中导入时没有错误
解释:通常,系统会提供一个名为 python3 的内核(显示名称为“Python 3”),配置为使用/usr/bin/python3
,例如/usr/share/jupyter/kernels/python3/kernel.json
。这通常会被 conda 环境中的内核覆盖,该内核指向环境 python 二进制文件/opt/miniconda3/envs/my-conda-env/bin/python
。两者都由包生成ipykernel
(请参阅此处
和此处)。
用户内核规范~/.local/share/jupyter/kernels/python3/kernel.json
可能会覆盖系统范围和环境内核。如果缺少环境内核或用户内核指向环境之外的 Python 安装,则选项 1(在环境中安装 jupyter)将失败。
有关该问题及其变体的出现和讨论,请参见此处、
此处、
此处
以及此处、
此处和
此处。
解决方案:使用jupyter kernelspec list
列出位置活动内核位置。
$ conda activate my-conda-env
$ jupyter kernelspec list
Available kernels:
python3 /opt/miniconda3/envs/my-conda-env/share/jupyter/kernels/python3
如果环境中缺少内核,您可以尝试ipython kernel install --sys-prefix
在激活的环境中手动创建它,但最好检查您的安装,因为conda install ipykernel
应该已经创建了环境(也许尝试重新创建环境并重新安装所有软件包?)。
如果用户内核规范阻碍了环境内核规范,您可以将其删除,也可以使用相对 Python 路径$PATH
来确定python
使用哪个路径。因此,像这样的操作应该完全没问题:
$ cat ~/.local/share/jupyter/kernels/python3/kernel.json
{
"argv": [
"python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
正确的 conda 环境未激活
症状: Jupyter 笔记本和 Python 终端中 conda 环境中安装的模块(但不是系统范围内安装的)出现 ImportError
解释:每个终端都有一组环境变量,当终端关闭时,这些变量会丢失。为了使用 conda 环境,需要设置某些环境变量,这可以通过使用 激活它来完成conda activate my-conda-env
。如果您尝试从 conda 环境内部运行 Jupyter 笔记本(选项 1),但在运行它之前没有激活 conda 环境,它可能会运行系统的 jupyter。
解决方案:在运行 Jupyter 之前激活 conda 环境。
conda activate my-conda-env
jupyter notebook
内核配置损坏
症状:奇怪的事情发生。可能出现与上述类似的症状,例如 ImportError
解释:如果您尝试使用选项 2,即通过使用内核的明确配置从系统运行 Jupyter 并在 conda 环境内运行 Jupyter 内核,但它的行为并不如您预期,则配置可能在某种程度上被损坏。
解决方案:检查配置~/.local/share/jupyter/kernels/my-kernel-name/kernel.json
并手动修复错误,或者删除整个目录并使用上面为选项 2 提供的命令重新创建它。如果您在那里找不到内核配置,请运行jupyter kernelspec list
。
Python 2 与 3
症状:由于Jupyter 内核的 Python 版本错误或Python 2/3 的其他问题导致 ImportError
解释:内核配置可能会产生各种令人困惑和误导的效果。例如,默认的 Python 3 内核配置将允许我启动在 Python 2 上运行的 Jupyter 笔记本:
conda create -n my-conda-env
conda activate my-conda-env
conda install python=2
conda install jupyter
jupyter notebook
默认的 Python 3 内核:
$ cat ~/.local/share/jupyter/kernels/python3/kernel.json
{
"argv": [
"python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
使用 Python 3 内核创建新的 Jupyter Notebook 后,即使 Jupyter 显示“Python 3”,也会使用 conda 环境中的 Python 2。
解决方案: 不要使用 Python 2 ;-)
解决方案 2:
我对这一点感到很头疼,因为它没有明确的记录。许多人会建议在每个 conda 环境中安装 Jupyter,许多人会告诉你在你的(基础)conda 环境中安装 Jupyter。不幸的是,这两种建议都是可行的,但都是错误的,迟早会导致奇怪的行为。我通过反复试验学会了这一点,我相信现在我知道如何正确地做这件事以及推荐的方式(2023 年 11 月!)。这在本地机器上有效,并为你设置了一个也适合远程计算的工作流程。
以下是我学到的一些技巧:
Conda 不鼓励在您的基础环境中安装任何东西。
Conda 鼓励为每个应用程序创建一个环境;其中包括 Jupyter
Jupyter 确实显示了我创建的内核
ipykernel install …
,但我无法让它真正使用正确的环境当我最终切换到
nb_conda_kernels
(lumbric 的解决方案 3,但不在基础环境中!)时,一切都自动运行!您只需要nb_conda_kernels
在 jupyter 环境中安装,并在任何想要与 Jupyter 一起使用的 conda 环境中安装 ipykernel。
创建 Jupyter 环境
请注意并检查conda install …
命令。我添加了一些有用的扩展,但你应该安装你想要的。在创建新环境之前,请确保处于正确的 conda 路径中。如果你在不同的目录中创建新的环境,你可能会遇到命名环境会消失的问题。
cd ~/<miniconda>
conda create -n jupyter python=3.11
conda activate jupyter
conda install --channel=conda-forge jupyterlab jupyterlab-favorites jupyterlab-system-monitor nb_conda_kernels
现在您将拥有一个带有 python 3.11 的 jupyter 环境。如果您想启动 Jupyter,只需打开一个终端并输入:jupyter lab
只要此终端窗口打开,Jupyter 就会运行。
创建项目环境
cd ~/<my_project>
conda create -n my_project python=3.9
conda activate my_project
conda install <…> #(whatever you want to install for your project; use conda first and only if n/a then pip, then brew, then compile)**
# install support for this env kernel in Jupyter:
conda install ipykernel
**让我们在这里重复这个警告两次:
如果您使用 conda,请不要使用 pip、brew 等(如果可能的话)。
首先寻找
conda install --channel=conda-forge mypackage
如果不可用,请尝试
conda install mypackage
如果没有,你可以使用
pip install mypackage
如果上述方法无效,您可以求助于
brew install
或apt-get …
等等...如果没有的话,你必须自己编译库。
启动 Jupyter:
conda activate jupyter
jupyter lab
选择环境
…现在您可以在 Jupyter 中选择您的环境内核。您通常会在笔记本的右上方区域找到它。您应该在那里看到安装了 nb_conda_kernels 的任何环境,如下所示:
Python [conda env:my_project]
解决方案 3:
以下命令也可以用作一行程序来创建运行最新版本的 Python 和最新版本的 Jupyter Notebooks 的 Conda 环境,
conda create -n <env-name> python jupyter
如果你想安装特定版本的 Python 或 Jupyter,你可以这样做,
conda create -n <env-name> python=<version> jupyter=<version>
例如,
conda create -n <env-name> python=3.10.4 jupyter=1.0.0
如果您想在此环境中使用其他软件包,您可以执行以下操作,
conda create -n <env-name> python jupyter <another-package> <another-package> ...
例如,
conda create -n <env-name> python jupyter scikit-learn
请注意,与之前类似,这些命令将安装最新版本的 Python 和相关包。如果您想要特定版本,可以使用=<version>
语法。
此外,在创建环境后,您仍然可以使用pip install
或安装所需的任何软件包。conda install
创建环境后(使用上面给出的任何方法),只需运行以下命令即可激活环境并运行 Jupyter Notebook,
conda activate <env-name>
jupyter notebook
解决方案 4:
conda activate my-conda-env
# 这是你的项目和代码的环境
conda install ipykernel
whereis python
# 使用当前 conda 环境中的 python,输出如下所示
python: /usr/bin/python /usr/bin/python2.7 /usr/bin/python2.7-config /usr/bin/python3.6 /usr/bin/python3.6m /usr/lib/python2.7 /usr/lib/python3.6 /usr/lib64/python2.7 /usr/lib64/python3.6 /etc/python /usr/include/python2.7 /usr/include/python3.6m /home/scx/.conda/envs/py38/bin/python3.8-config /home/scx/.conda/envs/py38/bin/python /home/scx/.conda/envs/py38/bin/python3.8 /home/anaconda3/bin/python3.7m-config /home/anaconda3/bin/python3.7m /home/anaconda3/bin/python3.7 /home/anaconda3/bin/python3.7-config /home/anaconda3/bin/python /usr/share/man/man1/python.1.gz
[your python environment] -m ipykernel install --name my-conda-env
#通过查看最后的输出将 [你的 Python 环境] 更改为你的 Python 环境,我的是 /home/scx/.conda/envs/py38/bin/python3.8
conda deactivate
conda activate base
jupyter notebook
此后,您可以通过单击“新建”按钮选择您的 conda 环境。
解决方案 5:
以下对我有用:
激活您想要使用的环境:
conda activate <env_name>
pip install ipykernel
(如果你还没有)python -m ipykernel install --user --name=<env_name>
扫码咨询,免费领取项目管理大礼包!