473,385 Members | 1,531 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,385 software developers and data experts.

Activating Batch Files from Python

I'm writing a launcher that should do the following:

1. Activate a .bat file to set environmental variables.
2. Start 3 programs, using said environmental variables as arguments.

However, I can't get the environmental variables to stick because all
of Pythons' system start/open functions split off into their own little
subshells, therefore the .bat file doesn't affect the main shell.

How can I use the .bat file to set environmental vars from Python?

Apr 19 '06 #1
9 6094
Jeff Groves wrote:
I'm writing a launcher that should do the following:

1. Activate a .bat file to set environmental variables.
2. Start 3 programs, using said environmental variables as arguments.

However, I can't get the environmental variables to stick because all
of Pythons' system start/open functions split off into their own little
subshells, therefore the .bat file doesn't affect the main shell.
That's right. That is how enviromental variables work regardless of
programming language.

How can I use the .bat file to set environmental vars from Python?


If it is your bat file, forget about it. Set the variables in your
Python program using os.environ. If it is a 3rd party bat file, the
best you can do is to copy it, add (at the end) a command to dump all
enviromental variables to a temp file like this: set >dump_file.txt,
parse the dump file to see what variables changed and set the variables
using os.environ.

Apr 19 '06 #2
In article <11**********************@g10g2000cwb.googlegroups .com>,
"Jeff Groves" <mi*********@yahoo.com> wrote:
How can I use the .bat file to set environmental vars from Python?


How about sourcing it from a shell, then using that same shell instance
to run the programs?
Apr 19 '06 #3
>How about sourcing it from a shell, then using that same shell instance
to run the programs?


How would I do that? As I've said, I haven't found a Python command
that lets you send multiple commands to the same shell yet. If I could,
my problem would be solved.

Apr 19 '06 #4
On 2006-04-19, Jeff Groves <mi*********@yahoo.com> wrote:
I'm writing a launcher that should do the following:

1. Activate a .bat file to set environmental variables.
2. Start 3 programs, using said environmental variables as arguments.

However, I can't get the environmental variables to stick because all
of Pythons' system start/open functions split off into their own little
subshells, therefore the .bat file doesn't affect the main shell.

How can I use the .bat file to set environmental vars from Python?


You can try launching a single shell with subprocess.Popen, and run
everything inside that:

from subprocess import *

p = Popen("cmd", stdin = PIPE, stdout = PIPE)

output, err = p.communicate("""
vars.bat
prog1.exe
prog2.exe
prog3.exe
""")

print output

I can't test this because I don't have a Windows system.

Otherwise, as others have suggested, replace vars.bat with modifications
to os.environ.
Apr 19 '06 #5
On 2006-04-19, Jeff Groves <mi*********@yahoo.com> wrote:
How about sourcing it from a shell, then using that same shell instance
to run the programs?


How would I do that? As I've said, I haven't found a Python command
that lets you send multiple commands to the same shell yet. If I could,
my problem would be solved.


If I understood correctly I think the idea is to run vars.bat first,
then run the Python program in _that_ shell (so it inherits the
environment) and then launch the other programs from the Python program.

i.e.

C:\> vars.bat
C:\> python launcher.py

You could put those commands in a batch file. So the outermost thing you
run is a batch file, which starts Python, which starts the other
programs.

Seems quite a neat solution.
Apr 19 '06 #6
On 4/18/2006 11:39 PM, Jeff Groves wrote:
I'm writing a launcher that should do the following:

1. Activate a .bat file to set environmental variables.
2. Start 3 programs, using said environmental variables as arguments.

However, I can't get the environmental variables to stick because all
of Pythons' system start/open functions split off into their own little
subshells, therefore the .bat file doesn't affect the main shell.

How can I use the .bat file to set environmental vars from Python?


Resource Kit has SETX, but better is SETENV by Vincent Fatica
http://barnyard.syr.edu/~vefatica/

C:\_Utils>setenv /?
SETENV syntax:

To set or change the value of a variable:

