'module'对象没有带有几个线程Python的属性'_strptime'

use*_*465 19 python time datetime multithreading python-2.7

我收到此错误,'module' object has no attribute '_strptime'但只有当我使用多个线程时.当我只使用一个它工作正常.我使用python 2.7 x64.这里有我正在调用的缩减功能

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

这是完整的追溯:

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'
Run Code Online (Sandbox Code Playgroud)

chr*_*rki 12

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

我能够通过导入"缺失"模块在我的脚本中修复此问题,如下所示:

import _strptime
Run Code Online (Sandbox Code Playgroud)


aks*_*han 10

该问题在邮件列表消息" stricular中的线程错误"中描述.

datetime.strptimePython 2的threading模块有问题.解决方法建议在启动任何线程之前似乎要调用strptime = datetime.datetime.strptime.


the*_*man 6

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

import datetime

print(datetime.datetime.strptime("2015-4-4", "%Y-%m-%d"))
Run Code Online (Sandbox Code Playgroud)

这工作得很好。然后我在我的工作区的一个空白文件中尝试了它。这给出了您描述的相同错误。我尝试从我的工作区中的命令行运行它。还是报了错。然后我从我的工作区启动了 shell。这次它在shell环境中给出了错误。事实证明,除了我所在的目录之外的任何目录都可以正常工作。

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

在您的情况下,我敢打赌问题出在您的文件名:“file.py”。称之为别的东西,你应该很高兴。