我正在尝试执行一个 BASH 脚本,该脚本从 Jupyter 笔记本中设置所需的环境变量。我知道魔术命令 %env 可以实现这一点,但在这种情况下需要 BASH 脚本。 !source 或 %system 的使用都无法实现使环境变量在 Jupyter notebook 中持久化的目标。可以这样做吗?
答案 0 :(得分:0)
你可以使用 python 来更新 os 变量:
细胞
! echo "export test1=\"This is the test 1\"" > test.sh
! echo "export test2=\"This is the test 2\"" >> test.sh
! cat test.sh
结果
export test1="This is the test 1"
export test2="This is the test 2"
单元格(取自set environment variable in python script)
import os
with open('test.sh') as f:
os.environ.update(
line.replace('export ', '', 1).strip().split('=', 1) for line in f
if 'export' in line
)
! echo $test1
! echo $test2
结果
"This is the test 1"
"This is the test 2"