Environment variables not visible from Python

Discussion in 'Python' started by Steven D'Aprano, Sep 22, 2011.

  1. 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
     
    Steven D'Aprano, Sep 22, 2011
    #1
    1. Advertising

  2. 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:58)
    [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.
     
    Hegedüs, Ervin, Sep 22, 2011
    #2
    1. Advertising

  3. On Thu, Sep 22, 2011 at 11:42 AM, 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 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
     
    Kushal Kumaran, Sep 22, 2011
    #3
  4. 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
     
    Thomas Rachel, Sep 22, 2011
    #4
  5. Ben Finney wrote:

    > Steven D'Aprano <> 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
     
    Steven D'Aprano, Sep 22, 2011
    #5
  6. 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.
     
    Thomas Rachel, Sep 22, 2011
    #6
  7. 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.
     
    Peter Pearson, Sep 22, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad

    button visible/not visible

    tshad, Oct 26, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,449
    Brian Hoops
    Oct 28, 2004
  2. tshad

    Sections visible and not visible

    tshad, Jan 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,064
    tshad
    Jan 31, 2005
  3. Patrick Olurotimi Ige
    Replies:
    7
    Views:
    5,971
    Patrick Olurotimi Ige
    Jun 15, 2005
  4. tshad
    Replies:
    1
    Views:
    4,256
    tshad
    Oct 31, 2009
  5. Jurjen de Groot
    Replies:
    0
    Views:
    309
    Jurjen de Groot
    May 19, 2008
Loading...

Share This Page