4

I've been trying to set up a script to run a python program at regular intervals using launchd. The python program fails because it can't find my imports---I know this because I caught the errors in a log file. To fix this problem, I created ~\.MacOSX\environment.plist, and duplicated my PYTHONPATH there, logged out and logged in again. This seems to be insufficient to solve the problem, and I'm at a loss as to what else to try.

I'm running OSX, 10.8.3.

Related threads:

UPDATE:

It appears that I can run the following command:

launchctl setenv PYTHONPATH $PYTHONPATH

and the script will execute successfully. So, to modify my question:

  1. Where does this get stored? I checked ~\.launchd.conf and \etc\.launchd.conf, neither existed.
  2. Presumably this setting is dumped when I reboot. Where can I change this information so that launchd will find it?
3
  • Are we exporting the PYTHONPATH? Commented Mar 20, 2013 at 23:27
  • What do you mean exporting? Where do I export it to? (PS: See the update.) Commented Mar 20, 2013 at 23:31
  • 1
    stackoverflow.com/questions/135688/… Commented Mar 20, 2013 at 23:32

2 Answers 2

7

To set the environment of a specific job you should use the EnvironmentVariables key in the job definition itself:

<?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>com.example.app</string>
    <key>Program</key>
    <string>/path/to/your/script</string>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PYTHONPATH</key>
        <string>/your/python/path</string>
    </dict>
</dict>
</plist>

You may define default environment variables for launchd(8) services by editing /etc/launchd.conf for daemons or /etc/launchd-user.conf for agents. The latter one works but is not documented. The currently documented (but unsupported) per-user config file is $HOME/.launchd.conf.

These config files contain a list of launchctl(1) subcommands. The one you want is:

setenv PYTHONPATH /your/python/path

Update: /etc/launchd.conf is not supported in Mac OS X 10.10 and higher. On these systems you'll have to define environment variables on a per-job-basis.

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't there a way to set it once, in one place, rather than having to tell every plist about it?
I've edited the answer to show how to globally define environment variables for launchd(8) jobs.
2

None of the above actually worked for me (OS X 10.11.3). The breakthrough was reading this script runs fine in terminal but not from launchd and realizing belatedly that one can write the absolute full path to the version of python with the right modules. D'oh.

<?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>com.example.app</string>
    <key>ProgramArguments</key>
    <string>/path/to/your/python</string>
    <string>/path/to/your/script</string>
</dict>
</plist>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.