简体   繁体   English

使用 Python 子进程运行 DRI_PRIME 命令

[英]Running DRI_PRIME Command By Using Python Subprocess

I need to run DRI_PRIME=1 glxinfo command by using Python subprocess also it should not cause shell injection risk.我需要使用 Python 子进程运行DRI_PRIME=1 glxinfo命令,它也不应该导致 shell 注入风险。

It gives the following error:它给出以下错误:

FileNotFoundError: [Errno 2] No such file or directory: 'DRI_PRIME=1'

Code:代码:

output = subprocess.check_output(["DRI_PRIME=1", "glxinfo"], shell=False).decode()
print(output)

System: Python3, OS: Linux

The problem is solved by using the following code:问题通过使用以下代码解决:

output = subprocess.check_output(["env", "DRI_PRIME=1", "glxinfo"], shell=False).decode()

A short explanation from manual page of env :来自env手册页的简短说明:

env - run a program in a modified environment

You are looking for how to run a command with a modified environment.您正在寻找如何在修改后的环境中运行命令。

import os
import subprocess

env = os.environ.copy()
env['DRI_PRIME'] = '1'
p = subprocess.run(['glxinfo'], env=env, capture_output=True)
print(p.stdout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM