faqts : Computers : Programming : Languages : Python : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

49 of 55 people (89%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I set environment variables in a Python script that is to be run on an Apache server?
After setting environment variables in a script will they be changed after running the script?

May 27th, 2000 20:53
unknown unknown, Cliff Crawford


Problem:

Say, I want to set ORACLE_HOME = /opt/app/ora/

I wrote the following code:

        import os
        os.system("ORACLE_HOME=/opt/app/ora/")
        os.system("export ORACLE_HOME")
        ....

I executed the script under my user account but
when I checked my enviroment variables with "env"
there is no ORACLE_ENV added.



Solution:

That's because os.system spawns a new process (with a copy of the
original environment of the parent process) each time it's called.  You
want to use os.environ instead:

    os.environ["ORACLE_HOME"]="/opt/app/ora/"

This changes the environment for all future os.system calls.

Once the process dies, the changed environment dies with it.