• Register
0 votes
1.5k 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 11 views
11 views
Problem: type object 'datetime.datetime' has no attribute 'datetime'
asked Feb 17 Dan phillip 1.9k points
0 votes
1 answer 50 views
0 votes
1 answer 738 views
738 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 159 views
159 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
1 vote
2 answers 352 views
352 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, 2020 morrisBson 3.2k points
0 votes
1 answer 724 views
724 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 14 views
14 views
Problem: I want to count the number of times a word is being repeated in the review string I am reading the csv file and storing it in a python dataframe using the below line reviews = pd.read_csv("amazon_baby.csv") The code in the below lines work when I apply it to a single review. print reviews["review"][1] a = reviews["review"][1].split("disappointed") print a b = len(a) print b
asked 2 days ago Mashhoodch 9.1k points
0 votes
1 answer 7 views
7 views
Problem: The code is the following (I am new to Python/Mysql): import mysql.connector conn = mysql.connector.connect(host='localhost',user='user1',password='puser1',db='mm') cursor = conn.cursor() string1 = 'test1' insert_query = """INSERT INTO items_basic_info (item_name) VALUES (%s)""", (string1) cursor.execute(insert_query) conn.commit() When I run this code I get this error:
asked 4 days ago Mashhoodch 9.1k points
0 votes
1 answer 11 views
11 views
Problem: attributeerror: 'series' object has no attribute 'reshape'
asked Feb 18 charles mathews 3.8k points
0 votes
1 answer 13 views
13 views
Problem: attributeerror: 'dataframe' object has no attribute 'sort'
asked Feb 18 charles mathews 3.8k points