Environment variables not visible from Python

S

Steven D'Aprano

I don't understand why some environment variables are not visible from
Python.

[steve@wow-wow ~]$ echo $LINES $COLUMNS $TERM
30 140 xterm
[steve@wow-wow ~]$ python2.6
Python 2.6.6 (r266:84292, Dec 21 2010, 18:12:50)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.(None, None, 'xterm')
 
H

Hegedüs, Ervin

hello,

I don't understand why some environment variables are not visible from
Python.

[steve@wow-wow ~]$ echo $LINES $COLUMNS $TERM
30 140 xterm
[steve@wow-wow ~]$ python2.6
Python 2.6.6 (r266:84292, Dec 21 2010, 18:12:50)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.(None, None, 'xterm')

I think TERM is inherited from parent shell, but LINES and
COLUMNS are re-created every child shell. IMHO it's normally,
cause TERM will not change in child, but another variables should
changed...


Look at this:

airween@sebulba:~$ export LINES COLUMNS TERM
airween@sebulba:~$ python2.6
Python 2.6.6 (r266:84292, Mar 25 2011, 19:24:58)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.('65', '210', 'rxvt-256color')


a.
 
K

Kushal Kumaran

I don't understand why some environment variables are not visible from
Python.

[steve@wow-wow ~]$ echo $LINES $COLUMNS $TERM
30 140 xterm
[steve@wow-wow ~]$ python2.6
Python 2.6.6 (r266:84292, Dec 21 2010, 18:12:50)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.(None, None, 'xterm')

I have this:

$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.$ python -S
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
I hope it helps narrow things down somewhat.
 
T

Thomas Rachel

Am 22.09.2011 08:12 schrieb Steven D'Aprano:
I don't understand why some environment variables are not visible from
Python.

[steve@wow-wow ~]$ echo $LINES $COLUMNS $TERM
30 140 xterm
[steve@wow-wow ~]$ python2.6
Python 2.6.6 (r266:84292, Dec 21 2010, 18:12:50)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.(None, None, 'xterm')

They are no environment variables, but merely shell variables.

You can turn them into environment variables with the shell command
"export". After exporting them, they are visible by Python.

The environment can be obtained with env.

So try:

$ python -c 'import os; print "\n".join(sorted("%s=%s" % (k,v) for k,v
in os.environ.iteritems()))' | diff -u - <(env|LANG=C sort)
@@ -61,4 +61,4 @@
XDG_DATA_DIRS=/usr/share
XKEYSYMDB=/usr/share/X11/XKeysymDB
XNLSPATH=/usr/share/X11/nls
-_=/usr/bin/python
+_=/usr/bin/env

and you see that they (nearly) match.


Try as well

$ python -c 'import os; print "\n".join(os.getenv(k) or "" for k in
("LINES","COLUMNS","TERM"))'


linux
$ export LINES
$ python -c 'import os; print "\n".join(os.getenv(k) or "" for k in
("LINES","COLUMNS","TERM"))'
24

linux
$ export COLUMNS
$ python -c 'import os; print "\n".join(os.getenv(k) or "" for k in
("LINES","COLUMNS","TERM"))'
24
80
linux
$

HTH,

Thomas
 
S

Steven D'Aprano

Ben said:
Not all variables are environment variables. Variables only become
environment variables if exported to the environment; the ‘export’
command is one way to do that.

I see. Thank you to everyone who answered.
 
T

Thomas Rachel

Am 22.09.2011 12:16 schrieb Ben Finney:
--
\ “As far as the laws of mathematics refer to reality, they are |
`\ not certain, and as far as they are certain, they do not refer |
_o__) to reality.†—Albert Einstein, 1983 |
Ben Finney

So, he said what in 1983? Wow.
 
P

Peter Pearson

On Thu, 22 Sep 2011 09:21:59 +0200, Thomas Rachel wrote:
[snip]
$ python -c 'import os; print "\n".join(sorted("%s=%s" % (k,v) for k,v
in os.environ.iteritems()))' | diff -u - <(env|LANG=C sort)

[standing ovation]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top