Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97030 > unrolled thread

Modify environment variable for subprocess

Started byloial <jldunn2000@gmail.com>
First post2015-09-23 02:51 -0700
Last post2015-09-24 03:12 +0300
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Modify environment variable for subprocess loial <jldunn2000@gmail.com> - 2015-09-23 02:51 -0700
    Re: Modify environment variable for subprocess Cameron Simpson <cs@zip.com.au> - 2015-09-23 20:00 +1000
    Re: Modify environment variable for subprocess Laura Creighton <lac@openend.se> - 2015-09-23 12:37 +0200
    Re: Modify environment variable for subprocess Akira Li <4kir4.1i@gmail.com> - 2015-09-24 03:12 +0300

#97030 — Modify environment variable for subprocess

Fromloial <jldunn2000@gmail.com>
Date2015-09-23 02:51 -0700
SubjectModify environment variable for subprocess
Message-ID<6f8c6233-9777-488f-9026-fec729fef6d1@googlegroups.com>
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?

[toc] | [next] | [standalone]


#97032

FromCameron Simpson <cs@zip.com.au>
Date2015-09-23 20:00 +1000
Message-ID<mailman.97.1443003590.28679.python-list@python.org>
In reply to#97030
On 23Sep2015 02:51, loial <jldunn2000@gmail.com> 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 <cs@zip.com.au>

Tachyon: A gluon that's not completely dry.

[toc] | [prev] | [next] | [standalone]


#97034

FromLaura Creighton <lac@openend.se>
Date2015-09-23 12:37 +0200
Message-ID<mailman.99.1443004647.28679.python-list@python.org>
In reply to#97030
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

[toc] | [prev] | [next] | [standalone]


#97054

FromAkira Li <4kir4.1i@gmail.com>
Date2015-09-24 03:12 +0300
Message-ID<mailman.112.1443053508.28679.python-list@python.org>
In reply to#97030
loial <jldunn2000@gmail.com> 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'))

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web