Is it possible to access system env vars in Qt?

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?

There are some suggestions in this on how you can address this in different ways

https://docs.python.org/3/library/subprocess.html

1 Like

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.

1 Like

Funny i noticed this today when reviewing some code and maybe its more helpful.

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'))

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.