Python cron - ValueError: improper number of cron entries specified; got 1 need 5 to 7
Python cron - ValueError: improper number of cron entries specified; got 1 need 5 to 7
我有错误:
ValueError: improper number of cron entries specified; got 1 need 5 to
7
我正在使用 Python 3,这是我的代码:
def add_cron():
cron = CronTab(getpass.getuser()) # For current user
basic_command = "*/15 * * * * python3 /opt/yesiam.py"
basic_iter = cron.find_command("yesiam")
exists=False
for item in basic_iter:
if str(item) == basic_command:
print("crontab job already exist", item)
exists=True
break
if not exists:
job = cron.new(command='python3 /opt/yesiam.py')
job.minute.every(15)
job.enable()
cron.write()
有谁知道我错在哪里?
问题是您使用的是完全不同的软件包。这是一个容易犯的错误,因为他们的名字如此相似。
该错误消息来自 crontab
包。
安装使用:
pip install crontab
您需要 python-crontab
包。
安装使用:
pip install python-crontab
它们是不同的项目,由不同的人维护。在此处查看开源存储库:
- 对于
python-crontab
包:https://gitlab.com/doctormo/python-crontab/
- 对于
crontab
包:https://github.com/josiahcarlson/parse-crontab
考虑使用 virtual environments 来防止您可能与使用冲突包和版本的其他项目发生任何冲突。
我有错误:
ValueError: improper number of cron entries specified; got 1 need 5 to 7
我正在使用 Python 3,这是我的代码:
def add_cron():
cron = CronTab(getpass.getuser()) # For current user
basic_command = "*/15 * * * * python3 /opt/yesiam.py"
basic_iter = cron.find_command("yesiam")
exists=False
for item in basic_iter:
if str(item) == basic_command:
print("crontab job already exist", item)
exists=True
break
if not exists:
job = cron.new(command='python3 /opt/yesiam.py')
job.minute.every(15)
job.enable()
cron.write()
有谁知道我错在哪里?
问题是您使用的是完全不同的软件包。这是一个容易犯的错误,因为他们的名字如此相似。
该错误消息来自 crontab
包。
安装使用:
pip install crontab
您需要 python-crontab
包。
安装使用:
pip install python-crontab
它们是不同的项目,由不同的人维护。在此处查看开源存储库:
- 对于
python-crontab
包:https://gitlab.com/doctormo/python-crontab/ - 对于
crontab
包:https://github.com/josiahcarlson/parse-crontab
考虑使用 virtual environments 来防止您可能与使用冲突包和版本的其他项目发生任何冲突。