An often asked question, I know, but after searching through other solutions I'm still having problems. Most solutions say to make sure your variables are exported before trying to access them, but have done that in my .profile

pi@raspberrypi ~ $ cat .profile | grep "export"
export MYCODE=/home/pi/Code
export MYINSTALL=/home/pi/gs_dist

After running the .profile again, the variables are clearly visible.

pi@raspberrypi ~ $ . .profile
pi@raspberrypi ~ $ env | grep "MY"

And the variables are being properly exported and are visible to sub shells

pi@raspberrypi ~ $ echo $MYCODE
pi@raspberrypi ~ $ bash
pi@raspberrypi ~ $ echo $MYCODE
pi@raspberrypi ~ $ exit
pi@raspberrypi ~ $

I'm running my application via a custom script /usr/bin/mycmd which throws an error caused by os.getenv() not finding MYCODE.

pi@raspberrypi ~ $ mycmd start
unsupported operand type(s) for +: 'NoneType' and 'str'

Here are the relevant parts of my code


case "$1" in
     sudo python $MYCODE/launcher.py


import os
import subprocess
import time
import datetime

processlist = []

def main():
        p = subprocess.Popen(['python', os.getenv('MYCODE') + '/sensor_launcher.py'])
        p2 = subprocess.Popen(['python', os.getenv('MYCODE') + '/server_listener.py'])

        while True:
                #just loop until program is exited...

if __name__ == "__main__":
        except Exception as e:
            print str(e)
                print "killing processes..."

So for some reason, the python script is not able to see the environment variable even though it is exported.

Now here's where it gets really interesting. If I open up a python shell and try to access the variable...

pi@raspberrypi ~ $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.getenv("MYCODE")

it works!

Also of note, I add the lines

print os.getenv('PATH')
print os.getenv('MYCODE')

into main(), it says


BUT, that's not what my PATH actually is.

pi@raspberrypi ~ $ echo $PATH

So clearly, something very strange is going on. Why is this happening? How can I fix this so that the python script gets the correct values?

  • 2
    Do you need to use sudo? It is dangerous and bad practice unless needed.
    – joan
    Jun 20, 2018 at 15:28

2 Answers 2


The problem is the use of sudo. sudo will clear environment variables by default.

You can use sudo -E (or --preserve-env) to not clear the environment. This can also be set in your sudoers file.

  • Bravo! I've learned something today :)
    – Seamus
    Jun 20, 2018 at 19:07

python script

# argstest.py

import sys

for line in sys.stdin.readlines():
    print('boom', line)

from bash

export -p | ./argstest.py

from the man page

The export special built-in shall support the Base Definitions volume of POSIX.1-2008, Section 12.2, Utility Syntax Guidelines.

       When -p is specified, export shall write to the standard output the names and values of all exported variables, in  the  following

           "export %s=%s\n", <name>, <value>

if you really wanted to get wacky . . .

export -p | ./argstest.py | export

and from python


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.