Re: Set an environment variable

2005-10-20 Thread the_crazy88
Just use 
os.system("export PYTHONPATH = %s" %("your_pythonpath"))

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-20 Thread jepler
In Unix, you generally can't affect the environment of your parent program (in
a broad sense, which includes environment variables, current working directory,
opened files, effective user ID, etc).

You have two basic choices to achieve an effect like this.  First, you can
start a subshell with the desired environment, something like (untested)
os.environ['VARIABLE'] = 'value'
os.system(os.environ['shell'])
(commands like 'su' work in this way)

Second, you can produce shell commands that have the effect of changing a
shell's environment when they are executed.  Something like:
print "VARIABLE=value"
To use this, the user must write something like
eval `myscript.py`
instead of just
myscript.py
this can typically be encapsulated in a shell alias or function.
(commands like 'resize' (which sets the shell's idea of a terminal's size) work
this way)

Finally, you might be able to use an OS-specific interface like linux' ptrace
to change the actual contents of a calling process's memory.  I didn't
immediately find an example of this, but on Linux it would consist of using
PTRACE_PEEKDATA and PTRACE_POKEDATA to locate the environment in another
process and modify it.  The ptrace interface is not available from any standard
Python module, and isn't portable anyway. (though my manpage actually says
'CONFORMING TO SVr4, ..., X/OPEN, BSD 4.3' so maybe ptrace is more portable
than I believed)

Jeff


pgpGRSQal48ZE.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Set an environment variable

2005-10-20 Thread Christian
Thanks Jeff and the crazy 88.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-20 Thread Eric Brunel
On 20 Oct 2005 01:58:44 -0700, the_crazy88 <[EMAIL PROTECTED]> wrote:

> Just use
> os.system("export PYTHONPATH = %s" %("your_pythonpath"))

... except it won't work: os.system will execute the command in a new process, 
so the environment variable change will only be visible in *this* process. 
Since you don't do anything else, the environment variable change will never be 
seen by anyone.

As for the OP's question, the short answer is "you can't": the Python 
interpreter will always be executed in a different process than the calling 
shell, and environment variable changes *never* impact the calling process on 
Unix.

The closest thing you can do is that:

-myScript.py--
print 'export MY_VARIABLE=value'
--

-myScript.sh--
python myScript.py > /tmp/chgvars.sh
. /tmp/chgvars.sh
--

This is quite ugly: you write the shell commands changing the environment 
variables to a file, then "source" this file in the calling shell. But this is 
probably the best way to do what you want.

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in 
'U(17zX(%,5.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-20 Thread Grant Edwards
On 2005-10-20, the_crazy88 <[EMAIL PROTECTED]> wrote:

> os.system("export PYTHONPATH = %s" %("your_pythonpath"))

No, that won't work.  

That will set the environment variable in the shell spawned by
the os.system command.  That shell will then immediately exit,
leaving the caller's environment unchanged.

-- 
Grant Edwards   grante Yow!  Are you mentally here
  at   at Pizza Hut??
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-20 Thread Grant Edwards
On 2005-10-20, Christian <[EMAIL PROTECTED]> wrote:

> How do I export an environment variable in a .py script?

http://www.python.org/doc/current/lib/os-procinfo.html#l2h-1548

-- 
Grant Edwards   grante Yow!  My BIOLOGICAL ALARM
  at   CLOCK just went off... It
   visi.comhas noiseless DOZE FUNCTION
   and full kitchen!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-20 Thread Mike Meyer
"Eric Brunel" <[EMAIL PROTECTED]> writes:
> -myScript.py--
> print 'export MY_VARIABLE=value'
> --
>
> -myScript.sh--
> python myScript.py > /tmp/chgvars.sh
> . /tmp/chgvars.sh

It's simpler to use eval and command substitution:

eval $(python myScript.py)

   http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Christian
> 
> The closest thing you can do is that:
> 
> -myScript.py--
> print 'export MY_VARIABLE=value'
> --
> 
> -myScript.sh--
> python myScript.py > /tmp/chgvars.sh
> . /tmp/chgvars.sh
> --

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)?

That would be much more what is my basic problem.


Thanks

Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Erik Max Francis
Christian wrote:

> 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)?

No, the shell script that the Python program would invoke would be a 
different process and so commands executed in it would have no effect on 
the state of another.

-- 
Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   Success and failure are equally disastrous.
   -- Tennessee Williams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Christian
