启动plist任务python sys.path错误

问题描述 投票:0回答:1

我正在尝试使用launchd构建每日计划的plist任务,该任务加载一个python脚本,该脚本通过sendgrid向我发送包含链接的电子邮件。

发送电子邮件的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输出,我发现我的系统似乎从命令行加载两个微妙不同的Python版本,而不是通过launchd运行任务(参见end和plist任务的输出)。

我有两个问题:

  1. 我该如何解决这种差异?还有兴趣了解为什么这些文件路径会有所不同?
  2. 是否有另一种方法可以将python模块加载/引用到plist任务中以使其正常工作?

谢谢!

系统:OSX El Capitan 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
python macos python-2.7 plist launchd
1个回答
0
投票

好的,不是100%肯定在这里,所以欢迎其他人改进这个答案。

-

在相关的PythonDocs上读取Python2.7的system.path,然后解释它给出的输出似乎你的命令行脚本版本的Python在/usr/local/lib下没有安装,这意味着它是本地安装,而LaunchDaemon运行的是基于系统的安装。 /System/Library/Frameworks/文件夹。

我的预感是你可能已经通过Homebrew安装了Python2.7,导致你最终得到2个不同版本的Python。关于StackOverflow的可能的上行和下行,有许多相关的问题/答案。您可能已经根据Homebrew的版本安装了模块,但LaunchDaemon使用的是macOS预安装版本。

在Python上阅读Homebrew Docs我的下一个预感是,如果你改变:

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

在你的plist任务中:

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

你可能应该没问题。

如果没有,请告诉我原因可能有多种方法可以解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.