Re: Modify environment variable for subprocess

2015-09-23 Thread Cameron Simpson

On 23Sep2015 02:51, loial  wrote:

I need to modify the LIBPATH environment variable when running a process via 
subprocess, but otherwise retain the existing environment.

Whats the best way to do that?


Make a copy of os.environ, modify the copy, pass it via the env=parameter of 
subprocess.Popen. That is the most direct and controllable method.


Cheers,
Cameron Simpson 

Tachyon: A gluon that's not completely dry.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Modify environment variable for subprocess

2015-09-23 Thread Laura Creighton
In a message of Wed, 23 Sep 2015 02:51:53 -0700, loial writes:
>I need to modify the LIBPATH environment variable when running a process via 
>subprocess, but otherwise retain the existing environment.
>
>Whats the best way to do that?

import subprocess, os
my_env = os.environ  # if your program should be able to modify the current env
# otherwise
my_env = os.environ.copy() # if it shouldn't

# if you just want to add something to the existing LIBPATH
my_env["LIBPATH"] = "/where/I/want/to/look/first:" + my_env["LIBPATH"]
# otherwise
my_env["LIBPATH"] = "/what/I/want"

subprocess.Popen(my_program, env=my_env)

Setting os.environ leaks memory under Mac OS and FreeBSD.
I am not sure if this means that if  you do this a gazillion times
on a Mac you will have a problem.

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Modify environment variable for subprocess

2015-09-23 Thread loial
I need to modify the LIBPATH environment variable when running a process via 
subprocess, but otherwise retain the existing environment.

Whats the best way to do that?

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Modify environment variable for subprocess

2015-09-23 Thread Akira Li
loial  writes:

> I need to modify the LIBPATH environment variable when running a
> process via subprocess, but otherwise retain the existing environment.
>
> Whats the best way to do that?

Pass env=dict(os.environ, LIBPATH=value) parameter:

  import os
  import subprocess 
  
  subprocess.check_call('echo $LIBPATH', shell=True,
env=dict(os.environ, LIBPATH='/some/path'))


-- 
https://mail.python.org/mailman/listinfo/python-list