Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for passing custom environment variables to subprocess #429

Open
stefan6419846 opened this issue May 23, 2022 · 3 comments
Open

Comments

@stefan6419846
Copy link
Contributor

stefan6419846 commented May 23, 2022

I would like to pass my own environment variables to the subprocess, for example to set OMP_THREAD_LIMIT. This custom value should be limited to a specific scope, as other parts of my program should not be affected by this.

Judging from the code, this is not possible for now.

@bozhodimitrov
Copy link
Collaborator

bozhodimitrov commented May 23, 2022

One way to workaround that is to set the environment beforehand, because if I recall correctly, forking inherits the env from the parent process.

Ofcourse, the downside is that you are setting the env vars once and it pollutes the current env forever until you change them again.

@stefan6419846
Copy link
Contributor Author

As you already mentioned, your approach would make this setting permanent for the whole program. I could write a context manager for temporarily changing the value, but this is quite an overhead.

For now, I settled on monkey patching the pytesseract environment, but this is just an ugly hack as well:

import copy
import os
from pytesseract import pytesseract

path = 'path/to/file.jpg'

pytesseract.environ = copy.deepcopy(os.environ)
pytesseract.environ['OMP_THREAD_LIMIT'] = '1'

print(pytesseract.image_to_data(path, output_type='data.frame'))

@bozhodimitrov
Copy link
Collaborator

bozhodimitrov commented May 24, 2022

I guess that you want to dynamically change the environment variables.
Because for standalone usage of pytesseract, you can just use OMP_THREAD_LIMIT=1 pytesseract ...

The python docs refer to os.environ.copy() as a good way to make environment copy of the mapping.
Then you can pass your specific modified version, but monkey patching pytesseract is still required with this method.

I am not sure, but maybe os.putenv() might work for your use case.
It looks less hacky, but also looks very implicit and the support for it varies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants