• jupyterlab开发环境最佳构建方式


    背景

    jupyter notebook切换到了jupyter lab. 这里记录一下本地环境的最佳构建方式. jupyter lab 安装在jupyterlab-local的anaconda 虚拟环境中.建立多个其他虚拟环境安装各种python包实现环境隔离, 并将这些虚拟环境映射为jupyter的kernel实现多个虚拟环境可被jupyterlab访问.

    jupyterlab环境构建

    jupyterlab 安装在jupyterlab-local的虚拟环境中, 首先使用conda 创建虚拟环境 python选择3.10版本

    conda create -n jupyter-lab python=3.10 -y
    
    • 1

    然后激活 此环境安装jupyterlab

    # 激活环境
    conda activate jupyterlab-local
    # 安装jupyterlab
    conda install -c conda-forge jupyterlab
    
    • 1
    • 2
    • 3
    • 4

    此时查看kernel信息

    # 查看kernel状态
    jupyter kernelspec list
    # 当前虚拟环境中已经存在jupyter kernel 这是默认的kernel
    ~/anaconda3/envs/jupyterlab-local/share/jupyter/kernels/python3
    
    • 1
    • 2
    • 3
    • 4

    退出当前虚拟环境

    conda deactivate
    
    • 1

    运行虚拟环境构建以及kernel映射

    新的虚拟环境用于管理python各种依赖包, 并将其映射为jupyter kernel
    首先依然是创建虚拟环境, 此处创建test的虚拟环境

    conda create -n test python=3.10 -y
    
    • 1

    激活当前虚拟环境test

    conda activate test
    
    • 1

    安装关键包ipykernel 任何一个虚拟环境要映射为jupyter kernel 必须安装此包, 且最好用conda 安装

    conda install ipykernel -y
    
    • 1

    将当前虚拟环境映射为kernel(关键操作)

    python -m ipykernel install --user --name {kernelName} --display-name {displayName}
    
    • 1

    此处kernelName和displayName都叫test

    python -m ipykernel install --user --name test --display-name test
    
    • 1

    此时有一句很关键的日志

    Installed kernelspec test in ~/Library/Jupyter/kernels/test
    
    • 1

    此处会发现路径里面不再有conda 虚拟环境相关的文件加, 可以理解为当前kernel可以夸虚拟环境共享了
    当然也可以在当前虚拟环境中的kernel状态

    # 查看当前虚拟环境中kernel状态
    jupyter kernelspec list
    # kernel 列表如下
    Available kernels:
      python3    ~/anaconda3/envs/test/share/jupyter/kernels/python3
      test       ~/Library/Jupyter/kernels/test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    可以这样理解第一行就是本虚拟环境中kernel的路径, 第二行是它可以被其他虚拟环境访问的路径.
    退出当前环境, 完成运行的虚拟环境构建以及kernel的映射.
    如果需要删除kernel的映射使用指令:

    jupyter kernelspec remove {kernelName}
    
    • 1

    验证

    首先test 环境目前是很干净的, 现在随便往里面安装一个python包, 比如pandas

    # 安装依赖包
    pip install pandas
    # 查看版本
    pip list | grep panda
    # 结果
    pandas            2.1.1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    切换到jupyterlab-local的虚拟环境启动jupyterlab

    jupyter lab
    
    • 1

    可以看到launcher页上面已经有两个kernel一个是python3b本虚拟环境自带的,另一个是test,自己创建的.
    kernels
    选择test kernel 打印pandas 版本

    # code
    import pandas as pd
    print(pd.__version__)
    # 结果
    2.1.1
    
    • 1
    • 2
    • 3
    • 4
    • 5

    实验成功, jupyterlab-local 虚拟环境可以访问test虚拟环境的python包. 右上角切换到python3(jupyterlab-local虚拟环境当前kernel,没有安装pandas)运行结果显示包不存在.

    # code
    import pandas as pd
    print(pd.__version__)
    # 结果
    ---------------------------------------------------------------------------
    ModuleNotFoundError                       Traceback (most recent call last)
    Cell In[1], line 1
    ----> 1 import pandas as pd
          2 print(pd.__version__)
    
    ModuleNotFoundError: No module named 'pandas'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    总结

    1. 创建虚拟环境jupyterlab-local安装jupyterlab包.
    2. 创建其他虚拟环境安装项目中python框架,包等等,进行环境隔离.
    3. 需要映射成kernel的虚拟环境安装ipykernel, 使用指令
    python -m ipykernel install --user --name {kernelName} --display-name {displayName}
    
    • 1

    将虚拟环境全局映射成jupyter kernel, 实现jupyterlab-local环境可进行访问.

  • 相关阅读:
    获取深度学习模型权重或者某一层特征图输出的方法:基于pytorch
    MVCC面试题
    2. 处理脚本命令行参数
    【数据结构】——链表经典OJ(leetcode)
    [Model.py 02] 地图按比例放大的实现
    Android自定义AppGlideModule,DataFetcher ,ModelLoaderFactory,ModelLoader,Kotlin(1)
    11 | JpaRepository 如何自定义
    【论文详读】Overcoming catastrophic forgetting in neural networks
    如何确认串口波特率
    软件设计师学习笔记9-进程调度
  • 原文地址:https://blog.csdn.net/Allocator/article/details/133183880