• Register
0 votes
1.1k views

Problem :

I am currently working on a django  and I have gotten the below error:

“Type object 'datetime.datetime' has no attribute 'datetime'”

On a below line:

date = datetime.datetime(int(year), int(month), 1)

Does someone know any reason for above error?

7 5 2
3,870 points

1 Answer

0 votes

Solution :

Datetime is the module that allows you handling of dates, times and datetimes (all of which are the datatypes). This clearly means that datetime is both the top-level module as well as being the type within that module. This is confusing.

Your error is mostly based on a confusing naming of a module, and what either you or the module you are trying to use has already imported.

>>> import datetime
>>> datetime
<module 'datetime' from '/usr/lib/python2.6/lib-dynload/datetime.so'>
>>> datetime.datetime(2001,5,1)
datetime.datetime(2001, 5, 1, 0, 0)

But, if you try to import datetime.datetime:

>>> from datetime import datetime
>>> datetime
<type 'datetime.datetime'>
>>> datetime.datetime(2001,5,1) # You shouldn't expect this to work 
                                # as you imported the type, not the module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
>>> datetime(2001,5,1)
datetime.datetime(2001, 5, 1, 0, 0)

I guess you or one of your modules you're using has imported like following: from datetime import datetime.

9 7 4
38,600 points

Related questions

0 votes
1 answer 453 views
453 views
Problem : I am new to HTMLParser my terminal showing me below error :- from django.utils.html import format_html, format_html_join, escape File "/usr/local/lib/python3.5/dist-packages/django/utils/html.py", line 16, in from .html_parser import HTMLParser, HTMLParseError ... I replace the HTMLParser with some other similar? Or should I try to downgrade the python ? Can someone help on this issue?
asked Mar 2 mphil 2.3k points
0 votes
1 answer 25 views
25 views
Problem: I am facing this error in my logs with the django app. Initially I thought such errors tend to occur if using Python 2.x but in my virtual environment you can see Python - 3.6.5. Django - 2.0.7. VESTA Control Panel with the wsgi. My Error ... '] = 'main.settings' -import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() I will be very happy for any help.
asked Aug 24 Raphael Pacheco 4.9k points
1 vote
2 answers 133 views
133 views
Problem : I am very new to programming world. Currently I am trying to learn how to use the flexx to create the GUI website. This is my first program in flex. If I try to compile my code then it gives me below error: &ldquo;Attributeerror: type object 'ioloop' has no ... browser') #show it now in a broswer flexx.run() # enter the mainloop Why am I facing above error ? How can I fix above error?
asked Apr 9 morrisBson 3.2k points
0 votes
1 answer 153 views
153 views
Problem: I have recently started learning Python so I have only fundamental knowledge in Python. Please find below my Transaction class for your reference: class Transaction(object):     def __init__(self, company, num, price, date, is_buy):         self.company = company         ... Python and because of lockdown I am unable to find any help to fix this issue. So I am posting my question here.
asked Aug 10 Raphael Pacheco 4.9k points