• Register
0 votes
1.8k 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

Please log in or register to answer this question.

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 58 views
58 views
Problem : I am currently working on a django and I have gotten the below error: &ldquo;Type object 'datetime.datetime' has no attribute 'datetime'&rdquo;
asked Mar 14 Wafa Abu Yousef 6.1k points
1 vote
1 answer 23 views
23 views
Problem: I am getting this error: Type object 'datetime.datetime' has no attribute 'timedelta'.
asked May 1 PkGuy 23.5k points
0 votes
1 answer 101 views
101 views
Problem: type object 'datetime.datetime' has no attribute 'datetime'
asked Feb 17 Dan phillip 4.8k points
0 votes
1 answer 6 views
6 views
Problem: What is wrong here? >Attributeerror: module 'datetime' has no attribute 'now'
asked 3 days ago Sawyer Parviz 13.4k points
0 votes
1 answer 12 views
12 views
Problem: This thing is bugging me, please help > Attributeerror: 'dataframe' object has no attribute 'dtype'
asked 5 days ago Irenka Kiri 17.9k points
0 votes
1 answer 299 views
299 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 6 views
6 views
Problem: I got error on my django rest framework, I am running it on windows 10 OS. this is the entire error: Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "c:\django\django\django\core\ ... api-auth/', include('rest_framework.urls', namespace='rest_framework')) ] does anyone have an idea about my situation? thanks in advance...
asked Apr 30 Yeamin 22k points
0 votes
1 answer 77 views
0 votes
1 answer 866 views
866 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, 2020 mphil 2.3k points
0 votes
1 answer 29 views
29 views
Problem: Any idea how do I fix this Flask self.errors.append() - AttributeError: 'tuple' object has no attribute 'append'
asked Mar 16 ummesalma 29.2k points