【问题标题】:How to use Jupyter notebooks in a conda environment?如何在 conda 环境中使用 Jupyter 笔记本?
【发布时间】:2020-01-23 21:36:20
【问题描述】:

通常在终端中运行 jupyter notebookjupyter-notebookipython notebook 在本地启动 Jupyter notebook 网络服务器(并在浏览器中打开 URL)。使用conda时 和conda environments运行 Jupyter 笔记本的最佳方式是什么? 导入安装在 conda 环境中的 Python 模块?

看起来,thisisnotquitestraightforwardandmany usershavesimilartroubles.

最常见的错误信息似乎是:在 conda 环境中安装包 XYZ 后 my-env 可以在以my-env 启动的python 控制台中运行import XYZ,但运行相同 Jupyter notebook 中的代码会导致 ImportError

这个问题已经被问过很多次了,但是没有好地方回答,大部分Q&A和 Github 票很乱所以let's start a new Q&A here.

【问题讨论】:

    标签: python jupyter-notebook environment-variables jupyter conda


    【解决方案1】:

    以下对我有用:

    1. 激活您要使用的环境:conda activate

    2. pip install ipykernel(如果你还没有的话)

    3. python -m ipykernel install --user --name=

    【讨论】:

    • 我想这基本上是我在上面选项 2 中的回答中所描述的,但使用的命令略有不同。
    • 我只是觉得答案太长了,直到那时才读到。从我自己的搜索中,我得到了这 3 个命令,这对寻求快速答案的人有帮助,而不是整篇文章就可以了。
    【解决方案2】:

    *免责声明:仅在 Ubuntu 和 Windows 中测试(请参阅此答案的 cmets),如果在使用不同的操作系统时发生变化,请发表评论。


    Jupyter 在名为kernel 的单独进程中运行用户代码。内核可以是不同的 Python 安装(在不同的 conda 环境或 virtualenv 或 Python 2 中,而不是 Python 3) 甚至是不同语言的解释器(例如 Julia 或 R)。内核由 指定解释器和名称以及其他一些参数(请参阅Jupyter documentation) 并且配置可以存储在系统范围内,用于活动环境(或 virtualenv)或每个 用户。如果使用nb_conda_kernels,除了静态配置的内核,每个内核都有一个单独的内核 安装了ipykernel 的 conda 环境将在 Jupyter 笔记本中可用。

    简而言之,如何使用 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 notebook 的其余部分可以被视为编辑器或查看器,没有必要 为每个环境单独安装它,并将其包含在每个 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 documentationIPython documentation。在大多数 Linux 安装中,此配置是~/.local/share/jupyter/kernels/my-conda-env-kernel/kernel.json 中的*.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 环境中使用内核

    安装package 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
    

    在 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
    

    常见错误和陷阱

    Jupyter notebook 未安装在 conda 环境中

    注意:症状并非此处描述的问题所独有。

    症状: 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
    

    请注意,当安装 conda 包 ipykernel 时,会附带一个可执行文件 jupyter,但 没有可执行文件jupyter-notebook。这意味着which jupyter 将返回到 conda 的路径 环境,但jupyter notebook 将启动系统的jupyter-nootebook(另见here):

     $ 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-notebookcalls it 开始一个新的 Python 进程。 /usr/bin/jupyter-notebook中的shebang是#!/usr/bin/python3not a dynamic #!/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(见herehere)。

    ~/.local/share/jupyter/kernels/python3/kernel.json 中的用户内核规范可能会覆盖 系统范围和环境内核。如果缺少环境内核或用户内核 指向环境选项1之外的python安装(安装jupyter在 环境)将失败。

    有关此问题和变体的出现和讨论,请参阅herehere, here 还有here, herehere.

    解决方案:使用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 环境未激活

    症状: 安装在 conda 环境中的模块出现 ImportError(但未安装系统 宽)在 Jupyter 笔记本和 Python 终端中

    说明:每个终端都有一组环境变量,终端使用时会丢失 已经关闭。为了使用 conda 环境,需要设置某些环境变量,其中 通过使用conda activate my-conda-env 激活它来完成。如果您尝试运行 Jupyter 来自 conda 环境内部的笔记本(选项 1),但未激活 conda 环境 在运行它之前,它可能会运行系统的jupyter。

    解决方案:在运行 Jupyter 之前激活 conda 环境。

     conda activate my-conda-env
     jupyter notebook
    

    损坏的内核配置

    症状:发生了奇怪的事情。可能与上述类似的症状,例如导入错误

    说明:如果您尝试使用选项 2,即从系统和 Jupyter 运行 Jupyter 通过使用内核的显式配置,在 conda 环境中使用内核,但它确实 未按预期运行,some way 中的配置可能已损坏。

    解决方法:检查~/.local/share/jupyter/kernels/my-kernel-name/kernel.json中的配置 并手动修复错误或删除整个目录并使用命令重新创建它 上面为选项 2 提供。如果找不到内核配置,请运行 jupyter kernelspec list.

    Python 2 与 3

    症状: ImportError 由于wrong Python version of the Jupyter kernelother problems with Python 2/3

    说明:内核配置可能会产生各种令人困惑和误导的效果。 例如,默认的 Python 3 内核配置将允许我启动 Jupyter 笔记本 在 Python 2 上运行:

    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 后,来自 conda 的 Python 2 即使 Jupyter 显示“Python 3”,也会使用环境。

    解决方案: Don't use Python 2 ;-)

    【讨论】:

    • 选项 3 有一个错误:nb_conda 之所以有效,是因为它安装了 nb_conda_kernels 作为依赖项。 nb_conda_kernels 是 env 中的 Jupyter 实例自动识别安装了 ipykernel (see docs) 的任何 env 的唯一要求。 nb_conda 包用于 Jupyter 扩展,它向 Jupyter 添加了“Conda”选项卡,提供了一个类似于 Anaconda Navigator 的 GUI 来管理环境。
    • @merv 嗯,是的!非常感谢您指出这一点,我已经纠正了错误。
    • 选项1中的代码:conda install juypter应该是conda install jupyter
    • 我确认这在 Windows 上也可以正常工作。您似乎失去了 !pip install xxx 命令,但这是一个很好的权衡。
    • 选项 1 适用于 Manjaro - 谢谢;不错的清洁解决方案
    猜你喜欢
    • 2020-03-11
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 2020-03-25
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多