如何在jupyter notebook使用conda已有的虚拟环境

如何在jupyter notebook使用conda已有的虚拟环境

  • 首先conda里已经配置好的环境,使用conda env list
(torch) D:\MyData\xiazw8.CN\Desktop\Python\Python+\Python_base\tensorboard>conda env list
base                     D:\MyData\x8.CN\Anaconda3
detectron2               D:\MyData\x8.CN\Anaconda3\envs\detectron2
ssl                      D:\MyData\x8.CN\Anaconda3\envs\ssl
torch                 *  D:\MyData\x8.CN\Anaconda3\envs\torch
  • 激活想在jupyter notebook中使用的虚拟环境,比如说torch
D:\MyData\x8.CN\Desktop\Python\Python+\Python_base\tensorboard>conda activate torch
(torch) D:\MyData\x8.CN\Desktop\Python\Python+\Python_base\tensorboard>
  • 在虚拟环境torch中安装ipykernel
pip install ipykernel
conda install ipykernel
# 两条命令都行
(torch) D:\MyData\x8.CN\Desktop\Python\2-classes>pip install ipykernel
Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple
Collecting ipykernel
  Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/90/6d/6c8fe4b658f77947d4244ce81f60230c4c8d1dc1a21ae83e63b269339178/ipykernel-5.5.5-py3-none-any.whl (120 kB)
     |████████████████████████████████| 120 kB 1.3 MB/s
Collecting jupyter-client
  Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/88/4e/50fcf8b38d9c08d5b4839c1650e595f6bfa4fc9b419e2b800db8f14ee532/jupyter_client-6.2.0-py3-none-any.whl (112 kB)
     |████████████████████████████████| 112 kB 3.3 MB/s
Requirement already satisfied: tornado>=4.2 in d:\mydata\xiazw8.cn\anaconda3\envs\torch\lib\site-packages (from ipykernel) (6.1)
Collecting traitlets>=4.1.0
  Downloading https://mirrors.bfsu.edu.cn/pypi/web/packages/ca/ab/872a23e29cec3cf2594af7e857f18b687ad21039c1f9b922fac5b9b142d5/traitlets-4.3.3-py2.py3-none-any.whl (75 kB)
     |████████████████████████████████| 75 kB 379 kB/s
     ............
Installing collected packages: ipython-genutils, decorator, wcwidth, traitlets, pywin32, parso, pygments, prompt-toolkit, pickleshare, nest-asyncio, jupyter-core, jedi, colorama, backcall, jupyter-client, ipython, ipykernel
Successfully installed backcall-0.2.0 colorama-0.4.4 decorator-5.0.9 ipykernel-5.5.5 ipython-7.16.1 ipython-genutils-0.2.0 jedi-0.18.0 jupyter-client-6.2.0 jupyter-core-4.7.1 nest-asyncio-1.5.1 parso-0.8.2 pickleshare-0.7.5 prompt-tool
kit-3.0.19 pygments-2.9.0 pywin32-301 traitlets-4.3.3 wcwidth-0.2.5

  • 将虚拟环境torch写入notebook的kernel中
python -m ipykernel install --name torch
  • 在terminal下执行命令行,cd到指定文件夹下,运行 jupyter notebook
D:\MyData\xiazw8.CN\Desktop\Python\Python+\Python_base>jupyter notebook

如何在jupyter notebook使用conda已有的虚拟环境_第1张图片

  • 新建notebook就可以选择conda中已有的虚拟环境torch,激活其他的虚拟环境同理。演示如下:
    如何在jupyter notebook使用conda已有的虚拟环境_第2张图片
  • 或者在已有的ipynb下面直接切换到写入ipy内核的虚拟环境
    如何在jupyter notebook使用conda已有的虚拟环境_第3张图片

你可能感兴趣的:(环境配置,anaconda)