Question

This Q&A is similar to the one offered in Python Script not running in crontab calling pysaunter , but is put in terms of Launchd & Plists.

I was successfully running python scripts in a virtualenv, but when I tried to schedule it in Launchd, I received a traceback error to my plist error log file saying "ImportError: No module named sqlalchemy". What to do, what to do?

Was it helpful?

Solution

At first I tried adding the .virtualenv sitepackages absolute path to the path and python path variables in .bash_profile, and .profile. I also tried setting the environment variables path and pythonpath in launchd config. No joy.

What finally fixed it was to add into the plist the environment variables for PATH and PYTHONPATH. Which fixed it? Not sure, but having them both doesn't hurt anything. In these environment variables, I included the absolute path to sitepackages in .virtualenvs. Problem solved.

HTH.

Example:

<key>EnvironmentVariables</key>
    <dict>
    <key>PATH</key><string>/Users/user/.virtualenvs/oraenv/lib/python2.7/site-packages/</string>
    <key>PYTHONPATH</key><string>/Users/user/.virtualenvs/oraenv/lib/python2.7/site-package/</string>
    </dict>

OTHER TIPS

With pipenv, the above answer becomes simplified to the following config:

<key>ProgramArguments</key>
<array>
  <string>/usr/local/bin/pipenv</string>
  <string>run</string>
  <string>python</string>
  <string>python_script_in_working_directory.py</string>
</array>
<key>WorkingDirectory</key>
<string>/path/to/working/directory</string>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top