Reputation: 45
I have a shell script which is calling the python script
#!/bin/bash
sudo python test.py
test.py is accessing some environment variable
os.getenv('MYKEY')
I get None
when python script is being called from shell script. However it works fine if test.py
is executed directly from the shell.
Please help
Upvotes: 0
Views: 78
Reputation: 36
sudo
does not keep environment variables by default.
See how to keep environment variables when using sudo.
Here is what I did to reproduce your result.
$ export MYKEY=5
$ python test.py
5
$ sudo python test.py
None
Your shell script should give the same result if you use python test.py
rather than sudo python test.py
. If you still want to use sudo
, then you would need to use sudo -E bash -c 'python test.py'
.
Upvotes: 1
Reputation: 14510
You almost certainly did not export MYKEY
before invoking your shell script, so that shell script actually has no access to MYKEY
and so the python script also has no access to it.
Upvotes: 1