Documentation
- Installation
- Self Test
- Requirements
- Calling a Batch File from a Python Script
- Calling a Python Script from a Batch File
- Calling a Python Script from a Python Script
Installation
Unzip the pybat.py and pybat.bat scripts to the same directory. Set the %PYCMD% environment variable on your system to point to the python.exe interpreter, or leave %PYCMD% blank if Windows is setup it run *.py files from the command-line (it usually is).Self Test
Unzip the other files in the download to the directory with pybat.py and pybat.bat. Run pybat_test.bat to perform the self-test.Requirements
Both the pybat.bat and pybat.py need to be able to write to the directory that they are located in. Pybat uses 32-bit exit codes, so Windows NT or later is required. You must have the Python 2.4 or later installed.
Although PyBat does have measures to deal with concurrency issues, a multi-threaded Python script should not call a batch file through PyBat because any environment variable changes made by the script in between calling and returning from the pybat.callbat()
function will be overwritten by the environment variables in the batch file.
Calling a Batch File from a Python Script
In the Python script, you must import pybat and call the batch file with thecallbat()
function. Remember that if the batch file's filepath has spaces, it must be enclosed in double-quotes.
import pybat
pybat.callbat('foobar.bat arg1 arg2')
pybat.callbat('"C:\\Program Files\\barfoo.bat" arg1 arg2')
The return value of callbat() is the exit code of the batch file that was run.
Calling a Python Script from a Batch File
In the batch file, make a call to pybat.bat and pass the python script's filepath and arguments to it. Be sure to use double-quotes if the python script's filepath has spaces.call pybat.bat foobar.py arg1 arg2
call pybat.bat "c:\program files\foobar.py" arg1 arg2
The value of %ERRORLEVEL% is set to the exit code of the Python script.
Calling a Python Script from a Python Script
It is recommended to use Python'sexecfile()
function to run other scripts. However, if you must share environment variables and pass the command-line arguments, use Pybat's callpy()
function.Import the pybat module and call
callpy()
with the script name and command-line arguments.import pybat
pybat.callpy('foobar.py arg1 arg2')