![](/img/trans.png)
[英]How do I set environment variables in a python script using a dockerfile?
[英]How do I export environment variables using a script in the same shell in python?
我的 django 项目从环境变量中获取凭据,现在我想自动化这个过程并将凭据存储在保险库(hashivcorp)中。 I have a python and shell script which fetches data from an API and exports it as environment variables, when I run it using os.system command it runs the shell script but as it runs it in a subprocess, I can't access the variables在主(父)进程/外壳中。 唯一的方法是在 settings.py 文件中插入 shell 脚本。 有什么办法可以让我在主流程中得到这些吗? Ps:我确实尝试过采购,os.system 没有将其识别为命令。
这是我正在运行的代码:
import os
os.environ['ENV'] = 'Demo'
os.system('python3 /home/rishabh/export.py')
print(os.environ.get('RDS_DB_NAME'))
output:
None
python 文件,shell 脚本工作得很好。
一种方法是在同一进程中运行export.py
,正如user1934428建议的那样:
import os
import sys
os.environ['ENV'] = 'Demo'
sys.path.append('/home/rishabh/')
import export # runs export.py in the same process
print(os.environ.get('RDS_DB_NAME'))
这假设 export.py 中没有export.py
__name__ == '__main__'
检查。
如果export.py
与当前脚本位于不同的目录中,则只需要sys.path
行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.