Environmental Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sathyaish

    #1

    Environmental Variables

    In which physical file are the python environmental variables located?
    I know I can access them using the:


    os.environ.get( 'PYTHONSTARTUP' )

    or

    os.environ.get( 'PYTHONPATH')


    to get their values. But out of the program, if I need to look at them
    and alter their values, where do I find them? Are they the OS
    environmental variables as I suspect? If they are, then I'll find them
    with the other OS environ variables in My
    Computer->System->Properties->Advanced->Environmenta l Variables.

    But I checked that place and did not find any default values for them.

  • Diez B. Roggisch

    #2
    Re: Environmental Variables

    Sathyaish wrote:
    [color=blue]
    > In which physical file are the python environmental variables located?
    > I know I can access them using the:
    >
    >
    > os.environ.get( 'PYTHONSTARTUP' )
    >
    > or
    >
    > os.environ.get( 'PYTHONPATH')
    >
    >
    > to get their values. But out of the program, if I need to look at them
    > and alter their values, where do I find them? Are they the OS
    > environmental variables as I suspect? If they are, then I'll find them
    > with the other OS environ variables in My
    > Computer->System->Properties->Advanced->Environmenta l Variables.
    >
    > But I checked that place and did not find any default values for them.[/color]

    They aren't necessarily stored somewhere - except in memory. You can open a
    cmd-prompt and do something like

    set FOO "bar"

    (syntax may vary, I'm a UNIX-guy)

    Then you will be able to access this variable from a process - like python.
    But it is nowhere saved.

    Diez

    Comment

    • Fredrik Lundh

      #3
      Re: Environmental Variables

      Sathyaish wrote:
      [color=blue]
      > In which physical file are the python environmental variables located?
      > I know I can access them using the:
      >
      >
      > os.environ.get( 'PYTHONSTARTUP' )
      >
      > or
      >
      > os.environ.get( 'PYTHONPATH')
      >
      >
      > to get their values. But out of the program, if I need to look at them
      > and alter their values, where do I find them? Are they the OS
      > environmental variables as I suspect?[/color]

      yes.
      [color=blue]
      > If they are, then I'll find them with the other OS environ variables in My
      > Computer->System->Properties->Advanced->Environmenta l Variables.[/color]

      yes, provided that they've been set, and set via that dialogue (and not
      by some startup script or other mechanism).
      [color=blue]
      > But I checked that place and did not find any default values for them.[/color]

      there are no default values for the variables you mention.

      (as written, the environ.get call returns None if the variable is not set)

      </F>



      Comment

      • Sathyaish

        #4
        Re: Environmental Variables

        Thanks for the replies.

        I am trying to have a startup file execute everytime I launch the
        interpreter. So, for a test, I wrote a small file I called
        "Sathyaish. py". The contents of the file were simply:

        # ! This is my new start-up file.

        print "Sathyaish is the best."


        Then, in the interpreter, on its prompt, I said:
        [color=blue][color=green][color=darkred]
        >>> os.environ['PYTHONSTARTUP'] = 'C:\\Sathyaish. py'
        >>>[/color][/color][/color]

        It didn't complain. I tested it immediately. On the same interpreter
        instance, I said:
        [color=blue][color=green][color=darkred]
        >>> import os
        >>> os.environ.get( 'PYTHONSTARTUP' )[/color][/color][/color]

        It gave me back 'C:\Sathyaish.p y'. I didn't close that interpreter. I
        left it open and launched a new instance of the interpreter. In this I
        queried the same:

        [color=blue][color=green][color=darkred]
        >>> import os
        >>> os.environ.get( 'PYTHONSTARTUP' )[/color][/color][/color]
        None

        was what I got. I checked the at DOS prompt (cmd) for PYTHONSTARTUP,
        and I got an 'unrecognized program/command/batch file' interrupt.

        What's the deal with environmental variables? Are they specific to an
        interpreter session? That shouldn't be.

        Comment

        • Diez B. Roggisch

          #5
          Re: Environmental Variables

          > was what I got. I checked the at DOS prompt (cmd) for PYTHONSTARTUP,[color=blue]
          > and I got an 'unrecognized program/command/batch file' interrupt.
          >
          > What's the deal with environmental variables? Are they specific to an
          > interpreter session? That shouldn't be.[/color]

          Yes they are - and yes, it should be that way. A process inherits the
          environment of its parent. It can set its own environment, and if it forks
          children, these will see that. But there is no way for a process to alter
          the environment of its parent, let alone some arbitrary other process. This
          is an OS-limitation (or feature) - so if you absolutely have to convince
          somebody that it is bad, try Redmond :)

          DIez

          Comment

          • Duncan Booth

            #6
            Re: Environmental Variables

            Sathyaish wrote:
            [color=blue]
            > What's the deal with environmental variables? Are they specific to an
            > interpreter session? That shouldn't be.[/color]

            If you think that then complain to Microsoft, or even the people who
            developed Unix since that is what Microsoft based their environment
            variables on.

            Environment variables work the same way in Python as they do in any other
            process: any process can modify its own environment variables and pass
            those changes down to other processes which it starts, but no process can
            modify the environment of its parent process or any other process in the
            system.

            Comment

            • Fredrik Lundh

              #7
              Re: Environmental Variables

              Sathyaish wrote:
              [color=blue]
              > What's the deal with environmental variables? Are they specific to an
              > interpreter session?[/color]

              they're copied from the parent process when a new process is started,
              and any changes to them are local to the process.
              [color=blue]
              > That shouldn't be.[/color]

              that's how environment variables work, on all platforms.

              </F>



              Comment

              • Sathyaish

                #8
                Re: Environmental Variables

                I recall now, the shells in Unix - a child inherited the variables
                declared in its parent but not vice-versa. It works the same way in
                DOS. So, I wasn't seeing it clearly earlier. I am seeing it clearly
                now. I was imagining that the PYTHONPATH had some default value on
                installation and was expecting to see it. And since it was an
                *environmental* variable I was setting, I just expected it to turn up
                in the entire environment, totally forgetting the scope of the
                environment I was declaring it in - a process and not an OS.

                Thanks.

                Comment

                • John Salerno

                  #9
                  Re: Environmental Variables

                  Sathyaish wrote:[color=blue]
                  > In which physical file are the python environmental variables located?
                  > I know I can access them using the:
                  >
                  >
                  > os.environ.get( 'PYTHONSTARTUP' )
                  >
                  > or
                  >
                  > os.environ.get( 'PYTHONPATH')
                  >
                  >
                  > to get their values. But out of the program, if I need to look at them
                  > and alter their values, where do I find them? Are they the OS
                  > environmental variables as I suspect? If they are, then I'll find them
                  > with the other OS environ variables in My
                  > Computer->System->Properties->Advanced->Environmenta l Variables.
                  >
                  > But I checked that place and did not find any default values for them.
                  >[/color]

                  As far as PYTHONPATH goes, I added that one manually to my list of
                  environment variables. As far as I know, it's not there by default.

                  Now, can someone tell me if adding it manually interferes with anything
                  else Python itself is doing?

                  Comment

                  • Diez B. Roggisch

                    #10
                    Re: Environmental Variables

                    Dennis Lee Bieber wrote:[color=blue]
                    > The Amiga did have a means for such... It differentiated between
                    > local and global environment variables. Locals were kept in a process
                    > memory structure and behaved as they do on most other OSs... Globals,
                    > however, were short files maintained in ENV: (a logical name to a disk
                    > directory); so any process changing the file contents (whether
                    > explicitly using open/write/close, or implicitly with SETENV name
                    > value) would be visible in all other processes. Locals were defined
                    > using just SET name value.[/color]

                    Well, the amiga lacked a MMU - so you could do _anything_ if you really
                    wanted :)

                    And I consider such a "feature" risky - think of buffer-overflows which
                    could be provoked in running processes with other user-rights.

                    But nonetheless I loved the AMIGA and its OS - actually I only moved to a PC
                    in 96 when I discovered that there was a OS (Linux) that appealed to me in
                    similar ways as the AMIGA-os did.

                    Diez

                    Comment

                    Working...