Batch files include:
I would like to run test.exe from Python without using this batch file.
run.bat
Use the command-line arguments when executing the exe file.
SETLOCAL
SET PATH = %PATH%;..\..\samples\external\opencv\bin;..\..\bin;
test.exe --view_mode=1
Here's what Python can tell you:
test.py
from os import path
import subprocess
#exe file execution part (I don't know if there's a way to call command line arguments.
exePath=path.join(path.dirname(__file__), 'test.exe')
subprocess.Popen([exePath, "--view_mode=1"])
I don't understand what SET PATH is doing, so I don't know how to describe it in Python.How can I do the same thing?
python windows batch-file
Generally speaking
PATH
indicates what to search for when starting the process.From these two, you get two effects.
test.exe
is searched in the environment variable PATH
.test.exe
starts the child process further, it is searched in the environment variable PATH
(unless test.exe
is explicitly excluded).The questionnaire didn't tell me which of these two effects to expect.
If you expect the former, if you know where test.exe
is located,
exePath=path.join(path.dirname(__file__), 'test.exe')
is sufficient.
If you expect the latter, you can pass it to the env
argument in subprocess.Popen()
.
In extreme cases, the test.exe
itself may not function properly if the environment variable PATH
is not set.Specifically, test.exe
can also search the environment variable PATH
to read the DLL and rely on it.You must also configure the env
argument for subprocess.Popen()
.
Assuming that the directory & file configuration is the same as the previous question I want to run .bat or .vbs files from Python, and the Python script and test.exe are in the same directory, you can apply these articles to:
How Python Subprocess Handles Environment Variables
Notes using Python's subprocess
from os import path
import subprocess
importos
savedir=os.getcwd()#### Save current directory information at startup and revert later if necessary.
#### Move the current directory to a directory with Python scripts and test.exe
os.chdir(os.path.dirname(os.path.abspath(__file__)))
#### Copy environment variables (equivalent to SETLOCAL) and add required software directories such as OpenCV to PATH
subenv=os.environ.copy()
subenv["PATH"] = subenv['PATH'] + r';..\..\samples\external\opencv\bin;..\..\bin;'
#exe file execution part (I don't know if there's a way to call command line arguments.
exePath=path.join(path.dirname(__file__), 'test.exe')
subprocess.Popen([exePath, "--view_mode=1", env=subenv)#### Specify the changed environment variable to startup parameters
Start the program directly
import subprocess
from pathlib import Path
def execpg():
for pin('../../samples/external/opencv/bin', '../../bin'):
prog=Path(p)/'test.exe'
ifprog.exists():
break
else:
print('Cannot find executable')
return
proc=subprocess.Popen ([prog, '--view_mode=1'])
execpg()
Python equivalent processing
If you need to process environment variables...
First of all, please look into the environment variable PATH roughly, and then do your own research
Note that PATH does not make sense when moving directories relative to the current directory
Setting environment variable PATH
(Includes current-facing actions)
(I use pathlib, but os.path
should be possible, as you like)
importos
from pathlib import Path
npath=os.pathsep.join(str(Path(p).resolve())
for pin('../../samples/external/opencv/bin', '../../bin')
print(os.environ ['PATH'])
os.environ ['PATH'] + = os.pathsep + npath
print(os.environ ['PATH'])
© 2025 OneMinuteCode. All rights reserved.