I wrote a python script which works if I run it normally (python path/to/file.py). I need it to run automatically every few hours. So I created a plist file in /Users/User/Library/LaunchAgents which triggers a .sh file which eventually triggers the python script.
The python script fails to run without errors. I managed to narrow the problem down to the modules „PyPDF2“ and „reportlab“ (if I exclude those modules the script runs fine). The error message I get says "No module named reportlab.pdfgen" and "No module named PyPDF2".
The relevant parts of code are:
.sh script:
#!/bin/sh
python /Users/User/path/to/file.py
Python script:
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
import mechanize #pip install mechanize
import time
import os
from reportlab.pdfgen import canvas
from PyPDF2 import PdfFileWriter, PdfFileReader
Again, the script works if I run it myself. My guess is that launchctl runs as root and root somehow can't find/access the modules. I already did an extensive search but couldn't find anything.
I am relatively new to this and might be missing something obvious?
I run the python script on version 2.7 on a Mac.
What seems to be the problem here?
thanks for your help
update: If I put print(sys.path) in the code and run it myself as well as using the trigger I get a long list of paths both times. Some of them are the same but most are different. I also ran
print(help("modules"))
print(help("modules reportlab"))
print(help("modules PyPDF2"))
myself and using the trigger. While the modules show up if i run it myself, they don't if I use the trigger.
print(reportlab.__file__)
print(PyPDF2.__file__)
gives me "/usr/local/lib/python2.7/site-packages/reportlab/init.pyc" and "/usr/local/lib/python2.7/site-packages/PyPDF2/init.pyc". I tried running sys.path.append("/usr/local/lib/python2.7/site-packages/PyPDF2/init.pyc") sys.path.append("/usr/local/lib/python2.7/site-packages/reportlab/init.pyc") in the script while I being triggered but it doesn't seem to help.