• 【Python】入门(三):在jupyterlab中使用conda虚拟环境


    AI学习目录汇总

    1、添加ipykernel

    1.1 创建虚拟环境时添加ipykernel

    例如在创建pytorch虚拟环境时直接加上参数ipykernel

    conda create -n pytorch python=3.9 ipykernel
    
    • 1

    1.2 创建虚拟环境后,再添加ipykernel

    如果在创建虚拟环境时,没有使用参数ipykernel,可以通过install命名追加,命令格式

    conda install -n 虚拟环境名字 ipykernel
    
    • 1

    例如创建pytorch环境时,没有使用ipykernel:conda create -n pytorch python=3.9,再想添加ipykernel,可以使用下面的命令:

    conda install -n pytorch  ipykernel
    
    • 1

    2、激活虚拟环境

    命令格式

    conda activate 虚拟环境名字
    
    • 1

    例如:

    conda activate pytorch
    
    • 1

    3、将虚拟环境加入 jupyterlab 的 kernel 中

    命令格式

    	python -m ipykernel install --user --name 虚拟环境名字 --display-name "虚拟环境名字"
    
    • 1

    例如:

    (pytorch) PS C:\Users\laoer> python -m ipykernel install --user --name pytorch --display-name "pytorch"
    Installed kernelspec pytorch in C:\Users\laoer\AppData\Roaming\jupyter\kernels\pytorch
    
    • 1
    • 2

    4、运行jupyterlab

    一定要在pytorch虚拟环境中运行 jupyter lab

    (pytorch) PS C:\Users\laoer> jupyter lab
    
    • 1

    打印信息可见运行成功,并有访问地址

    [I 2022-09-06 12:33:07.765 ServerApp] Jupyter Server 1.13.5 is running at:
    [I 2022-09-06 12:33:07.765 ServerApp] http://localhost:8890/lab?token=b3b692f9bf962d0aecf769c4ed62dc0e6aaa9f2731226b21
    [I 2022-09-06 12:33:07.766 ServerApp]  or http://127.0.0.1:8890/lab?token=b3b692f9bf962d0aecf769c4ed62dc0e6aaa9f2731226b21
    
    • 1
    • 2
    • 3

    一般会自动弹出浏览器界面,不需要输入url

    5、配置jupyterlab

    默认打开的jupyterlab,使用的kernel是base不是虚拟环境,需要配置kernel,配置步骤如下:
    点击:“Kernel”–>“Change Kernel”
    在这里插入图片描述
    选择虚拟环境的kernel:pytorch
    在这里插入图片描述

    6、验证

    配置成功,可以在左下角看到“pytorch | Idle”。测试pytorch环境
    加载库、打印版本信息

    import torch
    print(torch.__version__)
    
    • 1
    • 2

    在这里插入图片描述

    如果失败,在加载torch库时会报错
    在这里插入图片描述

  • 相关阅读:
    中文转拼音的方法
    Git入门
    Matlab地理信息绘图—研究区域绘制
    (四)Ansible-其他模块
    (数组) 724. 寻找数组的中心下标 ——【Leetcode每日一题】
    Spring IoC、容器初始化、对象
    Spring-----AOP面向切面
    ~按位取反
    nsoftware Cloud SMS 2022 .NET 22.0.8 Crack
    铼回收树脂RCX-5143性能测试
  • 原文地址:https://blog.csdn.net/u010168781/article/details/126721487