0

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.

Lassadar
  • 1
  • 1
  • Are your PyPDF2 and reportlab installed globally, under virtualenv or in any custom location? – wanderlust Mar 13 '17 at 21:31
  • i installed them using pip install ... – Lassadar Mar 14 '17 at 05:27
  • Can you include following two lines `import sys` and `print sys.path` and compare output when you call you script manually and by trigger. It looks like these packages are installed into not-default location, and they cannot be found when trigger runs. – wanderlust Mar 14 '17 at 08:54
  • @wanderlust I edited the question above to add my results. thanks for your help. – Lassadar Mar 14 '17 at 18:22
  • Please post your solution as an answer, so people checked it if they will get into the same problem? Or you would like I'll do that? – wanderlust Mar 15 '17 at 06:55
  • @wanderlust I haven't found a solution yet. I just did what you asked me to do yesterday. Do you know what is wrong here? – Lassadar Mar 15 '17 at 10:39
  • Sorry, didn't noticed that. Use `sys.path.append("/usr/local/lib/python2.7/site-packages/PyPDF2/")` and `sys.path.append("/usr/local/lib/python2.7/site-packages/reportlab/")` in your script to add these modules, also check if these folders have read rights for your trigger. – wanderlust Mar 15 '17 at 11:09
  • @wanderlust I did that and also added permissions using "sudo chmod -R +rwxrwxrwx /usr/local/lib/python2.7/site-packages/PyPDF2" and "sudo chmod -R +rwxrwxrwx /usr/local/lib/python2.7/site-packages/reportlab" but the result didn't change – Lassadar Mar 15 '17 at 15:38
  • How about files inside the packages? Can your script read them as well? – wanderlust Mar 15 '17 at 17:43

0 Answers0