gpt4 book ai didi

python - 导入错误 : No module named 'schedule' when i using system to run a service

转载 作者:行者123 更新时间:2023-12-05 04:01:07 37 4
gpt4 key购买 nike

我写了一个名为coinview.py 的脚本,它可以在linux 上运行。当我尝试将它作为 systemd 运行时,它会引发错误

错误:ImportError:没有名为“schedule”的模块。

我用pip3 show schedule,它已经存在了。所以我不知道我的脚本有什么问题。

我在 systemd 中打印 sys.executable 和 sys.path。

[Unit]
Description=coinview deamon
After=rc-local.service

[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/home/ubuntu/source/quotation_api
ExecStart=/usr/bin/python3 coinview.py
Restart=always

[Install]
WantedBy=multi-user.target
ubuntu@ip-100-00-40-02:/etc/systemd/system$ pip3 show schedule
Name: schedule
Version: 0.6.0
Summary: Job scheduling for humans.
Home-page: https://github.com/dbader/schedule
Author: Daniel Bader
Author-email: mail@dbader.org
License: MIT
Location: /home/ubuntu/.local/lib/python3.5/site-packages
Requires:
Required-by:

Mar 27 08:40:10 ip-100-00-40-02 python3[8634]: Traceback (most recent call last):
Mar 27 08:40:10 ip-100-00-40-02 python3[8634]: File "coinview.py", line 3, in <module>
Mar 27 08:40:10 ip-100-00-40-02 python3[8634]: import requests,threading,time,schedule,json
Mar 27 08:40:10 ip-100-00-40-02 python3[8634]: ImportError: No module named 'schedule'
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: coinview.service: Main process exited, code=exited, status=1/FAILURE
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: coinview.service: Unit entered failed state.
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: coinview.service: Failed with result 'exit-code'.
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: coinview.service: Service hold-off time over, scheduling restart.
Mar 27 08:40:10 ip-100-00-40-02 systemd[1]: Stopped coinview deamon.
Apr 09 07:59:03 ip-100-00-40-02 python[12095]: /usr/bin/python3
Apr 09 07:59:03 ip-100-00-40-02 python[12095]: ['/home/ubuntu/source/quotation_api', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x8

最佳答案

根据这些日志,我发现 PYTHONPATH 在手动 shell 和 systemd 中是不同的。我尝试将“/home/ubuntu/.local/lib/python3.5/site-packages”添加到/etc/profile但是 systemd 日志显示它仍然找不到路径。

所以我做了一个 stuip 事情,添加

sys.path.append("/home/ubuntu/.local/lib/python3.5/site-packages") 

在我的代码中,它有效...

关于python - 导入错误 : No module named 'schedule' when i using system to run a service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55587418/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com