Ich versuche eine tägliche geplante Plist-Aufgabe mit Launchd zu erstellen, die ein Python-Skript lädt, das mir eine E-Mail sendet, die einen Link über sendgrid enthält.Launched Plist Aufgabe python sys.path Fehler
Mein Python-Skript, um die E-Mail-Werke von der Kommandozeile mit python dailyemail.py
(siehe unten)
import os, requests, bs4, sendgrid
from sendgrid.helpers.mail import *
url = 'https://apod.nasa.gov/apod/astropix.html'
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("xxx")
to_email = Email("xxx")
subject = "Astronomy Picture of the Day"
content = Content("text/plain", 'https://apod.nasa.gov/apod/astropix.html')
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
Wenn ich laden und starten Sie meine plist Aufgabe es läuft zu der angegebenen Zeit zu senden, aber nicht aufgrund nicht sein in der Lage, die requests, bs4 and sendgrid
Module zu importieren. Aus der Protokollierung der sys.path
Ausgänge, habe ich festgestellt, dass mein System scheint, zwei subtil verschiedene Versionen von Python von der Befehlszeile vs geladen, wenn ich die Aufgabe über launchd ausführen (siehe Ausgabe am Ende und Plist Task).
Ich habe zwei Fragen:
- Wie kann ich diese Ungleichheit beheben? Interessiert auch zu verstehen, warum diese Dateipfade anders wären?
- Gibt es eine andere Möglichkeit, Python-Module in eine Plist-Task zu laden/zu referenzieren, damit sie funktioniert?
Dank!
System: OSX El Capitan 10.11.3
Plist Aufgabe
<?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>
<!-- The label should be the same as the filename without the extension -->
<string>com.alexanderhandy.nasa</string>
<!-- Specify how to run your program here -->
<key>ProgramArguments</key>
<array>
<string>/usr/bin/python</string>
<string>/Users/alexanderhandy/Documents/Programming/Scripts/dailyemail.py</string>
</array>
<!-- Run every dat -->
<key>StandardErrorPath</key>
<string>/tmp/ahnasa.err</string>
<key>StandardOutPath</key>
<string>/tmp/ahnasa.out</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>12</integer>
<key>Minute</key>
<integer>34</integer>
</dict>
</dict>
</plist>
Fehlerprotokolle
*Command line python sys.path*
/Users/alexanderhandy/Documents/Programming/Scripts/usr/local/lib/python2.7/site-packages/setuptools-17.0-py2.7.egg/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python27.zip/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/usr/local/lib/python2.7/site-packages/Library/Python/2.7/site-packages
*plist task python sys.path*
/Users/alexanderhandy/Documents/Programming/Scripts/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/Library/Python/2.7/site-packages
Dank @montmons - führte mich auf den richtigen Weg. Die Lösung endete damit, direkt mit dem Homebrew-Python-Bin-Verzeichnis in meinem Plist zu verbinden '' ' /usr/local/Cellar/python/2.7.10/bin/python2.7 ' '' –
AlexHandy1
Großartig! Freut mich zu hören, dass es gelöst ist. – Montmons