【问题标题】:Python dateutil rrule errorPython dateutil rrule 错误
【发布时间】:2014-08-30 19:44:19
【问题描述】:

我在下面的示例代码中收到以下错误。我不确定导致错误的原因或原因,因为此代码过去运行良好。我正在使用 Python 2.7

AttributeError: 'module' object has no attribute 'allocate_lock'

这是一个包含问题的最小示例。

import pandas as pd
import pytz

from datetime import datetime, timedelta
from dateutil import rrule

start = pd.Timestamp('1900-01-01', tz='UTC')
end_base = pd.Timestamp('today', tz='UTC')
end = end_base + timedelta(days=365)

def canonicalize_datetime(dt):
    return datetime(dt.year, dt.month, dt.day, tzinfo=pytz.utc)

def get_rules(start, end):
    rules = []

    start = canonicalize_datetime(start)
    end = canonicalize_datetime(end)

    weekends = rrule.rrule(
        rrule.YEARLY,
        byweekday=(rrule.SA, rrule.SU),
        cache=True,
        dtstart=start,
        until=end
    )
    rules.append(weekends)

    return rules

rules = get_rules(start, end)

完整的追溯

Traceback (most recent call last):
  File "/Users/mac/Documents/test.py", line 48, in <module>
    rules = get_rules(start, end)
  File "/Users/mac/Documents/test.py", line 42, in get_rules
    until=end
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dateutil/rrule.py", line 239, in __init__
    super(rrule, self).__init__(cache)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/dateutil/rrule.py", line 90, in __init__
    self._cache_lock = _thread.allocate_lock()
AttributeError: 'module' object has no attribute 'allocate_lock'

从 dateutil 源代码和用户 @PatrickCollins 可以看出问题的产生

import _thread

_thread.allocate_lock()

【问题讨论】:

  • 你是如何安装dateutil的?不知何故,看起来您可能拥有 py3k 版本。自上次运行以来,您是否进行了任何更改?
  • @PatrickCollins 我用过pip-2.7,我跑了pip-2.7 install --upgrade python-dateutil 只是为了确定,它说一切都是最新的,我刚试过python-3.4,3.4 运行良好。
  • 嗯,我错了py3k版本。这真是令人费解。你能在 2.7 shell 中尝试import threadthread.allocate_lock() 看看会发生什么吗?
  • @PatrickCollins 我刚刚在 2.7 上尝试了你的建议,没有错误。
  • @pyCthon 如果您将适合您的答案从您的问题中移出,将其作为答案发布,然后将其标记为已接受,那最好。这样,社区就可以看到这个问题得到了有效解决:)

标签: python macos python-2.7 python-datetime python-dateutil


【解决方案1】:

经过进一步调查,问题似乎与pip 安装了不正确的版本有关。安装 datetuil-1.5 解决了问题。

python-dateutil-2.0.tar.gz (Python &gt;= 3.0)

python-dateutil-1.5.tar.gz (Python &lt; 3.0)

然而,这可能会导致更多关于为什么 dateutil 2.2 版适用于其他使用 python 2.7 的问题

【讨论】:

    【解决方案2】:

    使用 Anaconda Python 运行 Caffe 时出现此错误; cmets中的语句为我解决了它

    pip install --upgrade python-dateutil
    Collecting python-dateutil
      Downloading python_dateutil-2.6.0-py2.py3-none-any.whl (194kB)
        100% |████████████████████████████████| 194kB 484kB/s 
    Requirement already up-to-date: six>=1.5 in /home/alex/anaconda3/lib/python3.6/site-packages (from python-dateutil)
    Installing collected packages: python-dateutil
      Found existing installation: python-dateutil 1.5
        Uninstalling python-dateutil-1.5:
          Successfully uninstalled python-dateutil-1.5
    Successfully installed python-dateutil-2.6.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多