fred basset
fred basset

Reputation: 10072

Python PyQt how to set environment variable for QProcess?

I'm trying to set an environment variable in a QT app for a QProcess that's being run. The code is below. The environment variable does not appear to be set however when inside the test. Any suggestions?

def runUbootTests(self):
    env = QtCore.QProcessEnvironment.systemEnvironment()
    env.insert("LINUX_ETH_ADDR", "3c:98:bf:00:00:f4")
    self.process.setProcessEnvironment(env)
    self.process.readyReadStandardOutput.connect(self.readReady)
    self.process.start("make", ("clean", "check_uboot"))

Upvotes: 1

Views: 4520

Answers (2)

ekhumoro
ekhumoro

Reputation: 120568

The code you posted does not seem obviously wrong, and works for me.

Here's my test files and output:

Makefile:

clean:  
    @echo 'SHELL:' $(SHELL)

check_uboot:    
    @echo 'ADDR:' $(LINUX_ETH_ADDR)

test.py:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.button = QtGui.QPushButton('Test', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)
        self.process = QtCore.QProcess(self)

    def handleButton(self):
        env = QtCore.QProcessEnvironment.systemEnvironment()
        env.insert("LINUX_ETH_ADDR", "3c:98:bf:00:00:f4")
        self.process.setProcessEnvironment(env)
        self.process.readyReadStandardOutput.connect(self.readReady)
        self.process.start("make", ("clean", "check_uboot"))

    def readReady(self):
        print str(self.process.readAllStandardOutput())

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

output:

$ python2 test.py
SHELL: /bin/sh

ADDR: 3c:98:bf:00:00:f4

Upvotes: 1

whardier
whardier

Reputation: 705

Have you tried using http://docs.python.org/library/os.html#os.environ? This modified the environment for the current process (as can be seen in /proc as well).

This new environment should be passed along to any spawned processes as well.

Upvotes: 1

Related Questions