3

I have written a script called coinview.py and it can run on linux. When I trying to run it as systemd, it raises error

error:ImportError: No module named 'schedule'.

I use pip3 show schedule, it already exist. So i have no idea what's wrong with my script.

I print sys.executable and sys.path in systemd.

[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: [email protected]
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

13
  • Have you tried pip install schedule on the command line?
    – Jan
    Apr 9, 2019 at 7:38
  • Can you show what sys.path has? Try raising a dummy exception with it's value just to show it in the service startup log. Apr 9, 2019 at 7:41
  • Sure, result is Requirement already satisfied.
    – Orz Han
    Apr 9, 2019 at 7:42
  • systemd probably uses the system-side python to run the script. You can test this by running something like import sys; print(sys.executable) at the top of the script, and saving the output produced by systemd
    – Arne
    Apr 9, 2019 at 7:43
  • In the same vein, you can add #!/usr/bin/python3 at the top of the file to suggest to any process that just tries to run the file as an executable that it should use python3 to run it. That shebang should point to the python interpreter that has the schedule module installed to its side-packages, which is probably the python3 at /usr/bin, but who knows.
    – Arne
    Apr 9, 2019 at 7:46

4 Answers 4

3

According to these logs, i found that the PYTHONPATH is different in manual shell and systemd.And i try to add "/home/ubuntu/.local/lib/python3.5/site-packages" into /etc/profile but systemd logs show that it still can't found the path.

So i do a stuip thing, add

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

in my code, and it works...

1

Install the package for root with

sudo pip install schedule

Or instead of running it as root, try running it as another specific user. Modify your .service to something like:

[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

Hope that helps!

3
  • I hope you used a specific username and not literally "User=user" .If so, is it the same error or different?
    – RKalra
    Apr 9, 2019 at 8:46
  • Maybe if you change it to ExecStart=/home/ubuntu/.local/bin/python3.5 coinview.py?
    – Arne
    Apr 9, 2019 at 12:17
  • just installed the package with root solved my problem. Thanks, mate
    – laughing
    Dec 31, 2020 at 11:22
0
#importing the required libraries
import schedule
import time
import pandas as pd
import mysql.connector as mysql
from datetime import timedelta
import datetime

Error:

ModuleNotFoundError                       Traceback (most recent call last)
Cell In[19], line 2
      1 # importing the required libraries
----> 2 import schedule
      3 import time
      4 import pandas as pd

ModuleNotFoundError: No module named 'schedule'
0

pip install schedule OR

pip3 install schedule

#3 is the version of the pip(Python Package Manager)

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Friedrich
    Sep 13 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.