【问题标题】:Python AttributeError: partially initialized modulePython AttributeError:部分初始化的模块
【发布时间】:2021-02-19 11:01:27
【问题描述】:

我正在制作一个每 10 秒运行一次脚本的脚本(例如)

但我明白了:

AttributeError: 部分初始化的模块 'schedule' 没有属性 'every' (很可能是由于循环导入)

我的代码:

import schedule
import time

def job():
    print("I'm working...")

schedule.every(3).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

【问题讨论】:

  • 你的文件名是什么?如果是 schedule.py 将其更改为其他内容,您最终只会导入自身。
  • 检查你的本地模块名称,可能其中一个叫做schedule.py
  • 你的代码在我这边工作。你安装了schedule 包吗?
  • 你的文件名是什么?或者您的文件所在的文件夹中是否还有其他文件,名称类似于 scheduletime
  • 您好!感谢您的回复,我调用了我的文件 schedule.py!我现在改名了。 schedule.run.pending() AttributeError: module 'schedule' has no attribute 'run'

标签: python time


【解决方案1】:
import schedule
import time

def job():
    print("I'm working...")

schedule.every(3).seconds.do(job)

while True:
    schedule.run.pending()
    time.sleep(1)

【讨论】:

  • 似乎也不起作用? ``` 导入计划导入时间 ^ SyntaxError: invalid syntax ```
  • 呃,你能解释一下吗?
  • 它不工作。 schedule 没有属性 run
  • 是run_pending(下划线),而不是run.pending(点)。
【解决方案2】:

感谢 Azat Ibrakov 和 Abdul Aziz Barkat 说我需要重命名我的 python 脚本。

感谢 joao 说它是“run_pending”而不是“run.pending”。

工作代码:

import schedule
import time

def job():
    print("I'm working...")

schedule.every(3).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-09
    • 2020-07-15
    • 2021-04-20
    • 2011-03-06
    • 1970-01-01
    • 2022-01-13
    • 2019-11-09
    相关资源
    最近更新 更多