comalex3
comalex3

Reputation: 2606

How to convert os.system command to subprocess.Popen

I need run following comamnd in background on windows machine(windows 7)

os.system("SET TZ=GMT-5 && firefox")

Seems like I should use Popen, but command

subprocess.Popen("SET TZ=GMT-5;firefox", shell=True, stdin=None, stdout=None, stderr=None)

has no effect. Other variant os.spawnl causes errors since i use rpc.

Any idea how to run os.system("SET TZ=GMT-2 && firefox") in bg?

SOLUTION 1

Add start in front of *.exe command

os.system("SET TZ=GMT-5 && start firefox")

Upvotes: 0

Views: 173

Answers (1)

tripleee
tripleee

Reputation: 189937

As already hinted in comments, you can pass an env parameter to Popen and friends to modify the environment of the program you are starting.

I'm guessing you actually want subprocess.run() instead, but this obviously depends on what exactly you need to do with the process once you have started it.

subprocess.run(['firefox'], env={'TZ': 'GMT-5'})

As remarked in comments, you probably want to pass all of os.environ with your addition.

env = os.environ.copy()
env['TZ'] = 'GMT-5'
subprocess.run(['firefox'], env=env)

If your change is harmless, you might manipulate os.environ directly, of course.

Upvotes: 2

Related Questions