python 子进程调用本地解释器而不是链接的解释器

问题描述 投票:0回答:1

我尝试使用 subprocess.run() 调用中指定的自己的解释器将 Python 脚本中的函数作为子进程执行。该解释器可能包含我的本地 Python 环境中不存在的包。当我调试该文件时,由于本地 python 中缺少这些库而不是命令中链接的库,因此发生了错误。我是不是误会了什么?

执行命令并捕获输出

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)
python python-3.x subprocess interpreter
1个回答
0
投票

使用

sys.executable
确定当前运行的解释器的路径。

© www.soinside.com 2019 - 2024. All rights reserved.