This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.environ.get() does not return the Environment Value in Linux
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, r.david.murray, themetman
Priority: normal Keywords:

Created on 2015-08-31 17:19 by themetman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (9)
msg249416 - (view) Author: Francis Greaves (themetman) Date: 2015-08-31 17:19
I am running an up to date Gentoo Linux x86_64. using Python 2.7
I have an environment variable defined, so from the command line 'echo $XRIT_DECOMPRESS_PATH' gives '/usr/local/bin/xRITDecompress'
however, in a Python script, 'cmd = os.environ.get('XRIT_DECOMPRESS_PATH')' gives 'None' as the output.
Why is that?
msg249418 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2015-08-31 18:28
Did you mark the variable for export in your shell? For example:

    $ v=1
    $ python -c 'import os;print os.environ.get("v")'
    None

    $ export v
    $ python -c 'import os;print os.environ.get("v")'
    1
msg249419 - (view) Author: Francis Greaves (themetman) Date: 2015-08-31 18:41
I have the environment variable in my .bashrc file.
Interestingly if I do 'os.environ.get("PATH")' it returns the correct value for $PATH. This is also defined in my .bashrc file, so it should be exported.
I have exported it manually from the command line, but it makes no difference.
The command is in a .py file run as 'python myscript.py' as follows
import os
cmd=os.environ.get("PATH")

this makes cmd as 'None'
??
msg249420 - (view) Author: Francis Greaves (themetman) Date: 2015-08-31 18:51
Sorry my error
I should have said
cmd = os.environ.get('XRIT_DECOMPRESS_PATH')' gives 'None' as the output
msg249424 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-31 19:05
Well, it works fine for us (I'm on Gentoo as well).  os.environ is a pretty thin wrapper around the os functions, so I'm not sure what to suggest as far as debugging why it isn't working for you.

Did you try exactly eryksun's example yourself?
msg249425 - (view) Author: Francis Greaves (themetman) Date: 2015-08-31 20:18
erykson's example works perfectly.
I have a script as follows

#!/bin/python
#
import os
cmd = os.environ.get('XRIT_DECOMPRESS_PATH')
print cmd

resulting in 'None'

and if I substitute this line
cmd = os.environ.get('PATH')

resulting in '/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.8.4:/usr/include/KDE'

however from the bash prompt
$ echo $XRIT_DECOMPRESS_PATH
results in '/usr/local/bin/xRITDecompress' so the environment variable  is there and available in bash, but not in Python.
I find this very strange
msg249426 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-08-31 20:21
That means you just haven't exported it properly.  There is nothing wrong with Python here, as demonstrated by the fact that you were able to run eryksun's example (which is equivalent to your script).
msg249460 - (view) Author: Francis Greaves (themetman) Date: 2015-09-01 06:55
OK, David, so what have I done wrong to not export it correctly?
in my .bashrc I have this
export XRIT_DECOMPRESS_PATH="/usr/local/bin/xRITDecompress"
so I would expect it to work.
Can you please clarify?
Many thanks
msg249461 - (view) Author: Francis Greaves (themetman) Date: 2015-09-01 06:57
Well since posting the last item it is now working correctly!!!!
Thank you for your help
History
Date User Action Args
2022-04-11 14:58:20adminsetgithub: 69159
2015-09-01 06:57:59themetmansetmessages: + msg249461
2015-09-01 06:55:35themetmansetmessages: + msg249460
2015-08-31 20:21:53r.david.murraysetstatus: open -> closed
resolution: not a bug
messages: + msg249426

stage: resolved
2015-08-31 20:18:36themetmansetmessages: + msg249425
2015-08-31 19:05:21r.david.murraysetnosy: + r.david.murray
messages: + msg249424
2015-08-31 18:51:51themetmansetmessages: + msg249420
2015-08-31 18:41:14themetmansetmessages: + msg249419
2015-08-31 18:28:04eryksunsetnosy: + eryksun
messages: + msg249418
2015-08-31 17:19:23themetmancreate