I have generated an executable, say,’test(no extension since its Linux) using PyInstaller and stored it in a directory, say
data`.
I have a python program that is as below:
import subprocess
from pathlib import Path
...
def run_exe():
try:
# get current directory
currdir = Path.cwd()
datadir = currdir / "data"
#print(currdir)
#print(datadir)
process = subprocess.run(["./test"], shell=False, capture_output=True, text=True, cwd=datadir)
if(process.stderr):
print("stderr:", process.stderr)
return JSONResponse(content={"success": False}, status_code=400)
if(process.returncode == 0):
print(process.stdout)
return JSONResponse(content={"success": True})
except OSError as e:
print(e)
except subprocess.CalledProcessError as e:
print(e)
except Exception as e:
print(type(e).__name__) # Prints the name of the exception class
print(e) # Prints the exception message
I have tried the following:
-
Putting
subprocess.run(["ls"]
, it shows the correct directory and files includingtest
-
Putting
subprocess.run(["ls", -l"]
, it shows the execution permission intest
-
I tried moving to the parent directory and running.
-
I tried
subprocess.run(["test"] ...)
without the ./
With both the ls commands (2 and 3) it prints the output.
Any help will be greatly appreciated. Thanks