【问题标题】:Jupyter and conda: error when cloning environmentJupyter 和 conda:克隆环境时出错
【发布时间】:2020-05-06 05:14:33
【问题描述】:

我创建了多个 conda 环境来测试已安装软件包的兼容性。我使用conda create -n test02 --clone test01 创建环境 test02 作为 test01 的克隆。我激活 test02,安装新包并启动 python 实例,从中导入新包没有问题。当我启动 jupyter notebook 或 qtconsole 并尝试导入新安装的包并出现导入错误时出现问题:没有名为“xxx”的模块。我做了一个sys.executable 并看到jupyter 正在从旧环境(test01)运行python,我从中克隆了新环境。为什么会这样?我可以在某个地方的配置文件中更改它吗?我在哪里可以找到这个文件?

编辑:更多信息

jupyter --paths 两个环境共享同一环境目录中配置和数据的路径条目

(test01) PS C:\Users\Aka> jupyter --paths
config:
    C:\Users\Aka\.jupyter
    c:\users\aka\miniconda3\envs\test01\etc\jupyter
    C:\ProgramData\jupyter
data:
    C:\Users\Aka\AppData\Roaming\jupyter
    c:\users\aka\miniconda3\envs\test01\share\jupyter
    C:\ProgramData\jupyter
runtime:
    C:\Users\Aka\AppData\Roaming\jupyter\runtime
(test01) PS C:\Users\Aka> conda activate test02
(test02) PS C:\Users\Aka> jupyter --paths
config:
    C:\Users\Aka\.jupyter
    c:\users\aka\miniconda3\envs\test01\etc\jupyter
    C:\ProgramData\jupyter
data:
    C:\Users\Aka\AppData\Roaming\jupyter
    c:\users\aka\miniconda3\envs\test01\share\jupyter
    C:\ProgramData\jupyter
runtime:
    C:\Users\Aka\AppData\Roaming\jupyter\runtime

edit2:我忘了提到我使用 pip 安装了 Jupyter。

重现我遇到的问题:

conda create -n env1
conda activate env1
pip install jupyter
jupyter --paths

配置:
C:\Users\Aka.jupyter
c:\users\aka\miniconda3\envs\env1\etc\jupyter
C:\ProgramData\jupyter
数据:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\env1\share\jupyter
C:\ProgramData\jupyter
运行时:
C:\Users\Aka\AppData\Roaming\jupyter\runtime

conda create --clone env1 -n env2
conda activate env2
jupyter --paths

配置:
C:\Users\Aka.jupyter
c:\users\aka\miniconda3\envs\env1\etc\jupyter
C:\ProgramData\jupyter
数据:
C:\Users\Aka\AppData\Roaming\jupyter
c:\users\aka\miniconda3\envs\env1\share\jupyter
C:\ProgramData\jupyter
运行时:
C:\Users\Aka\AppData\Roaming\jupyter\runtime

如果我将带有 conda 的 jupyter 安装到一个全新的环境中,然后克隆该环境,jupyter 的行为与预期相同。注意:我使用 pip 安装了 jupyter,因为我使用的是 python 3.5,因为我需要其他软件包,并且在我的环境中使用 conda 安装 jupyter 不断破坏它。我向 conda-forge 寻求帮助,但他们说“抱歉,我们不支持 python 3.5”。

【问题讨论】:

  • 你安装了ipykernel吗?还有,启动jupyter的时候,新建notebook的时候可以选择新环境(test02)吗(点击new下拉,有的话选择test02)?
  • 是的,已安装 ipykernel。从笔记本应用程序的下拉菜单中创建新笔记本时,没有选择环境的选项或指示。
  • 我认为问题在于 conda 环境需要在为它们创建内核之前被激活。这是 Jupyter 不做的事情。
  • @carlosCordoba,这不是问题所在。问题在于,当新环境处于活动状态并从该环境启动 jupyter 应用程序时,路径指向另一个环境。

标签: python pip jupyter-notebook conda conda-forge


【解决方案1】:

Jupyter 只需安装在一个位置 - 无论是 Conda 环境还是系统级别。

安装在 Conda 环境中的 Jupyter

要将其他环境用作内核,需要在带有 Jupyter 的环境中安装 nb_conda_kernels,并在您希望用作内核的任何环境中安装 ipykernel。始终从带有 Jupyter 的环境中启动 jupyter notebook,其他的将自动可用。

Conda 外的 Jupyter

如果 Jupyter 安装在系统级别,则必须手动注册希望用作内核的 Conda 环境:

conda activate my_env
conda install ipykernel
python -m ipykernel install --user --name my_env_name

然后从任何地方启动 Jupyter。

【讨论】:

  • 非常感谢。这让我走上了我需要完成的道路。我不知道这种可能性,所以可能问错了问题。我正在玩 nb_conda_kernels 并让它运行我的 py35 内核。我现在必须弄清楚如何让它在笔记本中运行 %qtconsole。对此有任何想法吗?如果我从 py35 内核笔记本尝试它,它会静默失败。然后,我将 qtconsole 安装到 py35 env 中并从笔记本尝试并再次失败,并出现“...ImportError: No module named 'console'”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 2022-12-21
  • 2021-05-27
  • 2021-01-24
  • 2021-05-16
  • 1970-01-01
相关资源
最近更新 更多