"type object 'datetime.datetime' has no attribute 'datetime'" even with "import datetime"

datetime, google-app-engine, python

Solution

The code you've included works for me, so the problem is likely that you've shadowed the `datetime` module with a `datetime.datetime` object called `datetime`. No seriously, I meant to type all that.

>>> import datetime
>>> datetime = datetime.datetime.now()  # waves goodbye to datetime module!
>>> task.due_at = datetime.datetime.strptime(date, '%Y-%m-%d %I:%M%p')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute 'datetime'

Problem

I'm getting `type object 'datetime.datetime' has no attribute 'datetime'` errors on AppEngine, complaining about the datetime type, but my import is `import datetime`. There are `from datetime import datetime` in other files, but I don't think that should affect this file? There's no 'accidental' re-imports, I've checked. I've checked my AppEngine logs, and it only started happening 2 days ago. I'm using 2.7 runtime. EDIT: Here's the line that's causing the error (note that I'm using `import datetime`, NOT `from datetime import datetime`) ``` task.due_at = datetime.datetime.strptime(date, '%Y-%m-%d %I:%M%p') ``` EDIT: Stack trace ``` type object 'datetime.datetime' has no attribute 'datetime' Traceback (most recent call last): File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch return method(*args, **kwargs) File "/base/data/home/apps/s~wmphighrise/1.373696587983821954/myapp/handler/decorators.py", line 22, in wrapper return fn(*args, **kwargs) File "/base/data/home/apps/s~wmphighrise/1.373696587983821954/myapp/handler/api/main.py", line 1343, in post task.due_at = datetime.datetime.strptime(date, '%Y-%m-%d %I:%M%p') AttributeError: type object 'datetime.datetime' has no attribute 'datetime' ``` EDIT 3: Some debugging Note: `import datetime` is at the top ``` #1st attempt import datetime class MyHandler(): def get(self): logging.info(datetime) # => "<type 'datetime.datetime'>" #2nd attempt import datetime class MyHandler(): def get(self): import datetime # explicitly re-import the module logging.info(datetime) # => "<module 'datetime' (built-in)>" #3rd attempt import datetime class MyHandler(): def get(self): logging.info(datetime) # => Throws UnboundLocalError: local variable 'datetime' referenced before assignment # Is this normal? This is new to me. import datetime logging.info(datetime) #4th attempt import datetime logging.info(datetime) # => "<module 'datetime' (built-in)>" class MyHandler(): def get(self): logging.info(datetime) # => "<type 'datetime.datetime'>" ``` Is there a way for a variable to be redefined outside the current file or module? Because I've looked and looked at this file and there's no redefining at all. EDIT 4: I've `ack`'d `"datetime ="`, `"datetime="`, `"datetime.datetime ="` and `"datetime.datetime="`, but there's no results that does reassigning. I've checked my git log for the last 2 days, and there's no changes that could've introduced it

Original source