473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Setting Environment Variables

Hello-

I am running python 2.3. on an HP-9000 box running Unix and have a POSIX
script that sets up my production environment. I would like to run the
script from inside a python routine and pick up the environment variables
but am having trouble getting it done. Suppose the script is called
setprod. How can I call this from inside a python script then pick up the
env's later in my program? I've tried os.system ('setprod') and
os.system('. setprod'), but neither seemed to work.

Thanks for your help,
--greg

Greg Lindstrom (501) 975-4859
NovaSys Health gr************@novasyshealth.com

"We are the music makers, and we are the dreamers of dreams" W.W.
Jul 18 '05 #1
3 2213
On 2004-10-20, Greg Lindstrom <gr************@novasyshealth.com> wrote:
I am running python 2.3. on an HP-9000 box running Unix and have a POSIX
script that sets up my production environment. I would like to run the
script from inside a python routine and pick up the environment variables
What do you mean by "pick up the environment variables"?

You do not have access to the environment of the child process, if that's
what you mean.
but am having trouble getting it done. Suppose the script is called
setprod. How can I call this from inside a python script then pick up the
env's later in my program?
You don't.
I've tried os.system ('setprod') and os.system('. setprod'), but neither
seemed to work.


That's right.

You're going to have to change the script to print the data of interest to
stdout or stderr, and read it via a pipe. The child process can't change
the environment of the parent, and the parent can't read the environment of
the child.

--
Grant Edwards grante Yow! If I pull this SWITCH
at I'll be RITA HAYWORTH!! Or
visi.com a SCIENTOLOGIST!
Jul 18 '05 #2
Greg Lindstrom wrote:
I am running python 2.3. on an HP-9000 box running Unix and have a POSIX
script that sets up my production environment. I would like to run the
script from inside a python routine and pick up the environment variables
but am having trouble getting it done. Suppose the script is called
setprod. How can I call this from inside a python script then pick up the
env's later in my program? I've tried os.system ('setprod') and
os.system('. setprod'), but neither seemed to work.


I'm not sure of the solution (there are several alternatives)
but the problem is that os.system() creates a new shell (or
subshell, if you will) that is exited when the script completes.
Obviously that means any changes made to the subshell's environment
will not appear in the environment of the process running the
Python script.

The answer depends on what you are trying to accomplish. Do
you need the environment variables to be used only in the Python
script, or are you trying to affect the environment even after
the Python script has completed?

(When people try to do this, it's sort of like trying to affect
the variables in the calling routine from inside a subroutine.
Although that's not usually a good idea, there are (ugly) ways
to work around the roadblocks, such as global variables...
in such a case, and perhaps yours, there may be other ways of
looking at the problem that can inspire more elegant solutions.)

-Peter
Jul 18 '05 #3
Greg Lindstrom <gr************@novasyshealth.com> wrote:
but am having trouble getting it done. Suppose the script is called
setprod. How can I call this from inside a python script then pick up the
env's later in my program? I've tried os.system ('setprod') and
os.system('. setprod'), but neither seemed to work.


try parsing the result of: os.popen('. setprod && env').readlines()

the env command emits the current environment to stdout, and the
os.popen() makes that environment available to you. If you want to know
exactly what setprod changed, you can compare the parsed result of that
with the still-current os.environ dictionary: the subprocess (in which
setprod necessarily runs) can't alter YOUR process's environment.
Alex
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rick Kasten | last post by:
Here's what I need to do: 1) Write "Password:" to the console - I know how to do that 2) Accept input that is not written back to the console - I know how to do that (Term::ReadKey) 3) Set the...
3
by: David Durkee | last post by:
Hi, I'm trying to write a script I can run from tcsh in Terminal (on Mac OS X) that will set environment variables that can be accessed by subsequent commands I execute in that session. Not...
6
by: shaan.shaan | last post by:
Hi I am trying to set an env. variable by exporting in unix environment on DB2. I am using export var_name=ON, but it is not taking the value of the var_name. This thing is working fine when I...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
8
by: Jack Vamvas | last post by:
I know that you can do : Request.ServerVariables("QUERY_STRING") but can you actually set a serverVariable? I know that sounds counter intuitive , but is it possible, via vbscipt/asp? -- ...
5
by: Henaro | last post by:
Hello~ I am having trouble setting environment variables in C++ on win32. The code that is not working is: char prxy; char pf_cmd1 = "set http_proxy="; ....
18
by: Tommy Nordgren | last post by:
Given the following: def outer(arg) avar = '' def inner1(arg2) # How can I set 'avar' here ? ------------------------------------- This sig is dedicated to the advancement of Nuclear Power...
2
by: Henry Hollenberg | last post by:
Hello, I have written a script that uses environment variables set during a particular users login in ".bash_profile" and ".profile". I have changed to that users uid and gid in my python...
2
by: bart.hernalsteen | last post by:
Hi, I want to read the envorinment setting "path" and then on certain conditions change this setting. ex path : "%SystemRoot%\system32;%SystemRoot%;%SystemRoot% \System32\Wbem;C:\Program...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.