'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.
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
Solution 1:[1]
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 - run a program in a modified environment
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pythonlearner9001 |
| Solution 2 | Sam Morris |
