subprocess.Popen and the env argument

Today I was looking at launching programs with some custom environment variables set and if you don’t want to redefine a whole lot of them for this new environment I’d recommend is copying the os.environ dictionary and do your changes on the copy, like so:

new_env = os.environ.copy()
new_env['MEGAVARIABLE'] = 'MEGAVALUE'
subprocess.Popen('path', env=new_env)

Then you should have your program launched with all them fancy environment variables set.