This could be a silly question. I have code that calls a subprocess in Python. For it to work and find the program I will need to set an environment variable on my Mac TEST__LIB_PATH.
subprocess.call(["find_info",
image,
json_file])
Is there a way in Python I can just import this environment variable to use instead of having to set this up globally?
call
takes a keyword argument env
that takes a mapping to use as the environment for the command. The current environment is in os.environ
; you can extend that with something like
subprocess.call(["find_info", image, json_file],
env=dict(TEST__LIB_PATH="/path/requried/for/test",
**os.environ))
You can access environment variables with os.environ
:
import os
print(os.environ['TEST__LIB_PATH'])
os.environ
also has a get()
method:
os.environ.get('TEST__LIB_PATH')
Edit : here's a link to the docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.