ImportError: No module named 'schedule' when i using system to run a service
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!
Related videos on Youtube
Orz Han
Updated on June 04, 2022Comments
-
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 assystemd
, it raises errorerror: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 about 5 yearsHave you tried
pip install schedule
on the command line? -
andreihondrari about 5 yearsCan 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 about 5 yearsSure, result is Requirement already satisfied.
-
Arne about 5 years
systemd
probably uses the system-sidepython
to run the script. You can test this by running something likeimport sys; print(sys.executable)
at the top of the script, and saving the output produced bysystemd
-
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 about 5 yearsIn 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 theschedule
module installed to its side-packages, which is probably thepython3
at/usr/bin
, but who knows. -
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 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 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 about 5 years@OrzHan well obviously that in the systemd you are using
python2.7
which probably doesn't have theschedule
package installed. -
Orz Han about 5 years@andreihondrari Oh, but how can i change it?
-
Orz Han about 5 yearsMy script have been written ExecStart=/usr/bin/python3 coinview.py, but systemd still execute my file with python2.7,why?
-
tripleee almost 3 yearsTangentially, check the correct spelling of daemon.
-
-
RKalra about 5 yearsI hope you used a specific username and not literally "User=user" .If so, is it the same error or different?
-
Arne about 5 yearsMaybe if you change it to
ExecStart=/home/ubuntu/.local/bin/python3.5 coinview.py
? -
laughing over 3 yearsjust installed the package with root solved my problem. Thanks, mate