ImportError:当我使用系统运行服务时,没有名为“日程表”的模块

时间:2019-04-09 07:36:18

标签: python systemd

我已经编写了一个名为format的脚本,它可以在 linux 上运行。当我尝试以coinview.py的身份运行它时,会引发错误

systemd

我使用error:ImportError: No module named 'schedule'.节目表,它已经存在。所以我不知道我的脚本有什么问题。

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

pip3
[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.

2 个答案:

答案 0 :(得分:0)

使用root安装软件包

sudo pip install schedule

或者代替以root用户身份运行,请尝试以其他特定用户身份运行。 将您的.service修改为:

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

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

[Install]
WantedBy=multi-user.target

希望有帮助!

答案 1 :(得分:0)

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

所以我做一个学习的东西,添加

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

在我的代码中,它可以正常工作...

相关问题