gpt4 book ai didi

Python dateutil 规则错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:33 28 4
gpt4 key购买 nike

我在下面的示例代码中收到以下错误。我不确定导致错误的原因或原因,因为此代码过去运行良好。我正在使用 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()

最佳答案

经过进一步调查,问题似乎出在 pip 上。安装不正确的版本。通过安装 datetuil-1.5 解决了这个问题。

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

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

然而,这可能会引发更多问题,即为什么 dateutil 2.2 版适用于其他使用 python 2.7 的人

关于Python dateutil 规则错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25586128/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com