PATH environment variable

Discussion in 'Python' started by mirandacascade@yahoo.com, Nov 20, 2005.

  1. Guest

    O/S: Win2K
    Vsn of Python:2.4

    Based on a search of other posts in this group, it appears as though
    os.environ['PATH'] is one way to obtain the PATH environment variable.

    My questions:
    1) is it correct that os.environ['PATH'] contains the PATH environment
    variable?
    2) are there other ways to obtain the PATH environment variable? if so,
    is one way of obtaining the PATH environment variable preferable to
    another way?
     
    , Nov 20, 2005
    #1
    1. Advertising

  2. wrote:

    > Based on a search of other posts in this group, it appears as though
    > os.environ['PATH'] is one way to obtain the PATH environment variable.
    >
    > My questions:
    > 1) is it correct that os.environ['PATH'] contains the PATH environment
    > variable?


    yes.

    > 2) are there other ways to obtain the PATH environment variable? if so,
    > is one way of obtaining the PATH environment variable preferable to
    > another way?


    no. using the environ variable is the preferred way to read environment
    variables.

    </F>
     
    Fredrik Lundh, Nov 20, 2005
    #2
    1. Advertising

  3. wrote:
    > O/S: Win2K
    > Vsn of Python:2.4
    >
    > Based on a search of other posts in this group, it appears as though
    > os.environ['PATH'] is one way to obtain the PATH environment variable.
    >
    > My questions:
    > 1) is it correct that os.environ['PATH'] contains the PATH environment
    > variable?
    > 2) are there other ways to obtain the PATH environment variable? if so,
    > is one way of obtaining the PATH environment variable preferable to
    > another way?
    >


    That's the best way because it's portable. If you didn't have
    os.environ, you might be able to get PATH but reading the output of some
    subprocess, e.g:

    import commands
    path = commands.getoutput('echo $PATH')
     
    Jeffrey Schwab, Nov 20, 2005
    #3
  4. Guest

    I observed something tangential to this topic, and that is the reason
    for this reply. I don't understand when os.environ gets updated. The
    '...captured the first time the os mdule is imported...' verbiage from
    the following link:

    http://www.python.org/dev/doc/newstyle/lib/os-procinfo.html

    provides some information, but it's not clear how to make that fit with
    the following observations:

    Sequence of steps...observation 1:
    1) open Pythonwin interactive window
    2) import os
    3) os.environ['PATH'] - this presents the contents of the PATH variable
    4) using Windows system properties/environment variables, change
    contents of PATH variable; apply the changes (after closing, I got back
    in to verify that the PATH variable was, in fact, changed)
    5) in interactive window, reload(os)
    6) os.environ['PATH'] - presents same value is in #3

    Sequence of steps...observation 2:
    1) open Pythonwin interactive window
    2) import os
    3) os.environ['PATH'] - this presents the contents of the PATH variable
    4) using Windows system properties/environment variables, change
    contents of PATH variable; apply the changes (after closing, I got back
    in to verify that the PATH variable was, in fact, changed)
    5) in interactive window, del os
    6) in interactive window, import os
    7) os.environ['PATH'] - presents the same value as in #3

    I also observed that if I exit the interactive window, and then go back
    into the interactive window, the os.environ['PATH'] reflects changes to
    the PATH environment variable that happened while in the interactive
    window the first time.

    Do these observations fit with what is stated in section 6.1.1 of the
    Python Library Reference?
     
    , Nov 20, 2005
    #4
  5. wrote:

    > Do these observations fit with what is stated in section 6.1.1 of the
    > Python Library Reference?


    yes. what part of your observations makes you think that the
    environment isn't "captured" (i.e. copied from the process environ-
    ment) when the os module is imported ?

    (hint: when you start a new process, it gets a *copy* of the
    current environment. changes to the original won't affect this
    copy. all modern operating systems work the same way).

    </F>
     
    Fredrik Lundh, Nov 20, 2005
    #5
  6. Guest

    Fredrik Lundh wrote:
    > what part of your observations makes you think that the environment isn't "captured" (i.e. > copied from the process environment) when the os module is imported ?


    Answer: the part that was informed by a fundamental misunderstanding on
    my part of how the os module obtains information.

    Based on Mr. Lundh's 'copied from the process environment' remark and
    his hint, I would infer the following:

    1) In the observations I detailed, the 'process' is the pythonwin
    application
    2) When the pythonwin application is invoked, it obtains a copy of the
    current environment
    3) the import os statement obtains information from the copy of the
    current environment that was obtained when the pythonwin process began
    4) a reload(os) or a combination of del os followed by import os also
    get information from the copy of the current environment that was
    obtained when the pythonwin process began

    My questions:
    a) are the above inferences (albeit oversimplified) correct?
    b) when the hint refers to a new process, does the term 'process' refer
    to the same concept that is described in secion 6.1.5 of the Python
    Library Reference?
     
    , Nov 20, 2005
    #6
    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. Glennn
    Replies:
    5
    Views:
    6,073
    Jim Higson
    Apr 6, 2006
  2. Chris  Ashley

    ASP.Net and PATH environment variable

    Chris Ashley, Nov 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    418
    Chris Ashley
    Nov 7, 2006
  3. Replies:
    5
    Views:
    688
  4. chandan
    Replies:
    1
    Views:
    383
    Naraendirakumar R.R.
    Jan 8, 2008
  5. chandan
    Replies:
    1
    Views:
    320
Loading...

Share This Page