Cody Jackson wrote
I'm working with an application that requires the user to source environment variables from a file into Bash. However, I cannot see these new env variables in Qt.
In Bash, once the file has been sourced, I can access the new variables via os.getenv(). But if I try to do that within Qt, it says those new variables don't exist, i.e. get a return value of "None".
Elsewhere in the Qt code, I can run a subprocess to execute Bash, call the source command, and gain access to the special commands from the external application, such as the code below ("ocpidev" is a command from the external application):
out = subprocess.run([
"bash",
"-c",
f"source {self.ocpi_path}/cdk/opencpi-setup.sh -r && "
f"cd {self.user_proj_path} && "
f"ocpidev create project {proj_name}"
], stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
If I try the same thing when trying to access the env vars:
out = subprocess.run([
"bash",
"-c",
f"source {self.ocpi_path}/cdk/opencpi-setup.sh -r && "
f"{os.getenv('OCPI_ROOT_DIR')}"
], stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
then Qt returns
bash: None: command not found
How can I access the sourced variables from Bash?
Never miss an update
Enjoyed this? Subscribe to get new updates straight in your Inbox.
Mike
There are some suggestions in this on how you can address this in different ways
- https://www.geeksforgeeks.org/python-os-environ-object/
- https://stackoverflow.com/questions/7040592/calling-the-source-command-from-subprocess-popen
- https://docs.python.org/3/library/subprocess.html
- https://stackoverflow.com/questions/3503719/emulating-bash-source-in-python
Cody Jackson
Interesting. I had found the GeeksforGeeks link previously, but the other ones provided, at first glance, appear to offer some new solutions more likely to help.
Thank you.
Mike
Funny I noticed this today when reviewing some code and maybe its more helpful. https://pypi.org/project/envbash/
relevant snippet
from envbash import load_envbash
def create_app():
app = DvmtApp(config=Config)
load_envbash(f'{os.environ.get("HOME")}/.bash_profile')
if os.environ.get('FERNET_KEY'):
app.decrypter(os.environ.get('FERNET_KEY'))
Cody Jackson
That's incredibly helpful. I just need to figure out why my Qt application doesn't seem to make the system Bash environment available. While the CLI shows the sourced env vars, Qt indicates that they aren't available.
But when I have more time, I can investigate. However, the envbash package you suggested does seem to provide more information.