27

I'm getting this error 'module' object has no attribute '_strptime' but only when I use several threads. When I only use one it works fine. Im using python 2.7 x64. Here there us the reduced function i'm calling

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

Here is the complete traceback:

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'
1
  • 1
    What is the complete traceback? Aug 27, 2015 at 9:36

6 Answers 6

23

I can confirm that the issue is related to multithreading, and it happens to me occasionally when I use datetime.datetime.strptime in combination with the ThreadPool module.

I was able to fix this in my script by importing the "missing" module as follows:

import _strptime
2
  • This helped me. Can anybody explain why? Any why this error occurs spontaneously? Jun 4, 2019 at 23:18
  • @AdamFuller See my comment on another answer. This is a known multi-threading bug in that Python 2 library. It will not be fixed.
    – kevinarpe
    Feb 8, 2022 at 12:13
16

The problem is described in a mailing list message "threading bug in strptime".

datetime.strptime has a problem with Python 2's threading module. The workaround suggested there seems to be to invoke strptime = datetime.datetime.strptime before any threads are started.

2
6

Just ran into this exact problem. It's a tricky one - took me an hour or so to track it down. I tried launching the shell and entering in the following code:

import datetime

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

This worked fine. Then I tried it in a blank file in my workspace. This gave the same error you described. I tried running it from the command line in my workspace. Still gave the error. I then launched the shell from my workspace. This time it gave the error in the shell environment. As it turned out, any directory other than the one I was in worked fine.

The problem was that my project was a python calendar app, and my main file was called "calendar.py". This conflicted with some native import, thus creating the bizarre error.

In your case, I'd bet anything the problem is the name of your file: "file.py". Call it something else, and you should be good to go.

3
  • 1
    I'm having this with PyqT :\
    – Josh Usre
    Aug 26, 2016 at 21:06
  • 1
    The problem must be something else
    – Nickpick
    Feb 1, 2018 at 11:54
  • I had this problem. The problem was that my python file was called time.py, renaming this made it work. Aug 31, 2018 at 15:29
2

I was running into this issue when testing out a script that had been working on Linux on a Windows machine, and I was able to fix it by simply adding an import statement at the top of the thread.

def multithreadedFunction():
    from datetime import datetime
    # Rest of the function

Probably worth trying this out before modifying your function to not use the datetime module, since this is a much quicker fix if it works.

1

Same error in my thread module which uses the datetime.strptime() method.

As filed in https://bugs.python.org/issue7980, that method does not import _strptime.py in a thread safe way.

One of the last comments says that: "this is a Python 2.7-only bug, and it's not a security issue, so the issue may be closed as either "wontfix" (because we won't fix it in Python 2) or "fixed" (because it is already fixed in Python 3), depending on your perspective."

0

I somehow managed to get a ModuleNotFoundError: No module named '_strptime' error in python 38, 39, and 310 when using datetime.strptime when running the below code within a gitlab ci pipeline. I haven't found anything like this online, so thought best to put it here for anyone who comes across it.

For example, the following works perfectly locally (and I expect should always work):

from datetime import datetime

my_datetime_object = datetime.strptime(date_string, date_format)

But the above code for some reason caused the ModuleNotFoundError: No module named '_strptime' error.

My roundabout way of fixing this was as follows:

import _strptime  # due to gitlab ci problem
from datetime import datetime

my_datetime_object = _strptime._strptime_datetime(datetime, date_string, date_format)

which worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.