我希望从笔记本的单元格中返回 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
更新:要将值作为字典,您可以使用以下脚本(不完美,在 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 - 如何从 notebook 中找到 jupyter notebook 的版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64323745/