Search code examples
pythonvirtualenvlaunchd

Finding modules when running python with virtualenvs, in launchd


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?


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>