By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

Set an environment variable

P: n/a
Another question from a not even newbie:

In Unix you can set an environment variable with the command
export PYTHONPATH
but I would like to set the variable from at .py script.

So my question is:
How do I export an environment variable in a .py script?
Thanks

Chris
Oct 20 '05 #1
Share this Question
Share on Google+
28 Replies


P: n/a
Just use
os.system("export PYTHONPATH = %s" %("your_pythonpath"))

Oct 20 '05 #2

P: n/a
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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDV4sxJd01MZaTXX0RAj4aAJ41BlvbrZxLNrhm+Ea5bg MyZOkVwQCglsLw
wFB6iqRqw3FRznozCpV5qh0=
=MqJa
-----END PGP SIGNATURE-----

Oct 20 '05 #3

P: n/a
Thanks Jeff and the crazy 88.
Oct 20 '05 #4

P: n/a
On 20 Oct 2005 01:58:44 -0700, the_crazy88 <py****@gentoolinux.demon.nl> 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+-'])"
Oct 20 '05 #5

P: n/a
On 2005-10-20, the_crazy88 <py****@gentoolinux.demon.nl> 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
Oct 20 '05 #6

P: n/a
On 2005-10-20, Christian <ch*******@spam.not> wrote:
How do I export an environment variable in a .py script?


http://www.python.org/doc/current/li....html#l2h-1548

--
Grant Edwards grante Yow! My BIOLOGICAL ALARM
at CLOCK just went off... It
visi.com has noiseless DOZE FUNCTION
and full kitchen!!
Oct 20 '05 #7

P: n/a
"Eric Brunel" <er*********@despammed.com> 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)

<mike

--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 20 '05 #8

P: n/a
>
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
Oct 21 '05 #9

P: n/a
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 && ma*@alcyone.com && 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
Oct 21 '05 #10

P: n/a
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?
Oct 21 '05 #11

P: n/a
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
Oct 21 '05 #12

P: n/a
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"
[sholden@headrat tmp]$ python one.py
STEVE is You are the man
Ran two
Ran one
[sholden@headrat 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/

Oct 21 '05 #13

P: n/a
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 <http://cfaj.freeshell.org>
================================================== ================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
Oct 21 '05 #14

P: n/a
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"
[sholden@headrat tmp]$ python one.py
STEVE is You are the man
Ran two
Ran one
[sholden@headrat 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")
Oct 21 '05 #15

P: n/a
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"
[sholden@headrat tmp]$ python one.py
STEVE is You are the man
Ran two
Ran one
[sholden@headrat 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/

Oct 21 '05 #16

P: n/a
Steve Holden wrote:
Time you answered your own questions by trying things at the interactive
interpreter prompt!

regards
Steve


Right again, Steve.

Thanks
Oct 21 '05 #17

P: n/a
On 2005-10-21, Christian <ch*******@spam.not> 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&l...nment+variable

Follow the first hit.

--
Grant Edwards grante Yow! TONY RANDALL! Is YOUR
at life a PATIO of FUN??
visi.com
Oct 21 '05 #18

P: n/a
Grant Edwards <gr****@visi.com> writes:
On 2005-10-21, Christian <ch*******@spam.not> 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&l...nment+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.

<mike

--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 21 '05 #19

P: n/a
On 2005-10-21, Mike Meyer <mw*@mired.org> 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&l...nment+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.com for traditional mental
attitudes!
Oct 21 '05 #20

P: n/a
Grant Edwards <gr****@visi.com> 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."

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 21 '05 #21

P: n/a
On 2005-10-21, Mike Meyer <mw*@mired.org> wrote:
Grant Edwards <gr****@visi.com> 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.com ATLANTA, Georgia!!
Oct 21 '05 #22

P: n/a
On Fri, 21 Oct 2005 20:13:32 -0000, Grant Edwards <gr****@visi.com> wrote:
On 2005-10-21, Mike Meyer <mw*@mired.org> wrote:
Grant Edwards <gr****@visi.com> 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+-'])"
Oct 24 '05 #23

P: n/a
On 2005-10-24, Eric Brunel <er*********@despammed.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.

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

P: n/a
Grant Edwards <gr****@visi.com> wrote:
On 2005-10-24, Eric Brunel <er*********@despammed.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 "environment variable" yet lets
such variables be ``exported'' to anything but a child process.
Alex
Oct 26 '05 #25

P: n/a
On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <al*****@yahoo.com> wrote:
Grant Edwards <gr****@visi.com> wrote:
On 2005-10-24, Eric Brunel <er*********@despammed.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 "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 Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Oct 26 '05 #26

P: n/a
Jorgen Grahn <jg*********@algonet.se> writes:
On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <al*****@yahoo.com> wrote:
Grant Edwards <gr****@visi.com> wrote:
On 2005-10-24, Eric Brunel <er*********@despammed.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 "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.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 26 '05 #27

P: n/a
On Wed, 26 Oct 2005 16:04:58 -0400, Mike Meyer <mw*@mired.org> declaimed
the following in comp.lang.python:

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.
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
<mike -- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Oct 27 '05 #28

P: n/a
On Wed, 26 Oct 2005 16:04:58 -0400, Mike Meyer <mw*@mired.org> wrote:
Jorgen Grahn <jg*********@algonet.se> writes:
On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli <al*****@yahoo.com> wrote:
Grant Edwards <gr****@visi.com> wrote:
On 2005-10-24, Eric Brunel <er*********@despammed.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 "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 Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Oct 27 '05 #29

This discussion thread is closed

Replies have been disabled for this discussion.