Skip to Content
0 points
over 7 years

ImportError: cannot import name datetime

from datetime import datetime

now = datetime.now()
yr = now.year
mn = now.month
day = now.day
hr = now.hour
mnt = now.minute
sec = now.second

print str(day) + "/" + str(mn) + "/" + str(yr), \
        str(hr) + ":" + str(mnt) + ":" + str(sec)

when i try to run this code in console, this pops out:

Traceback (most recent call last):
  File "datetime.py", line 1, in <module>
    from datetime import datetime
  File "/home/pr33/code/py/v2/datetime.py", line 1, in <module>
    from datetime import datetime
ImportError: cannot import name datetime

tried this piece of code in default interactive python in my ubuntu and in codecademy lab, its totally ok, but still dont understand why, it cannot import name datetime, my py version is Python 2.7.4,

Answer 54635b6752f863369d0025b3

1 vote

Permalink

that’s because you use name “datetime.py” for you script make new .py file with different name

90 points
Submitted by Masoud nasiri
over 6 years

Answer 52401a82abf82112c40019f0

0 votes

Permalink

Try simply:

import time
import datetime
1456 points
Submitted by Lucien
over 7 years

Answer 5240986280ff3333d00050ca

0 votes

Permalink

@Lucien:

Traceback (most recent call last):
  File "datetime.py", line 2, in <module>
    import date
ImportError: No module named date

:(

841 points
over 7 years

4 comments

Lucien over 7 years

What operating system did you install it on and how?

“current -> ubuntu 13.04, gnome desktop environment; upgraded from -> ubuntu 12.04, installed from live USB, but Y?”

Lucien over 7 years

I mean’t how did you install Python :) Because something like this seems like an installation issue.

i’m extremely sorry… had a rough day… it was pre-installed…! i think i should have a restart… me with my pc as well :P c ya…