This works in the Python 3.3.2 Shell
>>> import datetime
>>> print(datetime.datetime.utcnow())
2013
As @Sukrit Kalra says, don't use datetime.py
as your file name. Python is getting confused with which datetime
is which (and is importing itself!). Maybe;
$ mv datetime.py my_datetime.py
Naming your file datetime
causes Python to import the file you're running as a module. For example, look at sys.path
. Mine, for example, is ['', '/usr/lib/python3.3', ...]
, which means that Python looks FIRST in the current working directory (the ''
) for modules. And because anything that ends in .py can be imported as a module, it imports the script that you're actually running (which, if I'm not mistaken, actually causes it to be run twice, once as __main__
and once as a module).
Never use filenames same as module names. Change filename to something else apart from datetime.py
.
The problem is that file is recursively importing itself, instead of importing the built-in module datetime
:
Demo:
$ cat datetime.py
import datetime
print datetime.__file__
$ python datetime.py
/home/monty/py/datetime.pyc
/home/monty/py/datetime.pyc
This happens because the module is searched in this order:
Simply change the name of datetime.py
to something else.