User environment: setenv -u name value (also /u)
Machine environment: setenv -m name value (also /m)
Default user environment: setenv -d name value (also /d)
Volatile environment: setenv -v name value (also /v)

To display a variable: setenv -u|-m|-d|-v name

To delete a variable: setenv -u|-m|-d|-v name -delete (also
/delete)

To display an environment: setenv -u|-m|-d|-v

Use double-quotes around values containing spaces.

If a variable name or value is to CONTAIN a double-quote,
escape that double-quote as \"

Return codes: 0 = success 1 = variable not found
2 = access denied 3 = other error
4 = SETENV has shown this syntax message

Requested output goes to STDOUT; help and error messages to STDERR.
Apr 20 '06 #7
"Jeff Groves" <mi*********@yahoo.com> wrote:

I'm writing a launcher that should do the following:

1. Activate a .bat file to set environmental variables.
2. Start 3 programs, using said environmental variables as arguments.

However, I can't get the environmental variables to stick because all
of Pythons' system start/open functions split off into their own little
subshells, therefore the .bat file doesn't affect the main shell.

How can I use the .bat file to set environmental vars from Python?


One very real possibility is to parse your .bat files by hand, and make the
changes to your os.environ. Your subprocesses inherit that.

..BAT files are easy to parse, especially if they're just a bunch of 'set'
statements.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Apr 21 '06 #8

Jeff Groves wrote:
How would I do that? As I've said, I haven't found a Python command
that lets you send multiple commands to the same shell yet. If I could,
my problem would be solved.


any reason why you cannot create a temp .bat, consisting of:

setvar.bat
prog1.exe
prog2.exe

and then execute it form perl?

Apr 21 '06 #9
In article <11**********************@i39g2000cwa.googlegroups .com>,
"Jeff Groves" <mi*********@yahoo.com> wrote:
How about sourcing it from a shell, then using that same shell instance
to run the programs?


How would I do that? As I've said, I haven't found a Python command
that lets you send multiple commands to the same shell yet. If I could,
my problem would be solved.


What about os.popen? Otherwise there's the usual fork/exec thing.
Apr 21 '06 #10

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

Similar topics

12
by: Moosebumps | last post by:
So, after reading some messages about os.system, and looking at the popen stuff and trying it a bit, I still have not found a way to keep a command window open for several commands (on Windows...
1
by: Bernhard Sollfrank | last post by:
Hello, i have to start a batch process from a dll to transfer files created by this dll to remote ftp server. I created a batch file: ftp -n -v -i -s:D:\batch\ftpcmds.txt ftp-server and a...
7
by: erniedude | last post by:
Hi, I'm a newbie and I was wondering if anyone knew a (Python) script to run 4 batch files, one after the other (assuming the directories are known). It would be better if all 4 batch files...
0
by: Steve Jorgensen | last post by:
I remember that I used to set up utility batch files, and create Windows shortcuts to them that would ask the user for parameters to supply to the batch files. From what I can tell, this...
6
by: Charles Neitzel | last post by:
I'm trying to write a windows application in C# (Using Microsoft Visual C# 2005 Express) that is nothing more than a simple UI with buttons on it. The buttons do various things like running...
1
by: Charles | last post by:
I'm trying to write a windows application in C# (Using Microsoft Visual C# 2005 Express) that is nothing more than a simple UI with buttons on it. The buttons do various things like running...
10
by: rtilley | last post by:
Hope it's not inappropriate to post this here. Could someone critique my code? I have no Python programmers in my office to show this to. The script works OK, but should I do it differently? I...
1
by: SPE - Stani's Python Editor | last post by:
Phatch is a simple to use cross-platform GUI Photo Batch Processor Phatch handles all popular image formats and can duplicate (sub)folder hierarchies. It can batch resize, rotate, apply...
0
by: Chris Rebert | last post by:
On Mon, Sep 15, 2008 at 10:30 AM, aditya shukla <adityashukla1983@gmail.comwrote: Unless your Python scripts are essentially just *really basic* shell scripts that happen to be written in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.