python - Set environment variable in one script and use this variable in another script -


i trying set environment variable using python. , variable used in script. code is:

#!/usr/bin/env python import os os.environ['var'] = '/current_working_directory' 

after executing above python script,i execute second script uses same variable 'var', not working. when export var='/current_working_directory , run second script, works fine. tried putenv() also.

this depends on how second python script get's called.

if have shell, , shell first runs first python script, second, won't work. reason first python script inherits environment shell. modifying os.environ[] or calling putenv() modify inherited environment --- 1 second python script, not 1 shell script.

if shell script runs second python script, again inherit environment shell ... , because shell script unmodified, cannot see modification first python script did.

one way achive goal using helper file:

#!/bin/bash rm -f envfile ./first_pythonscript test -f envfile && . envfile rm -f envfile ./second_pythonscript 

that code crude, won't work if 2 instances of shell script run, don't use as-is. hope idea.

even way make second_pythonscript not program, python module first_pythonscript can import. can make hybrid, library when imported, program when run via if __name__ == "__main__": construct.

and can use 1 of os function, e.g. os.spawnvpe


Comments

Popular posts from this blog

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -