[英]how to pass variable from python to shell script when shell script is getting called inside python script
[英]accessing environment variable in python script when called from a shell script differs from running it in the shell directly
我有一個shell腳本正在調用python腳本
#!/bin/bash
sudo python test.py
test.py正在訪問一些環境變量
os.getenv('MYKEY')
從外殼程序腳本調用python腳本時,我什么也None
。 但是,如果直接從外殼執行test.py
它將很好地工作。
請幫忙
幾乎可以肯定的是,在調用shell腳本之前,沒有export MYKEY
,因此shell腳本實際上無權訪問MYKEY
,因此python腳本也無權訪問它。
sudo
默認不保留環境變量。
查看使用sudo時如何保留環境變量 。
這是我為重現您的結果所做的工作。
$ export MYKEY=5
$ python test.py
5
$ sudo python test.py
None
如果您使用python test.py
而不是sudo python test.py
則您的shell腳本應該給出相同的結果。 如果仍然要使用sudo
,則需要使用sudo -E bash -c 'python test.py'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.