Erik Max Francis wrote:
> Christian wrote:
> 
>> 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)?
> 
> No, the shell script that the Python program would invoke would be a 
> different process and so commands executed in it would have no effect on 
> the state of another.
> 

So executing an .sh script that calls a .py script works different when 
executed from a command promt than when executed from a starter .py script?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Sybren Stuvel
Mike Meyer enlightened us with:
> It's simpler to use eval and command substitution:
>
> eval $(python myScript.py)

This looks like the best solution to me.

Sybren
-- 
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
 Frank Zappa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Steve Holden
Christian wrote:
>>The closest thing you can do is that:
>>
>>-myScript.py--
>>print 'export MY_VARIABLE=value'
>>--
>>
>>-myScript.sh--
>>python myScript.py > /tmp/chgvars.sh
>>. /tmp/chgvars.sh
>>--
> 
> 
> 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)?
> 
> That would be much more what is my basic problem.
> 
You can do what you suggest without shell scripting, unless I 
misunderstand your intention: just set the environment variables you 
want your Python script to see and then run it using os.system():

::
one.py
::
import os
os.environ['STEVE'] = "You are the man"
os.system("python two.py")
print "Ran one"
::
two.py
::
import os
print "STEVE is", os.environ['STEVE']
print "Ran two"
[EMAIL PROTECTED] tmp]$ python one.py
STEVE is You are the man
Ran two
Ran one
[EMAIL PROTECTED] tmp]$

Hope this helps.

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/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Chris F.A. Johnson
On 2005-10-21, Christian wrote:
> Erik Max Francis wrote:
>> Christian wrote:
>> 
>>> 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)?
>> 
>> No, the shell script that the Python program would invoke would be a 
>> different process and so commands executed in it would have no effect on 
>> the state of another.
>> 
>
> So executing an .sh script that calls a .py script works different when 
> executed from a command promt than when executed from a starter .py script?

No; it's always the same: an environment variable will only be
effective in the process in which it is set, and its children.

When you call another program, whether it's a shell script, python
script, or binary executable, you are starting a new process.
Environment variables set in that process will not affect its
parent (i.e., the process that called it).

-- 
Chris F.A. Johnson 
==
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Christian
Steve Holden wrote:

> ::
> one.py
> ::
> import os
> os.environ['STEVE'] = "You are the man"
> os.system("python two.py")
> print "Ran one"
> ::
> two.py
> ::
> import os
> print "STEVE is", os.environ['STEVE']
> print "Ran two"
> [EMAIL PROTECTED] tmp]$ python one.py
> STEVE is You are the man
> Ran two
> Ran one
> [EMAIL PROTECTED] tmp]$
> 
> Hope this helps.
> 
> regards
>  Steve

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.sh")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Steve Holden
Christian wrote:
> Steve Holden wrote:
> 
> 
>>::
>>one.py
>>::
>>import os
>>os.environ['STEVE'] = "You are the man"
>>os.system("python two.py")
>>print "Ran one"
>>::
>>two.py
>>::
>>import os
>>print "STEVE is", os.environ['STEVE']
>>print "Ran two"
>>[EMAIL PROTECTED] tmp]$ python one.py
>>STEVE is You are the man
>>Ran two
>>Ran one
>>[EMAIL PROTECTED] tmp]$
>>
>>Hope this helps.
>>
>>regards
>> Steve
> 
> 
> 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.sh")

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/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Christian
Steve Holden wrote:

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

Right again, Steve.

Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Grant Edwards
On 2005-10-21, Christian <[EMAIL PROTECTED]> wrote:
>> 
>> The closest thing you can do is that:
>> 
>> -myScript.py--
>> print 'export MY_VARIABLE=value'
>> --
>> 
>> -myScript.sh--
>> python myScript.py > /tmp/chgvars.sh
>> . /tmp/chgvars.sh
>> --

Bullshit.  Are people being intentionally misleading??

> 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)?

Good grief, that's ugly.  Just use os.putenv().

> That would be much more what is my basic problem.

And even Google knows the correct answer

http://www.google.com/search?hl=en&lr=&q=python+set+environment+variable

Follow the first hit.

-- 
Grant Edwards   grante Yow!  TONY RANDALL! Is YOUR
  at   life a PATIO of FUN??
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Mike Meyer
Grant Edwards <[EMAIL PROTECTED]> writes:

> On 2005-10-21, Christian <[EMAIL PROTECTED]> wrote:
>>> 
>>> The closest thing you can do is that:
>>> 
>>> -myScript.py--
>>> print 'export MY_VARIABLE=value'
>>> --
>>> 
>>> -myScript.sh--
>>> python myScript.py > /tmp/chgvars.sh
>>> . /tmp/chgvars.sh
>>> --
>
> Bullshit.  Are people being intentionally misleading??

