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

时间:2020-10-12 18:54:59

标签: python jupyter-notebook jupyter-lab

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

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

from platform import python_version
python_version()

或获取熊猫版本:

pd.__version__

我尝试过:

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

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

NameError:未定义名称'jupyter'

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

如果有关系,我将在Python 3.7.3环境中运行Notebook v6.1.1。

1 个答案:

答案 0 :(得分:5)

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