簡體   English   中英

ImportError:當我使用system運行服務時,沒有名為'schedule'的模塊

[英]ImportError: No module named 'schedule' when i using system to run a service

我編寫了一個名為coinview.py的腳本,它可以在linux上運行。 當我嘗試將其作為systemd運行時,會引發錯誤

error:ImportError: No module named '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

安裝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

希望有所幫助!

根據這些日志,我發現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") 

在我的代碼中,它的工作原理......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM