如何从笔记本中查找 jupyter notebook 的版本

How to find the version of jupyter notebook from within the notebook

我希望从笔记本的一个单元格中 return Jupyter Notebook 版本。

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

from platform import python_version
python_version()

或获取 pandas 版本:

pd.__version__

我试过:

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

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

NameError: name 'jupyter' is not defined

我知道其他方法(例如单击 GUI 菜单中的“帮助”>“关于”;使用 conda 命令行)但我想自动生成所有包版本的文档。

如果重要的话,我正在 运行在 Python 3.7.3 环境中使用 Notebook v6.1.1。

将以下命令粘贴到您的 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 提供有关如何改进此脚本的建议

如果您只需要从 python 代码中了解 jupyter lab 版本,这应该可行:

import jupyterlab
print(jupyterlab.__version__)