Is it possible to access system env vars in Qt?

Heads up! You've already completed this tutorial.

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

python
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:

python
 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

python
bash: None: command not found

How can I access the sourced variables from Bash?


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

python
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.


Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Payhip , Gumroad , Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Is it possible to access system env vars in Qt? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.