简体   繁体   English

从 launchd 启动的 Python 脚本给出 ImportError: No module named foo

[英]Python script launched from launchd gives ImportError: No module named foo

When the python script iftttbot.py is run with launchd using iftttbot.plist (both included below) it gives the error:当 python 脚本 iftttbot.py 与 launchd 使用 iftttbot.plist (都包括在下面)一起运行时,它会给出错误:

Traceback (most recent call last):
  File "/usr/bin/iftttbot.py", line 1, in <module>
    from selenium import webdriver
ImportError: No module named selenium

Terminal also gives that error, but only if I explicitly use the full path of the python binary.终端也会给出该错误,但前提是我明确使用 python 二进制文件的完整路径。 Omitting the full path when calling the python binary results in the script running as expected.在调用 python 二进制文件时省略完整路径会导致脚本按预期运行。

iftttbot.plist sets the PATH variable to the the same as what my bash PATH variable is. iftttbot.plist 将 PATH 变量设置为与我的 bash PATH 变量相同。 Setting the PYTHONPATH variable to equal this too does not affect the behaviour.将 PYTHONPATH 变量设置为等于 this 也不会影响行为。

What is the proper way to run python scripts from launchd so that the modules are found?从 launchd 运行 python 脚本以便找到模块的正确方法是什么?

iftttbot.plist - installed as a user agent iftttbot.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>Disabled</key>
    <false/>
    <key>EnvironmentVariables</key>
    <dict>
        <key>HOME</key>
        <string>/users/scottmeup</string>
        <key>PATH</key>
        <string>/Users/scottmeup/.local/bin:/opt/local/bin:/opt/local/sbin:/Users/scottmeup/.nvm/versions/node/v15.8.0/bin:/Users/scottmeup/opt/anaconda3/bin:/Users/scottmeup/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string>
        <!--
        <key>PYTHONPATH</key>
        <string>/Users/scottmeup/.local/bin:/opt/local/bin:/opt/local/sbin:/Users/scottmeup/.nvm/versions/node/v15.8.0/bin:/Users/scottmeup/opt/anaconda3/bin:/Users/scottmeup/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string>
        -->
    </dict>
    <key>Label</key>
    <string>com.scottmeup.iftttbot</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/local/bin/bash</string>
        <string>-c</string>
        <string>exec python /usr/bin/iftttbot.py username password AppletID</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/ifttt_err.log</string>
    <key>StandardOutPath</key>
    <string>/tmp/ifttt.log</string>
    <key>StartInterval</key>
    <integer>60</integer>
</dict>
</plist>

iftttbot.py

#!/opt/local/bin/python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
import requests, threading,glob,sys
import bs4 as bs
from selenium.webdriver.common.action_chains import ActionChains
...
print("Invalid input, usage requires: Username Password AppletID")
...

removing #!/opt/local/bin/python does not prevent the error删除 #!/opt/local/bin/python 并不能防止错误

Some terminal testing showed that the module isn't found when the full path to the python binary is used, otherwise the script runs as expected:一些终端测试表明,当使用 python 二进制文件的完整路径时,找不到该模块,否则脚本会按预期运行:

$ which python
/opt/local/bin/python

$ /opt/local/bin/python iftttbot.py
Traceback (most recent call last):
  File "iftttbot.py", line 1, in <module>
    from selenium import webdriver
ImportError: No module named selenium

$ python iftttbot.py
Invalid input, usage requires: Username Password AppletID

$ echo $PATH
/Users/scottmeup/.local/bin:/opt/local/bin:/opt/local/sbin:/Users/scottmeup/.nvm/versions/node/v15.8.0/bin:/Users/scottmeup/opt/anaconda3/bin:/Users/scottmeup/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Python 3.7.10 Python 3.7.10

macOS 10.13.6 macOS 10.13.6

Changing the following line resolved the issue:更改以下行解决了该问题:

    <string>exec python /usr/bin/iftttbot.py username password AppletID</string>

    <string>exec python3 /usr/bin/iftttbot.py username password AppletID</string>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM