'module' object has no attribute '_strptime' with several threads Python

Question:

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:Python27x64libthreading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:Python27x64libthreading.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'
Asked By: user1618465

||

Answers:

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.

Answered By: thegiffman

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.

Answered By: akshan

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
Answered By: chrki

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.

Answered By: Josh Jacobson

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.”

Answered By: aritstack

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.

Answered By: PinkShnack