No. Are you being intentionally - never mind.

> And even Google knows the correct answer
>
> http://www.google.com/search?hl=en&lr=&q=python+set+environment+variable
>
> Follow the first hit.

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.

http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Grant Edwards
On 2005-10-21, Mike Meyer <[EMAIL PROTECTED]> wrote:

 The closest thing you can do is that:
 
 -myScript.py--
 print 'export MY_VARIABLE=value'
 --
 
 -myScript.sh--
 python myScript.py > /tmp/chgvars.sh
 . /tmp/chgvars.sh
 --
>>
>> Bullshit.  Are people being intentionally misleading??
>
> No. Are you being intentionally - never mind.

Well, yes, probably.

>> And even Google knows the correct answer
>>
>> http://www.google.com/search?hl=en&lr=&q=python+set+environment+variable
>>
>> Follow the first hit.

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.

> Maybe the results order has changed since you looked?

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.comfor traditional mental
   attitudes!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Mike Meyer
Grant Edwards <[EMAIL PROTECTED]> writes:
> 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?

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

 http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-21 Thread Grant Edwards
On 2005-10-21, Mike Meyer <[EMAIL PROTECTED]> wrote:
> Grant Edwards <[EMAIL PROTECTED]> writes:
>> 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?
>
> 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."

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.comATLANTA, Georgia!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-24 Thread Eric Brunel
On Fri, 21 Oct 2005 20:13:32 -, Grant Edwards <[EMAIL PROTECTED]> wrote:

> On 2005-10-21, Mike Meyer <[EMAIL PROTECTED]> wrote:
>> Grant Edwards <[EMAIL PROTECTED]> writes:
>>> 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?
>>
>> 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."
>
> 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...

> so I thought it obvious that was what he was
> doing with the "export" command example.

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.zmz5(17;8(%,5.Z65\'*9--56l7+-'])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-24 Thread Grant Edwards
On 2005-10-24, Eric Brunel <[EMAIL PROTECTED]> 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.

-- 
Grant Edwards   grante Yow!  My face is new, my
  at   license is expired, and I'm
   visi.comunder a doctor's care
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-26 Thread Alex Martelli
Grant Edwards <[EMAIL PROTECTED]> wrote:

> On 2005-10-24, Eric Brunel <[EMAIL PROTECTED]> 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 "environment variable" yet lets
such variables be ``exported'' to anything but a child process.


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-26 Thread Jorgen Grahn
On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <[EMAIL PROTECTED]> wrote:
> Grant Edwards <[EMAIL PROTECTED]> wrote:
>
>> On 2005-10-24, Eric Brunel <[EMAIL PROTECTED]> 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 "environment variable" yet lets
> such variables be ``exported'' to anything but a child process.

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

/Jorgen

-- 
  // Jorgen GrahnR'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-26 Thread Mike Meyer
Jorgen Grahn <[EMAIL PROTECTED]> writes:
> On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <[EMAIL PROTECTED]> wrote:
>> Grant Edwards <[EMAIL PROTECTED]> wrote:
>>> On 2005-10-24, Eric Brunel <[EMAIL PROTECTED]> 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 "environment variable" yet lets
>> such variables be ``exported'' to anything but a child process.
> AmigaDOS, if I recall correctly. Its "ENV:" drive/namespace is global, and
> that's its closest thing to Unix environment variables.

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.

 http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-27 Thread Jorgen Grahn
On Wed, 26 Oct 2005 16:04:58 -0400, Mike Meyer <[EMAIL PROTECTED]> wrote:
> Jorgen Grahn <[EMAIL PROTECTED]> writes:
>> On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <[EMAIL PROTECTED]> wrote:
>>> Grant Edwards <[EMAIL PROTECTED]> wrote:
 On 2005-10-24, Eric Brunel <[EMAIL PROTECTED]> 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 "environment variable" yet lets
>>> such variables be ``exported'' to anything but a child process.
>> AmigaDOS, if I recall correctly. Its "ENV:" drive/namespace is global, and
>> that's its closest thing to Unix environment variables.
>
> AmigaDOS had both global environment variables (using the ENV: device)
> and local environment variables, that worked like the Unix
> version.

As I recalled it, the latter type was shell-local and not accessible to
normal processes ...

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

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

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

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

/Jorgen

-- 
  // Jorgen GrahnR'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list