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

16,755

Solution 1

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

Solution 2

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!

Share:
16,755

Related videos on Youtube

Orz Han
Author by

Orz Han

Updated on June 04, 2022

Comments

  • Orz Han
    Orz Han almost 2 years

    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
    
    
    • Jan
      Jan about 5 years
      Have you tried pip install schedule on the command line?
    • andreihondrari
      andreihondrari about 5 years
      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.
    • Orz Han
      Orz Han about 5 years
      Sure, result is Requirement already satisfied.
    • Arne
      Arne about 5 years
      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
    • Orz Han
      Orz Han about 5 years
      >>> import sys >>> sys.path ['', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/home/ubuntu/.local/lib/python3.5/site-packages', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
    • Arne
      Arne about 5 years
      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.
    • andreihondrari
      andreihondrari about 5 years
      @OrzHan it is highly important that you print the sys.path from the service not from a manual shell. The reason is because I'm guessing your environment in the service might be different than the one you expect.
    • Orz Han
      Orz Han about 5 years
      @Arne print(sys.executable) is /usr/bin/python , and i add #!/usr/bin/python3 at the top of my file, but it doesn't work
    • Orz Han
      Orz Han about 5 years
      @andreihondrari i print sys.path as systemd, ['/home/ubuntu/source/quotation_api', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/di
    • andreihondrari
      andreihondrari about 5 years
      @OrzHan well obviously that in the systemd you are using python2.7 which probably doesn't have the schedule package installed.
    • Orz Han
      Orz Han about 5 years
      @andreihondrari Oh, but how can i change it?
    • Orz Han
      Orz Han about 5 years
      My script have been written ExecStart=/usr/bin/python3 coinview.py, but systemd still execute my file with python2.7,why?
    • tripleee
      tripleee almost 3 years
      Tangentially, check the correct spelling of daemon.
  • RKalra
    RKalra about 5 years
    I hope you used a specific username and not literally "User=user" .If so, is it the same error or different?
  • Arne
    Arne about 5 years
    Maybe if you change it to ExecStart=/home/ubuntu/.local/bin/python3.5 coinview.py?
  • laughing
    laughing over 3 years
    just installed the package with root solved my problem. Thanks, mate