3

I am using Python 3.7 and trying to learn datetime module from library, but I am getting AttributeError. code below:

import datetime

t = datetime.time(4, 20, 1)

# Let's show the different components
print(t)
print('hour  :', t.hour)
print('minute:', t.minute)
print('second:', t.second)
print('microsecond:', t.microsecond)
print('tzinfo:', t.tzinfo)

As run file I get this error AttributeError: module 'datetime' has no attribute 'time'

Please Help me to find where I am going wrong. Thanks

Amjad
  • 1,950
  • 5
  • 26
  • 41
  • 3
    Do you have a file in your working directory called "datetime.py"? – Daniel Roseman Sep 25 '18 at 09:23
  • Either your installation is broken or, as mentioned by Daniel, you have a file called `datetime` somewhere which is hiding the built-in modulo. What does `print(datetime.__file__)` output? – Giacomo Alzetta Sep 25 '18 at 09:24
  • According to Python 3.7 documentation, your code should work. https://docs.python.org/3/library/datetime.html I guess your problem has a different reason. – Sumedh Junghare Sep 25 '18 at 09:25
  • Your code seems to work: https://onlinegdb.com/SyHDJYDtX most likely a problem with your python or project files as the others have mentioned – bunbun Sep 25 '18 at 09:27
  • Your code works, so like said before, you probably check your project's others files. Try to isolate your code outside your project, and run your py file. If it works, check your project, if it doesn't, check your python conf – vieroli Sep 25 '18 at 09:29

1 Answers1

14

The problem may be that your python file has the same name as the datetime module, maybe you called your program file datetime.py.

You need to rename your program file and delete any eventual .pyc file generated in your path, then you can import the correct datetime module.

bhansa
  • 7,282
  • 3
  • 30
  • 55
m.rp
  • 718
  • 8
  • 22