2017-12-30 30 views
0

sendgrid를 통해 링크가 포함 된 전자 메일을 보내는 python 스크립트를로드하는 launchd를 사용하여 일일 예약 된 plist 작업을 작성하려고합니다.Launchd plist 작업 파이썬 sys.path 오류

python dailyemail.py와 명령 줄에서 이메일 작품을 보낼 내 파이썬 스크립트 내가로드하고 지정된 시간에 실행 내 PLIST 작업을 시작하면

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()) 

(아래 참조)하지만,없는 때문에 실패 requests, bs4 and sendgrid 모듈을 가져올 수 있습니다. sys.path 출력 로깅에서 내 시스템이 launchd를 통해 작업을 실행할 때 대 명령 행에서 두 가지 미묘하게 다른 버전의 Python을로드하는 것 같습니다 (최종 및 plist 작업의 출력 참조).

  1. 어떻게이 격차를 해결 않습니다

    나는이 개 질문이? 또한 이 파일 경로가 다른 이유를 이해하는 데 관심이 있습니까?

  2. 로드 할 수있는 또 다른 방법은 파이썬 모듈을 작동시키기 위해 plist 작업에 참조할까요?

고마워요!

시스템 : OSX 엘 캐피 10.11.3

PLIST 작업

<?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> 

오류 로그를 확인 여기에 다른 사람이 그래서

*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 

답변

0

좋아, 100 % 이 대답을 향상시키기를 환영합니다.

-

는 동안 로컬 설치있어 의미, 파이썬의 당신의 명령 줄 스크립트 버전이 /usr/local/lib에서 unstalled 것 같다 제공하는 출력을 해석 한 후 관련 PythonDocs에 Python2.7에 대한 system.path에 최대 읽기 및 LaunchDaemon은 /System/Library/Frameworks/ 폴더에있는 시스템 기반 설치를 실행 중입니다.

내 직감으로 Homebrew를 통해 Python2.7을 설치했기 때문에 2 가지 다른 버전의 Python으로 끝날 수 있습니다. StackOverflow 전체에 대한 가능한 상하 측면에 많은 관련 질문/답변이 있습니다. Homebrew의 버전에 맞춰 모듈을 설치했지만 LaunchDaemon은 macOS가 미리 설치된 버전을 사용하고 있습니다.

나의 다음 직감 당신이 변경되면 있다는 것 파이썬에 Homebrew Docs 읽기 : 당신의 PLIST 작업에서

<string>/usr/bin/python</string>

에 :

<string>/usr/bin/python2</string>

당신은 아마 괜찮을 것이다.

아니요, 문제를 해결하는 데 여러 가지 방법이있을 수 있음을 알려주세요.

+0

감사합니다. @montmons - 올바른 길로 나를 안내했습니다. 솔루션은 내 plist에있는''' /usr/local/Cellar/python/2.7.10/bin/python2.7''' – AlexHandy1

+0

에 homebrew가 설치된 python bin 디렉토리에 직접 연결되는 것을 끝내었다. 그것이 듣고있어 기쁘다. – Montmons

관련 문제