首页 文章

Conda环境没有出现在Jupyter Notebook中

提问于
浏览
159

我安装了Anaconda(使用Python 2.7),并在名为 tensorflow 的环境中安装了Tensorflow . 我可以在该环境中成功导入Tensorflow .

问题是Jupyter Notebook无法识别我刚刚创建的新环境 . 无论我从GUI Navigator或命令行 within tensorflow env启动Jupyter Notebook,菜单中只有一个名为 Python [Root] 的内核,并且无法导入Tensorflow . 当然,我多次点击该选项,保存文件,重新打开,但这些没有帮助 .

奇怪的是,当我打开Jupyter首页上的 Conda 标签时,我可以看到这两个环境 . 但是,当我打开 Files 标签,并尝试 new 笔记本时,我仍然只有一个内核 .

我看了这个问题:Link Conda environment with Jupyter Notebook但是我的电脑上没有 ~/Library/Jupyter/kernels 这样的目录!此Jupyter目录只有一个名为 runtime 的子目录 .

我真的很困惑 . Conda环境是否应该自动成为内核? (我按照https://ipython.readthedocs.io/en/stable/install/kernel_install.html手动设置了内核,但被告知找不到 ipykernel . )

8 回答

  • 16

    @ HarshaManjunath的评论提到当使用Anaconda3(或Miniconda3)时,你需要在conda环境中安装 nb_conda (除了 jupyter ):

    (py35) $ conda install nb_conda
    

    注意这个does not currently work with python 3.6 environments . 该软件包的信息确实说有一个python 3.6版本,它只是不起作用 .

    $ conda info nb_conda
    ...
    nb_conda 2.0.0 py36_0
    ---------------------
    file name   : nb_conda-2.0.0-py36_0.tar.bz2
    name        : nb_conda
    version     : 2.0.0
    build string: py36_0
    build number: 0
    channel     : defaults
    size        : 30 KB
    arch        : x86_64
    date        : 2016-12-20
    license     : BSD
    md5         : 24d433439f2fdd1d27e49c27688c2589
    noarch      : None
    platform    : linux
    url         : https://repo.continuum.io/pkgs/free/linux-64/nb_conda-2.0.0-py36_0.tar.bz2
    dependencies:
        _nb_ext_conf
        nb_conda_kernels
        notebook >=4.2
        python 3.6*
    

    要在Jupyter笔记本中使用python 3.6,可以在python 3.6环境中运行 jupyter . 您将无法从Jupyter中查看或切换到其他环境 .

    $ conda create -n py36_test -y python=3.6 jupyter
    $ source activate py36_test
    (py36_test) $ which jupyter
    /home/schowell/anaconda3/envs/py36_test/bin/jupyter
    (py36_test) $ jupyter notebook
    

    请注意,我在这个笔记本中运行Python 3.6.1:
    enter image description here

  • 5

    令人讨厌的是,在 tensorflow 环境中,您可以运行 jupyter notebook without installing jupyter in that environment . 赶紧跑

    (tensorflow) $ conda install jupyter
    

    现在, tensorflow 环境应该可以在任何 conda 环境中启动的Jupyter笔记本中看到,如 Python [conda env:tensorflow] .

  • 51

    我们在这个问题上遇到了很多困难,这对我们有用 . 如果您使用conda-forge channel,请务必确保使用 conda-forge 中的更新软件包,即使在 Miniconda root环境中也是如此 .

    所以安装Miniconda,然后执行:

    conda config --add channels conda-forge --force
    conda update --all  -y
    conda install nb_conda_kernels -y
    conda env create -f custom_env.yml -q --force
    jupyter notebook
    

    并且您的自定义环境将在Jupyter中显示为可用内核,只要 ipykernel 列在 custom_env.yml 文件中进行安装,如下例所示:

    name: bqplot
    channels:
    - conda-forge
    - defaults
    dependencies:
    - python>=3.6
    - bqplot
    - ipykernel
    

    只是为了证明它适用于一堆自定义环境,这里是Windows的屏幕抓取:

    enter image description here

  • 34

    这是令人沮丧的,我的问题是在新构建的conda python36环境中,jupyter拒绝加载“seaborn” - 即使seaborn安装在该环境中 . 它似乎能够从同一环境中导入大量其他文件 - 例如numpy和pandas但不是seaborn . 我在这里和其他线程上尝试了许多修复但没有成功 . 直到我意识到Jupyter没有从该环境中运行内核python,而是将系统python作为内核运行 . 尽管在环境中已经存在一个像样的内核和kernel.json . 只有在阅读了ipython文档的这一部分之后:https://ipython.readthedocs.io/en/latest/install/kernel_install.html#kernels-for-different-environments并使用以下命令:

    source activate other-env
    python -m ipykernel install --user --name other-env --display-name "Python (other-env)"
    

    我能够很好地完成一切 . (我实际上并没有使用-user变量) .

    我还没想到的一件事是如何将默认python设置为“Python(other-env)” . 目前,从主屏幕打开的现有.ipynb文件将使用系统python . 我必须使用内核菜单“更改内核”来选择环境python .

  • 252

    只需在新环境中运行 conda install ipykernel ,只有这样你才能获得带有这个环境的内核 . 即使您在每个env中安装了不同的版本,它也不会再次安装jupyter笔记本 . 您可以从任何环境中启动笔记本,您将能够看到新添加的内核 .

  • 0

    我遇到了同样的问题,我的新conda环境 myenv 无法被选为内核或新笔记本 . 从env中运行 jupter notebook 给出了相同的结果 .

    我的解决方案,以及我了解Jupyter笔记本如何识别conda-envs和内核:

    使用conda将jupyter和ipython安装到 myenv

    conda install -n myenv ipython jupyter
    

    之后,在任何列出 myenv 的env之外运行 jupter notebook 作为内核以及我之前的环境 .

    Python [conda env:old]
    Python [conda env:myenv]
    

    激活环境后运行笔记本:

    source activate myenv
    jupyter notebook
    

    隐藏我所有的其他环境内核,只显示我的语言内核:

    python 2
    python 3
    R
    
  • 6

    我不认为其他答案正在发挥作用,因为conda停止自动将环境设置为jupyter内核 . 您需要以下列方式为每个环境手动添加内核:

    source activate myenv
    python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
    

    如此处所述:http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments另见this issue .

    附录:你应该可以使用 conda install nb_conda_kernels 安装 nb_conda_kernels 软件包以自动添加所有环境,请参阅https://github.com/Anaconda-Platform/nb_conda_kernels

  • 78

    我必须运行前3个答案中提到的所有命令才能使其正常工作:

    conda install jupyter
    conda install nb_conda
    conda install ipykernel
    python -m ipykernel install --user --name mykernel
    

相关问题