Set an environment variable

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

    #16
    Re: Set an environment variable

    Christian wrote:[color=blue]
    > Steve Holden wrote:
    >
    >[color=green]
    >>::::::::::::: :
    >>one.py
    >>::::::::::::: :
    >>import os
    >>os.environ['STEVE'] = "You are the man"
    >>os.system("py thon two.py")
    >>print "Ran one"
    >>::::::::::::: :
    >>two.py
    >>::::::::::::: :
    >>import os
    >>print "STEVE is", os.environ['STEVE']
    >>print "Ran two"
    >>[sholden@headrat tmp]$ python one.py
    >>STEVE is You are the man
    >>Ran two
    >>Ran one
    >>[sholden@headrat tmp]$
    >>
    >>Hope this helps.
    >>
    >>regards
    >> Steve[/color]
    >
    >
    > Thanks Steve, you're quite right, you are the man. And thanks to all the
    > rest of you for your kind help and patient understanding. I have learned
    > quite a lot and is about to consider my self advanced to the status of
    > Python newbie.
    >
    > So here is my final question:
    > Do I call the .sh script with a .py script like this:
    >
    > os.system("/path/to/the/script/startupscript.s h")[/color]

    Time you answered your own questions by trying things at the interactive
    interpreter prompt!

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/

    Comment

    • Christian

      #17
      Re: Set an environment variable

      Steve Holden wrote:
      [color=blue]
      > Time you answered your own questions by trying things at the interactive
      > interpreter prompt!
      >
      > regards
      > Steve[/color]

      Right again, Steve.

      Thanks

      Comment

      • Grant Edwards

        #18
        Re: Set an environment variable

        On 2005-10-21, Christian <christian@spam .not> wrote:[color=blue][color=green]
        >>
        >> The closest thing you can do is that:
        >>
        >> -myScript.py--------------------------------------
        >> print 'export MY_VARIABLE=val ue'
        >> --------------------------------------------------
        >>
        >> -myScript.sh--------------------------------------
        >> python myScript.py > /tmp/chgvars.sh
        >> . /tmp/chgvars.sh
        >> --------------------------------------------------[/color][/color]

        Bullshit. Are people being intentionally misleading??
        [color=blue]
        > Can I write a .py script that calls a .sh script that executes the
        > export command and then calls another .py script (and how would the
        > first .py script look)?[/color]

        Good grief, that's ugly. Just use os.putenv().
        [color=blue]
        > That would be much more what is my basic problem.[/color]

        And even Google knows the correct answer



        Follow the first hit.

        --
        Grant Edwards grante Yow! TONY RANDALL! Is YOUR
        at life a PATIO of FUN??
        visi.com

        Comment

        • Mike Meyer

          #19
          Re: Set an environment variable

          Grant Edwards <grante@visi.co m> writes:
          [color=blue]
          > On 2005-10-21, Christian <christian@spam .not> wrote:[color=green][color=darkred]
          >>>
          >>> The closest thing you can do is that:
          >>>
          >>> -myScript.py--------------------------------------
          >>> print 'export MY_VARIABLE=val ue'
          >>> --------------------------------------------------
          >>>
          >>> -myScript.sh--------------------------------------
          >>> python myScript.py > /tmp/chgvars.sh
          >>> . /tmp/chgvars.sh
          >>> --------------------------------------------------[/color][/color]
          >
          > Bullshit. Are people being intentionally misleading??[/color]

          No. Are you being intentionally - never mind.
          [color=blue]
          > And even Google knows the correct answer
          >
          > http://www.google.com/search?hl=en&l...nment+variable
          >
          > Follow the first hit.[/color]

          The first hit is basically the solution presented above translated
          from Unix to Windows: your python script writes the appropriate shell
          commands into a file, then you get the command processor to process
          that file. The Windows version carries this a step further by wrapping
          it all up in a script to make it easy to run, but that's the only real
          difference. Maybe the results order has changed since you looked?

          Watch the recipe - I may add a Unix/sh solution.

          <mike

          --
          Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
          Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

          Comment

          • Grant Edwards

            #20
            Re: Set an environment variable

            On 2005-10-21, Mike Meyer <mwm@mired.or g> wrote:
            [color=blue][color=green][color=darkred]
            >>>> The closest thing you can do is that:
            >>>>
            >>>> -myScript.py--------------------------------------
            >>>> print 'export MY_VARIABLE=val ue'
            >>>> --------------------------------------------------
            >>>>
            >>>> -myScript.sh--------------------------------------
            >>>> python myScript.py > /tmp/chgvars.sh
            >>>> . /tmp/chgvars.sh
            >>>> --------------------------------------------------[/color]
            >>
            >> Bullshit. Are people being intentionally misleading??[/color]
            >
            > No. Are you being intentionally - never mind.[/color]

            Well, yes, probably.
            [color=blue][color=green]
            >> And even Google knows the correct answer
            >>
            >> http://www.google.com/search?hl=en&l...nment+variable
            >>
            >> Follow the first hit.[/color][/color]

            My bad. I got links mixed up -- it wasn't the first one, it
            was this one:

            http://www.faqts.com/knowledge_base/view.phtml/aid/3298

            There are two almost-equivalent tirival answers:

            os.environment['foo'] = 'bar'

            os.putenv('foo' ,'bar')

            I don't get why people seem to be obfuscating things with
            multiple layers of shells or writing shell commands to to a
            file and executing them.
            [color=blue]
            > Maybe the results order has changed since you looked?[/color]

            No, I mixed them up.

            My point: the OP wanted to know how to export an environment
            variable to a child process. Either of the lines of code above
            will do that, so what's with all the shellular shenanigans?

            --
            Grant Edwards grante Yow! Youth of today! Join
            at me in a mass rally
            visi.com for traditional mental
            attitudes!

            Comment

            • Mike Meyer

              #21
              Re: Set an environment variable

              Grant Edwards <grante@visi.co m> writes:[color=blue]
              > My point: the OP wanted to know how to export an environment
              > variable to a child process. Either of the lines of code above
              > will do that, so what's with all the shellular shenanigans?[/color]

              Actually, the OP didn't say he wanted to export a variable to a child
              process. He said he wanted to know how to do the equivalent of "export
              SYMBOL=value", which got intreprted as "setting a variable in the
              parent shell."

              <mike
              --
              Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
              Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

              Comment

              • Grant Edwards

                #22
                Re: Set an environment variable

                On 2005-10-21, Mike Meyer <mwm@mired.or g> wrote:[color=blue]
                > Grant Edwards <grante@visi.co m> writes:[color=green]
                >> My point: the OP wanted to know how to export an environment
                >> variable to a child process. Either of the lines of code above
                >> will do that, so what's with all the shellular shenanigans?[/color]
                >
                > Actually, the OP didn't say he wanted to export a variable to a child
                > process. He said he wanted to know how to do the equivalent of "export
                > SYMBOL=value", which got intreprted as "setting a variable in the
                > parent shell."[/color]

                The only think you can export an environment variable to is a
                child process, so I thought it obvious that was what he was
                doing with the "export" command example.

                --
                Grant Edwards grante Yow! I'm gliding over a
                at NUCLEAR WASTE DUMP near
                visi.com ATLANTA, Georgia!!

                Comment

                • Eric Brunel

                  #23
                  Re: Set an environment variable

                  On Fri, 21 Oct 2005 20:13:32 -0000, Grant Edwards <grante@visi.co m> wrote:
                  [color=blue]
                  > On 2005-10-21, Mike Meyer <mwm@mired.or g> wrote:[color=green]
                  >> Grant Edwards <grante@visi.co m> writes:[color=darkred]
                  >>> My point: the OP wanted to know how to export an environment
                  >>> variable to a child process. Either of the lines of code above
                  >>> will do that, so what's with all the shellular shenanigans?[/color]
                  >>
                  >> Actually, the OP didn't say he wanted to export a variable to a child
                  >> process. He said he wanted to know how to do the equivalent of "export
                  >> SYMBOL=value", which got intreprted as "setting a variable in the
                  >> parent shell."[/color]
                  >
                  > The only think you can export an environment variable to is a
                  > child process[/color]

                  Well, you know that, and I know that too. From my experience, many people don't...
                  [color=blue]
                  > so I thought it obvious that was what he was
                  > doing with the "export" command example.[/color]

                  Well, when I first read the OP's message, I thought it was obvious he wanted to export the variable to the parent shell. Re-reading it, it was actually not so obvious, so my apologies for not having included an example using os.environment. ..
                  --
                  python -c "print ''.join([chr(154 - ord(c)) for c in 'U(17zX(%,5.zmz 5(17;8(%,5.Z65\ '*9--56l7+-'])"

                  Comment

                  • Grant Edwards

                    #24
                    Re: Set an environment variable

                    On 2005-10-24, Eric Brunel <eric_brunel@de spammed.com> wrote:
                    [color=blue][color=green]
                    >> The only think you can export an environment variable to is a
                    >> child process[/color]
                    >
                    > Well, you know that, and I know that too. From my experience,
                    > many people don't...[/color]

                    True. Using Unix for 20+ years probably warps one's perception
                    of what's obvious and what isn't.

                    --
                    Grant Edwards grante Yow! My face is new, my
                    at license is expired, and I'm
                    visi.com under a doctor's care!!!!

                    Comment

                    • Alex Martelli

                      #25
                      Re: Set an environment variable

                      Grant Edwards <grante@visi.co m> wrote:
                      [color=blue]
                      > On 2005-10-24, Eric Brunel <eric_brunel@de spammed.com> wrote:
                      >[color=green][color=darkred]
                      > >> The only think you can export an environment variable to is a
                      > >> child process[/color]
                      > >
                      > > Well, you know that, and I know that too. From my experience,
                      > > many people don't...[/color]
                      >
                      > True. Using Unix for 20+ years probably warps one's perception
                      > of what's obvious and what isn't.[/color]

                      This specific issue is identical in Windows, isn't it? I do not know
                      any OS which does have the concept of "environmen t variable" yet lets
                      such variables be ``exported'' to anything but a child process.


                      Alex

                      Comment

                      • Jorgen Grahn

                        #26
                        Re: Set an environment variable

                        On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <aleaxit@yahoo. com> wrote:[color=blue]
                        > Grant Edwards <grante@visi.co m> wrote:
                        >[color=green]
                        >> On 2005-10-24, Eric Brunel <eric_brunel@de spammed.com> wrote:
                        >>[color=darkred]
                        >> >> The only think you can export an environment variable to is a
                        >> >> child process
                        >> >
                        >> > Well, you know that, and I know that too. From my experience,
                        >> > many people don't...[/color]
                        >>
                        >> True. Using Unix for 20+ years probably warps one's perception
                        >> of what's obvious and what isn't.[/color]
                        >
                        > This specific issue is identical in Windows, isn't it? I do not know
                        > any OS which does have the concept of "environmen t variable" yet lets
                        > such variables be ``exported'' to anything but a child process.[/color]

                        AmigaDOS, if I recall correctly. Its "ENV:" drive/namespace is global, and
                        that's its closest thing to Unix environment variables.

                        /Jorgen

                        --
                        // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
                        \X/ algonet.se> R'lyeh wgah'nagl fhtagn!

                        Comment

                        • Mike Meyer

                          #27
                          Re: Set an environment variable

                          Jorgen Grahn <jgrahn-nntq@algonet.se > writes:[color=blue]
                          > On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <aleaxit@yahoo. com> wrote:[color=green]
                          >> Grant Edwards <grante@visi.co m> wrote:[color=darkred]
                          >>> On 2005-10-24, Eric Brunel <eric_brunel@de spammed.com> wrote:
                          >>> >> The only think you can export an environment variable to is a
                          >>> >> child process
                          >>> > Well, you know that, and I know that too. From my experience,
                          >>> > many people don't...
                          >>> True. Using Unix for 20+ years probably warps one's perception
                          >>> of what's obvious and what isn't.[/color]
                          >> This specific issue is identical in Windows, isn't it? I do not know
                          >> any OS which does have the concept of "environmen t variable" yet lets
                          >> such variables be ``exported'' to anything but a child process.[/color]
                          > AmigaDOS, if I recall correctly. Its "ENV:" drive/namespace is global, and
                          > that's its closest thing to Unix environment variables.[/color]

                          AmigaDOS had both global environment variables (using the ENV: device)
                          and local environment variables, that worked like the Unix
                          version. You manipulated them in a similar way in the shell, and they
                          had a similar API for programmers: one call with a flag to indicate
                          which you wanted. The ENV: device was an implementation detail that
                          let you save/restore the state of the global environment with file
                          commands. The posix calls checked the local then global variables.

                          Of course, this is now 10+ year old memory, and I may not RC.

                          <mike
                          --
                          Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                          Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                          Comment

                          • Dennis Lee Bieber

                            #28
                            Re: Set an environment variable

                            On Wed, 26 Oct 2005 16:04:58 -0400, Mike Meyer <mwm@mired.or g> declaimed
                            the following in comp.lang.pytho n:
                            [color=blue]
                            >
                            > AmigaDOS had both global environment variables (using the ENV: device)
                            > and local environment variables, that worked like the Unix
                            > version. You manipulated them in a similar way in the shell, and they
                            > had a similar API for programmers: one call with a flag to indicate
                            > which you wanted. The ENV: device was an implementation detail that
                            > let you save/restore the state of the global environment with file
                            > commands. The posix calls checked the local then global variables.
                            >
                            > Of course, this is now 10+ year old memory, and I may not RC.
                            >[/color]
                            Close enough. ENV: was a system defined logical name for a standard
                            system directory. The contents of that directory contained short files
                            (one line). The environment variable name was the file name, the
                            variable value, then, was the content of the file.

                            CLI commands for manipulating them tended to be of the order of

                            setenv name value
                            vs
                            set name value
                            for a shell local environment variable.

                            But AmigaOS also had those logical names mentioned, and those were
                            used in some places UNIX uses environment variables.

                            How would you like to refer to a directory?

                            HD0:myDirectory
                            SYS:myDirectory
                            myDir:

                            The first references via the hardware partition name; the second by
                            a volume label, and the third by a logical name (assign myDir:
                            SYS:myDirectory )

                            Windows lets one assign a volume label to a partition, but it is not
                            used for anything except display. Amiga volume labels were useful: one
                            could refer to two separate floppy disks by two different volume labels,
                            and the OS would prompt the user to insert the disk by name as needed.
                            Even more, if the need was fresh, rather than to perform I/O on an
                            already opened file, once could satisfy the prompt by putting the floppy
                            in ANY available floppy drive
                            [color=blue]
                            > <mike[/color]
                            --[color=blue]
                            > =============== =============== =============== =============== == <
                            > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                            > wulfraed@dm.net | Bestiaria Support Staff <
                            > =============== =============== =============== =============== == <
                            > Home Page: <http://www.dm.net/~wulfraed/> <
                            > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

                            Comment

                            • Jorgen Grahn

                              #29
                              Re: Set an environment variable

                              On Wed, 26 Oct 2005 16:04:58 -0400, Mike Meyer <mwm@mired.or g> wrote:[color=blue]
                              > Jorgen Grahn <jgrahn-nntq@algonet.se > writes:[color=green]
                              >> On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <aleaxit@yahoo. com> wrote:[color=darkred]
                              >>> Grant Edwards <grante@visi.co m> wrote:
                              >>>> On 2005-10-24, Eric Brunel <eric_brunel@de spammed.com> wrote:
                              >>>> >> The only think you can export an environment variable to is a
                              >>>> >> child process
                              >>>> > Well, you know that, and I know that too. From my experience,
                              >>>> > many people don't...
                              >>>> True. Using Unix for 20+ years probably warps one's perception
                              >>>> of what's obvious and what isn't.
                              >>> This specific issue is identical in Windows, isn't it? I do not know
                              >>> any OS which does have the concept of "environmen t variable" yet lets
                              >>> such variables be ``exported'' to anything but a child process.[/color]
                              >> AmigaDOS, if I recall correctly. Its "ENV:" drive/namespace is global, and
                              >> that's its closest thing to Unix environment variables.[/color]
                              >
                              > AmigaDOS had both global environment variables (using the ENV: device)
                              > and local environment variables, that worked like the Unix
                              > version.[/color]

                              As I recalled it, the latter type was shell-local and not accessible to
                              normal processes ...
                              [color=blue]
                              > You manipulated them in a similar way in the shell, and they
                              > had a similar API for programmers: one call with a flag to indicate
                              > which you wanted.[/color]

                              .... but if there were system calls to access them, I must have remembered
                              incorrecly. Possibly I was too stupid back then to find enviroment variables
                              very useful ;-)
                              [color=blue]
                              > Of course, this is now 10+ year old memory, and I may not RC.[/color]

                              I think I remember /you/ though, from the Amiga newsgroups in the early
                              nineties. And now I feel old -- and offtopic.

                              /Jorgen

                              --
                              // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
                              \X/ algonet.se> R'lyeh wgah'nagl fhtagn!

                              Comment

                              Working...