-2

I'm using this inside a Django view. I copied this exactly: http://www.pythonexamples.org/2010/12/23/how-to-get-todays-date-in-python/

That is:

import datetime
variable = datetime.date.today()

I also tried

from datetime import datetime
variable = datetime.date.today()

But then this error occurs:

'method_descriptor' object has no attribute 'today'
Firkamon
  • 807
  • 1
  • 9
  • 19
  • 5
    The first variant works just fine. Neither fits your error message. – Martijn Pieters Aug 15 '14 at 15:47
  • 3
    What version of python are you using? Also, see if there are any namespace conflicts - do a `import datetime as helloworld` and then do `helloworld.date.today()` – Bo Milanovich Aug 15 '14 at 15:48
  • That's nice, but it doesn't work for me. Testing 'helloworld' as we speak – Firkamon Aug 15 '14 at 15:49
  • 'helloworld' was not successful, I'm using 2.6 – Firkamon Aug 15 '14 at 15:52
  • 1
    Wow, thanks for the downvotes. I wouldn't be asking here if it were as simple as you think. The fact that it works for you is worthless to me. I know this should work. I'm asking why this doesn't work in my case. – Firkamon Aug 15 '14 at 15:52
  • 1
    Possible duplicate: http://stackoverflow.com/questions/4909577/python-djangjo-why-am-i-getting-this-error-attributeerror-method-descripto and another: http://stackoverflow.com/questions/7572690/python-datetime-randomly-breaking – Johndt Aug 15 '14 at 15:55
  • Does anything else from the datetime class work? Try also `datetime.date.fromtimestamp(time.time())` – Bo Milanovich Aug 15 '14 at 15:57
  • Check [this question](http://stackoverflow.com/q/4909577/1113211) – salmanwahed Aug 15 '14 at 16:02
  • No luck with datetime.date.fromtimestamp(time.time()). Thanks for the link @Johndt6, that slipped my grasp. Edit - Using datetime.datetime.today().date() (reversing the order of today() and date() yields the same error unfortunately. – Firkamon Aug 15 '14 at 16:02
  • 2
    @Firkamon: it would help you actually included full tracebacks for all the variants you are trying. There are some *common* mistakes with importing the `datetime` module and confusion over the types it contains and methods on the types. However your question title contradicts your question code and there are no concrete error messages. That makes it **terribly hard** for us to help you and why you probably are getting downvoted. – Martijn Pieters Aug 15 '14 at 16:29

2 Answers2

2

You asked about why this thing happens... so here goes my try and my first ever answer in stack overflow.

It seems like a scope/namespace issue to me.

Check today() 's scope. Is it accessible from where you refer to it?

Check also this guy: http://igotgenes.blogspot.gr/2009/01/class-attributes-and-scoping-in-python.html

And this: https://docs.python.org/2/tutorial/classes.html

in order to understand deeper the reason it might happened.

Sorry but can't help more, since I don't have your whole project at my disposal :(

Edit: Just a few typos.

George Eco
  • 466
  • 3
  • 16
0

import datetime variable = datetime.date.today()

This works as expected.

from datetime import datetime variable = datetime.date.today()

This gives the error you described. That's because datetime.datetime.date is a method, not a class. datetime.date.today is a method, but not datetime.datetime.date.today (the extra datetime is the problem).

First thing you should do is launch an interpreter (type python at a command prompt) and type just the first two lines. It should work. (if it doesn't, I would probably just reinstall python)

If it DOES work, the problem is in your code file. You must have something overshadowing one of those classes. Since you didn't provide the error message from the first code example (which is correct) I have no guesses as to which class it is... but carefully look at your import statements and variable definitions between import datetime and when you try to use it.

Paul Becotte
  • 9,767
  • 3
  • 34
  • 42