[Django]-'module' object has no attribute 'now' will trying to create a CSV

115๐Ÿ‘

โœ…

You probably have

import datetime

change that to

from datetime import datetime

Demo:

>>> import datetime
>>> datetime.now()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'now'
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2013, 10, 7, 13, 57, 18, 456504)
>>> 

Also, you will run into issues due to indentation. Please fix those.

๐Ÿ‘คkarthikr

12๐Ÿ‘

When you do

import datetime

you have to use

>>> datetime.datetime.now()
datetime.datetime(2016, 12, 14, 1, 15, 58, 606802)

otherwise if you import like

>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2016, 12, 14, 1, 17, 31, 772406)

But on some machine you could refer to wrong datetime module because of sys.path , instead of doing from datetime import datetime or import datetime make a habbit of using

from datetime import datetime as dt
๐Ÿ‘คRizwan Mumtaz

2๐Ÿ‘

i had the same problem when i used

from datetime import datetime,date,timedelta
import pytz
utc=pytz.UTC
today = datetime.now().replace(tzinfo=utc)

Solution for this which i will suggest is to import all the dependencies

from datetime import *
import pytz
utc=pytz.UTC
today = datetime.now().replace(tzinfo=utc)
๐Ÿ‘คYash Rastogi

Leave a comment