HI All!
I am creating a Python function that should set environment variable. My simple code is

#!/usr/bin/python
import os
os.environ="12345"
print "After setting Environment Variable is ",os.environ

When i execute this code file in command prompt it is giving me this result

After setting Environment Variable is 12345

But when i run this command in command prompt SET FK_DBO_PASS
The result is
Environment variable FK_DBO_PASS not defined

My code is not setting environment variable .Can some one help me how i can fix this code? and how i can set temporary environment variable through python code just for my existing process?
Waiting for someone help. Thanks

Best Regards
Sarfraz

Recommended Answers

All 5 Replies

Have you tried
os.putenv(key, value)
?

Have you tried
os.putenv(key, value)
?

HI
Yes i tried now . i write
os.putenv('FK_DBO_PASS','12345') and also i tried os.putenv(FK_DBO_PASS,12345)
in 1ast case it is giving error that FK_DBO_PASS is not defined and also for 12345. And for 1st case it is not setting.
Am i doing right? do you have more suggestions?

Best Regards
Sarfraz

I just did the following interactively and as you can see, had no problems:

>>> os.putenv('FK_DBO_PASS', '12345')
>>> han = os.popen("SET FK") # Handle to output of subprocess SET FK command
>>> for line in han: print line
...
FK_DBO_PASS=12345

>>>han.close()

Try this and be sure it works for you.

I trust you realize setting environment variables can only be done for the current process level and below. You can't set an environment variable for a higher level process. That's not Python, it's the O/S.

I just did the following interactively and as you can see, had no problems:

>>> os.putenv('FK_DBO_PASS', '12345')
>>> han = os.popen("SET FK") # Handle to output of subprocess SET FK command
>>> for line in han: print line
...
FK_DBO_PASS=12345

>>>han.close()

Try this and be sure it works for you.

I trust you realize setting environment variables can only be done for the current process level and below. You can't set an environment variable for a higher level process. That's not Python, it's the O/S.

Hi BearofNH
I did and it works for current process.
But i want to do it for Higher level process , on O/S level for temporary.
do you have any idea how i can do it?
or how i can get value of a python variable in other batch file , then i will use that value and will set environment variable in batch?
Regards
SA

But i want to do it for Higher level process , on O/S level for temporary.

That of course is the problem, it just can't be done. I'm sure the O/S folks will tell you they don't allow it because it's a security issue, or possible synchronization issues or any of a dozen other valid reasons.

The good news is you're nowhere near the first person to need this capability. I usually just punt the issue and write my parameters to a file. Sometimes you can get away with that, sometimes that's not a practical option. You might ask in other forums specific to your O/S. Since it's not really a Python issue you may not get needed attention in this forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.