Set Environmental Variables in Python with Popen -
i want set environmental variable in linux terminal through python script. seem able set environmental variables when using os.environ['blastdb'] = '/path/to/directory' .
however trying set variable subprocess.popen no success.
import subprocess import shlex cmd1 = 'export blastdb=/path/to/directory' args = shlex.split(cmd1) p = subprocess.popen(args, stdout=subprocess.pipe).communicate() why subprocess.popen fail set environmental variable blastdb '/path/to/directory'?
note: fails when using:
import os os.system('export blastdb=/path/to/directory')
use env parameter set environment variables subprocess:
proc = subprocess.popen(args, stdout=subprocess.pipe, env={'blastdb': '/path/to/directory'}) per the docs:
if env not none, must mapping defines environment variables new process; these used instead of inheriting current process’ environment, default behavior.
note: if specified, env must provide variables required programme execute. on windows, in order run side-by-side assembly specified env must include valid systemroot.
os.environ can used accessing current environment variables of python process. if scheme supports putenv, os.environ can used setting environment variables (and used instead of popen's env parameter shown above). however, oses such freebsd , macos, setting os.environ may cause memory leaks, setting os.environ not robust solution.
os.system('export blastdb=/path/to/directory') runs subprocess sets blastdb environment variable subprocess. since subprocess ends, has no effect on subsequent subprocess.popen calls.
python environment-variables subprocess
No comments:
Post a Comment