如何在Jupyter Notebook中查找版本号

8

我希望从笔记本电脑的单元格内返回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环境下运行Notebook v6.1.1。

2个回答

18
把以下命令粘贴到你的jupyter单元格中(惊叹号表示你需要运行shell命令,而不是Python)。

把以下命令粘贴到你的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 --version 命令获取 Python 版本:

!python --version

示例输出:

Python 3.6.8

更新:如果你想将值以字典形式获取,可以使用以下脚本(不完美,只花了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
@CreekGeek,我已经更新了答案,并添加了脚本以将所有Jupyter组件版本解析为字典。 - n0nvme
2
太棒了!正是我在寻找的东西。祝你和 Trenton 万事如意。 - CreekGeek
很高兴能帮助你 :) - n0nvme

2

如果您只需要从Python代码中知道Jupyter Lab的版本,可以使用以下方法:

import jupyterlab
print(jupyterlab.__version__)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接