python subprocess calling the local interpreter instead of the linked one

36 Views Asked by At

I am attempting to execute a function within a Python script as a subprocess using its own interpreter, specified in the subprocess.run() call. This interpreter may include packages not present in my local Python environment. When I debug the file, errors occur due to these missing libraries in my local python instead of the one linked in the command. Am I misunderstanding something?

try:
    proc = subprocess.run(
        [
            r'C:\Users\user\AppData\Local\Programs\Python\Python311\python.exe',
            r'C:\subprocess_python\my_file_subprocess.py',
            str(self.x), str(self.y), str(self.z)
        ],
        capture_output=True,
        check=True
    )
except subprocess.CalledProcessError as proc_err:
    print("An exception occurred in the subprocess: \n ", proc_err)
    print("stdout : \n", proc_err.stdout.decode())
    print("stderr : \n", proc_err.stderr.decode())
    exit(1)
1

There are 1 best solutions below

2
Mikaelblomkvistsson On

Use sys.executable to determine what's the path of currently ran interpreter.