Hexo

凡事预则立,不预则废


  • Home

  • Tags

  • Archives

  • Navigation

  • Search

Python-Jupyter——Jupyter使用笔记


Jupyter 安装和启动

  • 推荐使用 pip 安装

    1
    2
    3
    4
    python3 -m pip install jupyter

    # (可选) 如果想使用更现代化的 JupyterLab 界面,可以同时安装 `jupyterlab`
    python3 -m pip install jupyterlab
  • 启动 Jupyter

    1
    2
    3
    4
    5
    # 启动 Jupyter Notebook
    jupyter notebook

    # 启动 JupyterLab
    jupyter lab

pip 命令与执行环境不同

  • 原因是 pip 执行的是系统的命令行,而系统的命令行默认 shell 环境与 python 环境不同,可以通过下面的代码确认原因:

    1
    2
    3
    4
    5
    6
    # 查看内核的 Python 路径(应正确)
    import sys
    print(sys.executable) # 输出:.conda/envs/xxx_env/bin/python

    # 查看 shell 中的 pip 路径(可能错误)
    !which pip # Linux/Mac
    • 若两者输出不同,则说明是 shell 环境问题
  • 解法一:指定 python 来执行对应的 pip

    1
    2
    import sys
    !{sys.executable} -m pip list
  • 解法二:修改 ~/.bashrc 文件,并重启 Jupyter

    • 待尝试

Python-Jupyter——安装pip包

安装pip包


在Jupyter环境中安装pip包

  • 为了方便使用,我们一般会选择将Jupyter的内核切换为某个特定的虚拟Python环境,切换后,Jupyter的Python命令会按照新环境运行,但是安装软件包不会默认安装到当前环境(而是base环境)

  • 直接使用下面的命令安装会安装在base环境中

    1
    !pip install xxx
  • 如果想要安装在当前运行环境中,可以使用下面的命令

    1
    2
    import sys
    !{sys.executable} -m pip install xxx
1…333334335…352
San Ye

San Ye

Stay Hungry. Stay Foolish.

704 posts
53 tags
© 2026 San Ye
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4