"tipo de objeto 'datetime.datetime' no tiene atributo 'datetime'" incluso con "importar fecha y hora"

Me estoy poniendo type object 'datetime.datetime' has no attribute 'datetime' errors on AppEngine, complaining about the datetime type, but my import is import datetime. Allí están from datetime import datetime in otros 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 datetimeNO from datetime import datetime)

task.due_at = datetime.datetime.strptime(date, '%Y-%m-%d %I:%M%p')

EDITAR: Seguimiento de pila

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

Nota: import datetime está en la parte superior

#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 afuera the current file or module? Because I've looked and looked at this file and there's no redefining at all.

EDIT 4:

He ack'd "datetime =", "datetime=", "datetime.datetime =" y "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

preguntado el 12 de febrero de 14 a las 06:02

No hay datetime.datetime.datetime. You've gone one layer too deep in your inception. -

Yes, I know. I've imported the datetime module. There should be a datetime.datetime type class in it. -

there is, but you're trying to call datetime en un datetime.datetime object. I count 3 :) -

The error says 'datetime.datetime' does not have 'datetime', because it doesn't. You just need 'datetime.datetime'. -

Are your edits all in one module? Or different versions? -

3 Respuestas

The code you've included works for me, so the problem is likely that you've shadowed the datetime módulo con un datetime.datetime objeto llamado 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'

Respondido 12 Feb 14, 06:02

I wish it was that. I've reviewed my code, and there's no re-assigning datetime. - juan2x

¿Puede usted print datetime before the line in question? - mhléster

Updated OP with print statements. - juan2x

Is there a way for a variable to redefined afuera ¿el archivo? - juan2x

yes. everything importing datetime is accessing the same module. you could theoretically have a call datetime.datetime = datetime.datetime.now() made from a different module - mhléster

If I understand you correctly, you are asking about the UnboundLocalError error in the following code snippet. Am I right?

#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)

If so, the error is quite obvious. Two things you should understand:

  1. All namespace changes (assignment,del,import,def,class) happen in the alcance local(i.e. in the current scope in which the namespace-changing code executes)

  2. import X imports the module X, and creates a reference to that module in the current namespace. i.e. import changes namespace.

Though name datetime did refer to the same object(module datetime) before and after you call import datetime en función get, but those two datetime are in different scope(first one global, second one local).

Once Python detect that there is a name(in your case "datetime") in local scope, it will raise error if you use the name before your local name binding.

>>> import datetime
>>> def get():
        print(dir())

>>> get()
[]
>>> def get():
        import datetime
        print(dir())

>>> get()
['datetime']
>>> 

Espero eso ayude.

Respondido 12 Feb 14, 08:02

I've always gotten a chuckle out of saying datetime.datetime, but you've taken it even further and asked python for datetime.datetime for it's attribute datetime. : D

And of course, python doesn't have a datetime.datetime.datetime

Respondido 12 Feb 14, 06:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.