Search code examples
pythonmacospython-2.7python-datetimepython-dateutil

Python dateutil rrule error


I get the following error in the example code below. I'm not sure why or what is causing the error as this code was working fine in the past. I am using Python 2.7

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

Here is a minimal example that contains the problem.

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)

The full traceback

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'

From the dateutil source code and from user @PatrickCollins , the problem can be produced with

import _thread

_thread.allocate_lock()

Solution

  • After further investigation the problem looks to be with pip installing the incorrect version. The problem is resolved with installing datetuil-1.5.

    python-dateutil-2.0.tar.gz (Python >= 3.0)

    python-dateutil-1.5.tar.gz (Python < 3.0)

    However this may lead to more questions as to why dateutil version 2.2 is working for others with python 2.7