Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Environment variables not visible from Python

Reply
Thread Tools

Environment variables not visible from Python

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-22-2011
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.
>>> import os
>>> (os.getenv('LINES'), os.getenv('COLUMNS'), os.getenv('TERM'))

(None, None, 'xterm')



--
Steven
 
Reply With Quote
 
 
 
 
Hegedüs, Ervin
Guest
Posts: n/a
 
      09-22-2011
hello,

On Thu, Sep 22, 2011 at 06:12:01AM +0000, Steven D'Aprano wrote:
> 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.
> >>> import os
> >>> (os.getenv('LINES'), os.getenv('COLUMNS'), os.getenv('TERM'))

> (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:5
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> (os.getenv('LINES'), os.getenv('COLUMNS'), os.getenv('TERM'))

('65', '210', 'rxvt-256color')


a.

 
Reply With Quote
 
 
 
 
Kushal Kumaran
Guest
Posts: n/a
 
      09-22-2011
On Thu, Sep 22, 2011 at 11:42 AM, Steven D'Aprano
<steve+> wrote:
> 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.
>>>> import os
>>>> (os.getenv('LINES'), os.getenv('COLUMNS'), os.getenv('TERM'))

> (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.
>>> import os
>>> os.getenv('LINES')
>>>

$ python -S
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
>>> import os
>>> os.getenv('LINES')

'35'
>>>


I hope it helps narrow things down somewhat.

--
regards,
kushal
 
Reply With Quote
 
Thomas Rachel
Guest
Posts: n/a
 
      09-22-2011
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.
>>>> import os
>>>> (os.getenv('LINES'), os.getenv('COLUMNS'), os.getenv('TERM'))

> (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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-22-2011
Ben Finney wrote:

> Steven D'Aprano <steve+> writes:
>
>> I don't understand why some environment variables are not visible from
>> Python.

>
> 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.


--
Steven

 
Reply With Quote
 
Thomas Rachel
Guest
Posts: n/a
 
      09-22-2011
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.

 
Reply With Quote
 
Peter Pearson
Guest
Posts: n/a
 
      09-22-2011
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]

--
To email me, substitute nowhere->spamcop, invalid->net.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Input button visible when not visible tshad ASP .Net 1 10-31-2009 10:53 PM
Hide textbox / Make textbox not visible (NOT USING visible property) Jurjen de Groot ASP .Net Web Controls 0 05-19-2008 09:50 AM
How to make a hyperlink Visible or not visible in DataList Patrick Olurotimi Ige ASP .Net 7 06-15-2005 12:01 PM
Sections visible and not visible tshad ASP .Net 4 01-31-2005 09:30 PM
button visible/not visible tshad ASP .Net 6 10-28-2004 10:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57