如何从 python 脚本中获取变量

How to get variables from a python script

我 运行宁 this code 为 ALBERT,Google 在 Google Colab 上的机器学习模型之一。在代码的末尾,他们通过使用 运行 使用 !(shell)到 运行 脚本来完成所有操作。但是,我想在代码具有 运行.

之后对生成的模型做一些事情

是否有任何方法可以访问或获取脚本以我在 Colab 中的代码之后可以访问的方式输出特定变量?

这是表达我的问题的另一种方式。将 $HELLO_WORLD 放入 shell 命令 访问 我的 Colab 代码中的 HELLO_WORLD 变量。 有什么方法可以让脚本在我的 Colab 代码中设置 HELLO_WORLD 变量?

你可以这样使用os.environ

import os
os.environ['HELLO_WORLD']='hello world from Python'

然后

!echo $HELLO_WORLD

# hello world from Python