2017-12-30 47 views
0

私はsendgrid経由のリンクを含む電子メールを私に送信するpythonスクリプトを読み込むlaunchdを使用して、毎日のスケジュールされたplistタスクを構築しようとしています。Launchd plistタスクのpython sys.pathエラー

電子メールを送信するために私のPythonスクリプトはpython dailyemail.pyでコマンドラインから動作します(下記参照)

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

私はロードして起動すると、私のplistタスクは、それが指定した時刻に実行されますが、原因されていないために失敗しましたrequests, bs4 and sendgridモジュールをインポートできます。 sys.pathの出力をログに記録すると、システムがlaunchd経由でタスクを実行したときと、コマンドラインからPythonの2つの微妙に異なるバージョンをロードしているように見えることがわかりました(endおよびplistタスクの出力を参照)。

  1. どのように私はこの格差を解決します:

    私は2つの質問がありますか?また、なぜ これらのファイルパスが異なるのか理解することに興味がありますか?

  2. pythonモジュールを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

[OK]を、ではない100%この答えを改善することを歓迎しました。

-

関連PythonDocsにPython2.7用system.path上に読み込み、その後、それがローカルインストール中だという意味、Pythonのあなたのコマンドラインスクリプト・バージョンは、/usr/local/lib下unstalledているようです与え出力の解釈LaunchDaemonは、/System/Library/Frameworks/フォルダ内にあるシステムベースのインストールを実行しています。

私の勘違いは、Homebrewを使ってPython2.7をインストールした可能性があり、2つの異なるバージョンのPythonで終了する可能性があります。 StackOverflow全体に渡って、これに関連する疑問や答えがたくさんあります。 Homebrewのバージョンに対してモジュールをインストールしたことがありますが、LaunchDaemonはmacOSがプリインストールされたバージョンを使用しています。

私の次の直感は、あなたが変更した場合ということでしょうPythonの上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

関連する問題