I'm working with PHP and a Laravel framework, and I use .env to set my environment variables. I then call python scripts from my PHP environment, and return the result to PHP. My problem is, the default os.environ set in Python is being overridden by my PHP's environment.
My code (an example):
<?php
//Setting up the laravel environment and loading in the $_ENV variable
exec('python script.py');
and then my python:
print os.environ
print os.environ will return all of my environment variables set during the PHP environment setup. All of the default environment variables, such as PATH, are either not set or overridden by the new env.
It took me a while to diagnose the problem. However, with my limited knowledge of python, how to solve it is proving problematic. Is there a way I can reinitialize the python environment without affecting the PHP environment, and vice versa? Basically I need both processes to be separate from one another environment-wise, or I need to set the python environment, then update it with the old values before returning to PHP.
If anybody knows how to go about that you'd be a lifesaver.
The child processes inherit the environment variables from their parent processes on startup. No changes in the child process' environment are ever propagated to the parent process.
In your case, the python script is the child process; it inherits the environment from the php script; now you are free to do any changes to os.environ and these wouldn't affect the php process at all, for example prepend a new path in the beginning of the PATH environment variable
os.environ['PATH'] = '/home/foo/bar/bin:' + os.environ['PATH']
As for the PHP, the changes in $_ENV should not be inherited by child processes, only the ones explicitly set with putenv should be inherited by children.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With