I want to do the same thing as "SET PATH" for Windows batch files from Python.

Asked 1 years ago, Updated 1 years ago, 404 views

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

2022-12-04 01:25

3 Answers

Generally speaking

    The
  • environment variable PATH indicates what to search for when starting the process.
  • Environment variables are taken over by child processes

From these two, you get two effects.

  • test.exe is searched in the environment variable PATH.
  • When
  • 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().


2022-12-04 05:02

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


2022-12-04 10:59

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

  • Environment variables exist for each process, and when subprocess is started, it takes over the parent process environment (new environment variable)
  • PATH lists the paths to search for programs when starting the program
  • So it doesn't make sense if the search list is a relative path

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'])


2022-12-04 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.