0

''''

from datetime import datetime
now = datetime.now().time()
print(now)

o/p: 21:44:22.612870

'''' But, when i am trying: ''''

import datetime
now = datetime.now().time()
print(now)

''''

it give following error: Traceback (most recent call last): File "D:/3. WorkSpace/3. Django/datemodel/first.py", line 9, in now = datetime.now().time() # time object AttributeError: module 'datetime' has no attribute 'now'

any one explain what is difference between both?

  • [datetime module](https://docs.python.org/3/library/datetime.html?highlight=replace#available-types) contains different types, `date`, `time` and `datetime` amongst others. The module name is the same as one of the types it defines, hence `from datetime import datetime` and `import datetime` are not importing the same thing. – dirkgroten Apr 06 '20 at 16:20
  • Does this answer your question? ["import datetime" v.s. "from datetime import datetime"](https://stackoverflow.com/questions/15707532/import-datetime-v-s-from-datetime-import-datetime) – andreis11 Apr 07 '20 at 13:08

1 Answers1

0

The datetime library exports a module called datetime.

Modules are Python .py files that consist of Python code. Any Python file can be referenced as a module.

if you want you can also use it this way:

import datetime
datetime.datetime.now() 
Bogdan Veliscu
  • 641
  • 6
  • 11