首页 > 解决方案 > 使用 Python 脚本设置环境环境变量

问题描述

操作系统:Mac OSX 10.14 Python 2.7

我有一个 python 脚本,如下所示:

#! /usr/bin/python

import os

os.system('./binaryfileproducesenv_variables > ./env_variables_file')
os.system('chmod 744 ./env_variables_file')
os.system('./env_variables_file')

os.system('python anotherpythonscript.py')

env_variables_file 看起来像这样:

passwordA='abcd';
passwordB='1234';
export passwordA passwordB

anotherpythonscript.py 只有在上面的环境变量设置正确的情况下才能正常工作。当我通过主 python 脚本运行它时,它没有定义变量。虽然,如果我直接从命令行运行 ./env_variables_file ,它将设置环境变量。关于如何通过 Python 脚本运行并设置环境变量的任何建议。

标签: pythonlinuxvariablesoperating-systemenvironment-variables

解决方案


见上面的评论:

#!/usr/bin/env bash
./binaryfileproducesenv_variables > ./env_variables_file
. ./env_variables_file
python anotherpythonscript.py

推荐阅读