gpt4 book ai didi

ImportError: No module named 'schedule' when i using system to run a service(ImportError:当我使用系统运行服务时,没有名为‘Schedule’的模块)

转载 作者:bug小助手 更新时间:2023-10-25 13:36:56 30 4
gpt4 key购买 nike



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

我已经编写了一个名为coinview.py的脚本,它可以在Linux上运行。当我尝试以SYSTEM D身份运行它时,出现错误



error:ImportError: No module named 'schedule'.

错误:ImportError:没有名为‘Schedule’的模块。



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

我用的是PIP3节目时间表,它已经存在了。所以我不知道我的剧本出了什么问题。



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

我在system d中打印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: [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


更多回答

Have you tried pip install schedule on the command line?

您在命令行中尝试过pip安装时间表吗?

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.

你能展示一下sys.Path有什么吗?尝试引发一个带有它的值的伪异常,以便在服务启动日志中显示它。

Sure, result is Requirement already satisfied.

当然,结果是已经满足了要求。

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

SYSTEM D可能使用系统端的Python来运行该脚本。您可以通过在脚本的顶部运行类似IMPORT sys;print(sys.executable)的命令并保存SYSTEM D生成的输出来测试这一点

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.

同样,您可以在文件的顶部添加#!/usr/bin/python3,以建议任何试图将文件作为可执行文件运行的进程应该使用python3来运行它。Shebang应该指向在其侧包中安装了调度模块的python解释器,它很可能是/usr/bin上的python3,但谁知道呢。

优秀答案推荐

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.

根据这些日志,我发现PYTHONPATH在手动外壳和系统中是不同的,我尝试在/“/home/ubuntu/.local/lib/python3.5/site-packages”/Profile中添加配置文件,但系统日志显示仍然找不到路径。



So i do a stuip thing, add

所以我做了一件很有趣的事,加



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


in my code, and it works...

在我的代码中,它起作用了..。



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:

或者,尝试以另一个特定用户的身份运行它,而不是以超级用户身份运行。将您的.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


Hope that helps!

希望这能有所帮助!



#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'


pip install schedule OR

PIP安装计划或


pip3 install schedule

PIP3安装计划


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

#3是PIP(Python包管理器)的版本


更多回答

I hope you used a specific username and not literally "User=user" .If so, is it the same error or different?

我希望您使用的是特定的用户名,而不是字面上的“user=user”。如果是这样,它是相同的错误还是不同的错误?

Maybe if you change it to ExecStart=/home/ubuntu/.local/bin/python3.5 coinview.py?

也许你可以把它改成ExecStart=/home/ubuntu/.local/bin/python3.5coinview.py?

just installed the package with root solved my problem. Thanks, mate

我只是用超级用户安装了这个包,就解决了我的问题。谢谢,伙计

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.

正如它目前所写的,你的答案并不清楚。请编辑以添加更多详细信息,以帮助其他人了解这是如何解决提出的问题的。你可以在帮助中心找到更多关于如何写出好答案的信息。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com