“module”对象没有带有多个线程Python的属性“strtime”

2024-04-23 18:37:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了这个错误'module' object has no attribute '_strptime',但只有当我使用多个线程时。当我只使用一个的时候,效果很好。我使用Python2.7x64。这里是我调用的简化函数

import datetime
def get_month(time):
    return datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%S+0000').strftime("%B").lower()

以下是完整的回溯:

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

Exception in thread Thread-22:
Traceback (most recent call last):
  File "C:\Python27x64\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27x64\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\file.py", line 81, in main
    month=get_month(eventtime)
  File "C:\file.py", line 62, in get_month
    return datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%S+0000').strftime("%B").lower()
AttributeError: 'module' object has no attribute '_strptime'

Tags: noinpyselfgetdatetimeobjecttime
3条回答

在邮件列表消息“threading bug in strptime”中描述了该问题。

datetime.strptime与Python 2的threading模块有问题。这里建议的解决方法似乎是在启动任何线程之前调用strptime = datetime.datetime.strptime

刚刚碰到这个问题。这是个棘手的问题-我花了一个小时左右才找到它。我试着启动shell并输入以下代码:

import datetime

print(datetime.datetime.strptime("2015-4-4", "%Y-%m-%d"))

这很管用。然后我在我工作区的一个空白文件里试了一下。这个错误和你描述的一样。我试着从我工作区的命令行运行它。还是犯了错误。然后我从我的工作区启动了shell。这次它在shell环境中给出了错误。结果发现,除了我所在的目录外,其他任何目录都运行良好。

问题是我的项目是一个python日历应用程序,我的主文件名为“calendar.py”。这与一些本机导入冲突,从而产生了奇怪的错误。

对你来说,我敢打赌问题出在你的文件名:“file.py”。换个说法吧,你该走了。

我可以确认这个问题与多线程有关,当我使用datetime.datetime.strptimeThreadPool模块结合使用时,偶尔会发生这种情况。

通过导入“missing”模块,我可以在脚本中修复此问题,如下所示:

import _strptime

相关问题 更多 >