【问题标题】:How to find the version of jupyter notebook from within the notebook如何从笔记本中查找 jupyter notebook 的版本
【发布时间】:2025-12-28 02:55:06
【问题描述】:

我希望从笔记本的单元格中返回 Jupyter Notebook 的版本。

例如,要获取python版本,我运行:

from platform import python_version
python_version()

或获取 pandas 版本:

pd.__version__

我试过了:

notebook.version()
ipython.version()
jupyter.version()

以及其他几种相关形式(包括首字母大写),但会出现以下错误(例如):

NameError: 名称 'jupyter' 未定义

我知道其他方式(例如单击 GUI 菜单中的帮助>关于;使用 conda 命令行),但我想自动记录所有包版本。

如果重要的话,我在 Python 3.7.3 环境中运行笔记本 v6.1.1。

【问题讨论】:

    标签: python jupyter-notebook jupyter-lab


    【解决方案1】:

    如果您只需要知道 jupyter 实验室版本,这应该可以:

    import jupyterlab
    print(jupyterlab.__version__)
    

    【讨论】:

      【解决方案2】:

      将以下命令粘贴到您的jupyter单元格中(感叹号表示您需要运行shell命令,而不是python)

      !jupyter --version
      

      示例输出:

      jupyter core     : 4.6.0
      jupyter-notebook : 6.0.1
      qtconsole        : 4.7.5
      ipython          : 7.8.0
      ipykernel        : 5.1.3
      jupyter client   : 5.3.4
      jupyter lab      : not installed
      nbconvert        : 5.6.0
      ipywidgets       : 7.5.1
      nbformat         : 4.4.0
      traitlets        : 4.3.3
      

      要获取 python 版本,请使用python --version 命令:

      !python --version
      

      示例输出:

      Python 3.6.8
      

      更新: 要获取 dict 的值,您可以使用以下脚本(不完美,3 分钟内编写)

      import subprocess
      versions = subprocess.check_output(["jupyter", "--version"]).decode().split('\n')
      parsed_versions = {}
      for component in versions:
          if component == "":
              continue
          comps = list(map(str.strip, component.split(': ')))
          parsed_versions[comps[0]] = comps[1]
      

      parsed_versions 变量的值

      {
          "jupyter core": "4.6.0",
          "jupyter-notebook": "6.0.1",
          "qtconsole": "4.7.5",
          "ipython": "7.8.0",
          "ipykernel": "5.1.3",
          "jupyter client": "5.3.4",
          "jupyter lab": "not installed",
          "nbconvert": "5.6.0",
          "ipywidgets": "7.5.1",
          "nbformat": "4.4.0",
          "traitlets": "4.3.3"
      }
      

      更新 2:感谢 @TrentonMcKinney 就如何改进此脚本提出建议

      【讨论】:

      • 干杯。有没有办法将结果作为列表(或字典)返回?
      • @CreekGeek,我已经更新了答案并添加了脚本以将所有 jupyter 组件版本解析为 dict
      • 太棒了!正是我正在寻找的。为你和特伦顿干杯。
      • 很高兴为您提供帮助:)