【问题标题】:Python-crontab package returns __init__ errorPython-crontab 包返回 __init__ 错误
【发布时间】:2020-06-03 21:13:35
【问题描述】:

谁能告诉我我在使用 python-crontab 时做错了什么?

from crontab import CronTab
system_cron = CronTab()

产生以下错误:

File "test.py", line 3, in <module>
cron = CronTab()
TypeError: __init__() takes exactly 2 arguments (1 given)

当我直接从包装说明中尝试其他示例变体时,我遇到了同样的问题:

my_user_cron  = CronTab(user=True)
users_cron    = CronTab(user='username')

我也尝试使用这种方法创建对象,我在 python-crontab.py 文件中找到了该方法:

cron = CronTab(tab='')

但它会产生这个错误:TypeError: __init__() got an unexpected keyword argument 'tab'

我尝试查看包中的代码,看看是否可能是文档错误并想办法解决,但这超出了我的技能水平。我相信这是定义我应该如何创建 crontab 对象的代码:

def __init__(self, user=None, tab=None, tabfile=None, log=None):
    if user == True and not WINOS:
        user = pwd.getpwuid( os.getuid() )[ 0 ]
    self.lines = None
    self.crons = None
    self.filen = None
    # Protect windows users
    self.root  = not WINOS and os.getuid() == 0
    self.user  = user
    # Detect older unixes and help them out.
    self.intab = tab
    self.read(tabfile)
    self._log = log

对我做错了什么有什么想法吗?

help(CronTab) 返回:

class CronTab(__builtin__.object)
|  Methods defined here:
|
|  __init__(self, crontab)
|
|  next(self, now=None, increments=[<function <lambda>>, <function <lambda>>, <function <lambda>>, <function _month_incr>, <function <lambda>>, <function _year_incr>, <function <lambda>>, <function <lambda>>, <function <lambda>>, <function <lambda>>, <function <lambda>>], delta=True)
|      How long to wait in seconds before this crontab entry can next be
|      executed.
|
|  previous(self, now=None, delta=True)
|
|  test(self, entry)
|
|   ----------------------------------------------------------------------
|  Data descriptors defined here:
|
|   matchers

【问题讨论】:

  • help(CronTab) 打印什么?
  • 为问题添加了帮助(CronTab)输出。

标签: python crontab


【解决方案1】:

您已经安装了crontab 软件包,您显示的文档是针对python-crontab 的。它们是两个完全不同的包。

【讨论】:

  • 谢谢。前两行代码直接来自 python-crontab 包文档。如果这是文档中的错误,它似乎由来已久。同一包的早期版本中使用了相同的文档。
  • 这不是文档错误,您安装了错误的软件包。两个包只是碰巧有名字冲突。
【解决方案2】:

如果您在使用 CronTab 时收到错误 TypeError: init() 仅采用 2 个参数,则说明您安装了错误的模块。您需要从 pypi 或本地包管理器安装 python-crontab 而不是 crontab,然后重试。

参考:https://pypi.org/project/python-crontab/

卸载 crontab

pip uninstall crontab

& 安装 python-crontab

pip install python-crontab

【讨论】:

    【解决方案3】:
    cron = CronTab(user='name')
    TypeError: __init__() got an unexpected keyword argument 'user'
    

    如果您遇到上述错误,请执行以下操作:

    使用

    卸载 crontab
    pip uninstall crontab
    

    并安装

    pip install python-crontab
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-08
      • 2013-06-24
      • 1970-01-01
      • 2020-08-08
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      相关资源
      最近更新 更多