2

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?

2 Answers 2

2

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))
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.