HI All!
I need to have some help. I have a python function name is abc.py and variable is declared in abc.py, variable name is 'uname'. How can i call variable name 'uname' of abc.py in my batch file name testing.bat and get uname value in my batch testing.bat file?

Best Regards
Sarfraz

One solution would be putenv.
On linux:
abc.py:

import os

uname="Some uname value"
os.putenv("UNAME",uname)
os.system("./test.sh")

test.sh:

echo $UNAME

Other solutions:
- communication file
- os.environ
- database
- parsing .py file and getting out the variable from the batch file

The last version would be:
test.bat

python -m abc -c print uname

The last version would be:
test.bat

python -m abc -c print uname

Hi Slate
Thanks for help. i tried all 3 solutions but they are not working. i am using Windows 2003 as my OS and need to work on batch file and python script.
Do you have any more idea?

Best Regards
Sarfraz

Please include the error messages. The variable must of course be global variable in the python file. At least saving the value to text file to be read by the script should work.

Please include the error messages. The variable must of course be global variable in the python file. At least saving the value to text file to be read by the script should work.

HI Tonyjv
I write in abc.py

import os
pname="fk12345"
os.putenv("FK_DBO_PASS",pname)
os.system("test.bat")

And then i write in my test.bat BATCH file

@echo off
python setev.py
echo $FK_DBO_PASS

And it gives me this result
C:\Python26>test.
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS
$FK_DBO_PASS

c:\python26>

My goal is to set environment variable in python function. but os.environ is not working for this purpose. so i want to get value of variable in python function into my batch file and then i will use that value to set environment variable in batch file.
Can you please guide more on it. Thanks
Regards
Sarfraz

My recommendations:

Try write a bat file, that can display any environment variable...
Then write a bat file, that runs a predefined python script. Not just any.
Read about recursion, and independent thinking.....

Okay, it has been 7 years, but for anyone trying to do this in 2019 or later, here's how I managed to do it in Windows.

First, make a python file like Slate has already shown:

import os

var1 = "Hello, world!"
os.putenv("VAR1", var1) #This takes the variable from python and makes it a batch variable

Second, make your batch file, by going to the folder where you want your python program to work, then right-clicking in the map, then create new text file. In this text file, write whatever you want to do with the variable and make sure you call your variable using %...% like so: echo %VAR1%
Save this file as a batch file like so: file>save as>name_of_file.bat then select: save as file: all files.

Then to call your batch file in python, write:

os.system("name_of_file.bat")

Make sure all these files are in the same map for them to work!
There you go, this worked for me, hopefully I can help some people with this comment, because I searched for so long to find how this works.

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.