Global environment variables in macOS Catalina

Viewed 1429

I have created a global environment variable by adding a LaunchDaemon plist file as described in this article. I have some troubles though.

If I call:

  1. env QTBIN is not in the list.
  2. echo $QTBIN nothing is returned.
  3. /bin/launchctl getenv QTBIN The path is returned.

I am using this in a Python 3 script which is used on both Windows and macOS and are therefore accessing QTBIN with the command os.getenv("QTBIN"). However, this command returns None when executed on macOS. This makes me assume it builds upon command option 1 or 2 above.

Question How can I create an environment variable that is accessible to all programs and available in Python on both Windows and macOS? If possible, I would like to avoid having os specific code in the python script. I think the preferred solution will be to have QTBIN occur in the env list.


My plist file: /Library/LaunchDaemons/setenv.QTBIN.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
  <key>Label</key>
  <string>setenv.QTBIN</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/launchctl</string>
    <string>setenv</string>
    <string>QTBIN</string>
    <string>/usr/local/Cellar/qt/5.14.1/bin</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>ServiceIPC</key>
  <false/>
</dict>
</plist>

Command to load environment variable without restarting:

launchctl load -w /Library/LaunchDaemons/setenv.QTBIN.plist.

1 Answers

Your plist file and launchctl command are working as expected.

Unix processes inherit the environment from their parent at the time they are forked, so the new variable will not be available in your terminal until you close and reopen the terminal app.

Related