6

This works in the Python 3.3.2 Shell

Inside the Python 3.3.2 Shell

>>> import datetime
>>> print(datetime.datetime.utcnow())
2013-07-09 19:40:32.532341

That's great! I then wrote a simple text file named "datetime.py"

Inside Datetime.py

#Date time
import datetime
print(datetime.datetime.utcnow())
#Prints GMT, which is named Universal Coordinated Time
# Which is UTC because in French it's something like
# Universahl Tyme Coordinatay
#Outputs something like 2013-07-09 15:15:19.695531

Proving that the file exists

C:\Python33\myscripts>ls
__pycache__  ex1.out  ex2.out  ex3.py    helloworld.py              read1.py
datetime.py  ex1.py   ex2.py   first.py  pythonintoimportexport.py  test.py

Here is where it gets mysterious!

C:\Python33\myscripts>python datetime.py
Traceback (most recent call last):
  File "datetime.py", line 2, in <module>
    import datetime
  File "C:\Python33\myscripts\datetime.py", line 3, in <module>
    print(datetime.datetime.utcnow())
AttributeError: 'module' object has no attribute 'utcnow'

Question

Why does the same code work in the Python Shell, but not when run as a script?

Rentsy
  • 378
  • 3
  • 12
  • 8
    Try changing your filename to something else. – Sukrit Kalra Jul 09 '13 at 20:04
  • *Never* use names of built-ins. This is true for both filenames but also for class/functions/identifier names. As you already experienced not following this advice *will* create problems. – Bakuriu Jul 09 '13 at 20:24

4 Answers4

11

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:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

Simply change the name of datetime.py to something else.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

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
danodonovan
  • 19,636
  • 10
  • 70
  • 78
2

Never use filenames same as module names. Change filename to something else apart from datetime.py .

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
0

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).

Travis DePrato
  • 392
  • 3
  • 9