'module' 对象没有具有多个线程的属性“_strptime”Python
'module' object has no attribute '_strptime' with several threads Python
我收到此错误 '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()
这是完整的回溯:
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'
只是 运行 进入这个确切的问题。这是一个棘手的问题——我花了一个小时左右的时间才找到它。我尝试启动 shell 并输入以下代码:
import datetime
print(datetime.datetime.strptime("2015-4-4", "%Y-%m-%d"))
这很好用。然后我在我工作区的一个空白文件中尝试了它。这给出了您描述的相同错误。我在工作区的命令行中尝试 运行 它。还是报错。然后我从我的工作区启动了 shell。这次它在 shell 环境中给出了错误。事实证明,除我所在的目录之外的任何目录都可以正常工作。
问题是我的项目是一个 python 日历应用程序,我的主文件名为 "calendar.py"。这与某些本机导入冲突,从而产生了奇怪的错误。
就您而言,我敢打赌问题出在您的文件名上:"file.py"。换个名字,你应该可以开始了。
邮件列表消息“threading bug in strptime”中描述了该问题。
datetime.strptime
Python 2 的 threading
模块有问题。 似乎建议的解决方法是在启动任何线程之前调用strptime = datetime.datetime.strptime
。
我可以确认这个问题与多线程有关,当我将 datetime.datetime.strptime
与 ThreadPool
模块结合使用时偶尔会发生这种情况。
我能够通过导入 "missing" 模块在我的脚本中解决这个问题,如下所示:
import _strptime
在 Windows 机器上测试一直在 Linux 上运行的脚本时,我 运行 遇到了这个问题,我可以通过简单地添加一个线程顶部的 import 语句。
def multithreadedFunction():
from datetime import datetime
# Rest of the function
在修改您的函数以不使用 datetime 模块之前可能值得尝试一下,因为如果它有效,这是一个更快的修复。
我使用 datetime.strptime() 方法的线程模块出现同样的错误。
如 https://bugs.python.org/issue7980 中所述,该方法不会以线程安全的方式导入 _strptime.py。
最后一条评论说:"this is a Python 2.7-only bug, and it's not a security issue, so the issue may be closed as either "wontfix"(因为我们不会在 Python 2 中修复它)或 "fixed"(因为它已经在 Python 3), 取决于你的观点。"
我收到此错误 '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()
这是完整的回溯:
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'
只是 运行 进入这个确切的问题。这是一个棘手的问题——我花了一个小时左右的时间才找到它。我尝试启动 shell 并输入以下代码:
import datetime
print(datetime.datetime.strptime("2015-4-4", "%Y-%m-%d"))
这很好用。然后我在我工作区的一个空白文件中尝试了它。这给出了您描述的相同错误。我在工作区的命令行中尝试 运行 它。还是报错。然后我从我的工作区启动了 shell。这次它在 shell 环境中给出了错误。事实证明,除我所在的目录之外的任何目录都可以正常工作。
问题是我的项目是一个 python 日历应用程序,我的主文件名为 "calendar.py"。这与某些本机导入冲突,从而产生了奇怪的错误。
就您而言,我敢打赌问题出在您的文件名上:"file.py"。换个名字,你应该可以开始了。
邮件列表消息“threading bug in strptime”中描述了该问题。
datetime.strptime
Python 2 的 threading
模块有问题。 似乎建议的解决方法是在启动任何线程之前调用strptime = datetime.datetime.strptime
。
我可以确认这个问题与多线程有关,当我将 datetime.datetime.strptime
与 ThreadPool
模块结合使用时偶尔会发生这种情况。
我能够通过导入 "missing" 模块在我的脚本中解决这个问题,如下所示:
import _strptime
在 Windows 机器上测试一直在 Linux 上运行的脚本时,我 运行 遇到了这个问题,我可以通过简单地添加一个线程顶部的 import 语句。
def multithreadedFunction():
from datetime import datetime
# Rest of the function
在修改您的函数以不使用 datetime 模块之前可能值得尝试一下,因为如果它有效,这是一个更快的修复。
我使用 datetime.strptime() 方法的线程模块出现同样的错误。
如 https://bugs.python.org/issue7980 中所述,该方法不会以线程安全的方式导入 _strptime.py。
最后一条评论说:"this is a Python 2.7-only bug, and it's not a security issue, so the issue may be closed as either "wontfix"(因为我们不会在 Python 2 中修复它)或 "fixed"(因为它已经在 Python 3), 取决于你的观点。"