Skip to content

Instantly share code, notes, and snippets.

@pierreozoux
Created November 3, 2013 16:59
Show Gist options
  • Save pierreozoux/7292309 to your computer and use it in GitHub Desktop.
Save pierreozoux/7292309 to your computer and use it in GitHub Desktop.
Python...
✘ pierreozoux@local ⮀ ~ ⮀ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.today()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'today'
>>> from datetime import datetime
>>> datetime.today()
datetime.datetime(2013, 11, 3, 16, 45, 50, 745661)
>>> datetime.timedelta(days=30)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
>>> import datetime
>>> datetime.timedelta(days=30)
datetime.timedelta(30)
>>>
@coot
Copy link

coot commented Nov 4, 2013

I agree this is confusing. The datetime.datetime is a class which has bunch of useful classmethods (like now). I usually do from datetime import datetime, timedelta and then your code will look good. If you need to access the datetime module (rather than the datetime.datetime class), you can access it with: sys.modules['datetime'].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment