python - 使用 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 脚本运行并设置环境变量的任何建议。
解决方案
见上面的评论:
#!/usr/bin/env bash
./binaryfileproducesenv_variables > ./env_variables_file
. ./env_variables_file
python anotherpythonscript.py
推荐阅读
- sonarqube - SonarQube Web API 不接受“c++”参数
- c++ - 如何在 MacOS 上将 gdb 设置为 C/C++ 扩展 pf VSCode 的调试器?
- swiftui - 是否可以在 iPad 应用程序中将 DatePicker 作为导航栏项?
- angular - npm i @angular/material 和 ng add @angular/material 有什么区别
- tomcat9 - org.apache.catalina.startup.ContextConfig.authenticatorConfig 无法实例化
- node.js - NodeJS在If条件下双重输出
- ruby - 您可以通过组合两个范围来创建嵌套数组吗?
- npm - 如何从命令行获取 npm 包访问权限?
- javascript - Jquery 从表数据中渲染货币
- c# - 在使用 web api post 方法的客户端中请求 postAsync 时出错