
I'm teaching myself Python and was just "exploring". Google says that datetime is a global variable but when I try to find todays date in the terminal I receive the NameError in the question title?
mynames-MacBook:pythonhard myname$ python Enthought Canopy Python 2.7.3 | 64-bit | (default, Aug 8 2013, 05:37:06) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> date = datetime.date.today() Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'datetime' is not defined >>>
12 Answers
You need to import the module datetime
first:
>>> import datetime
After that it works:
>>> import datetime >>> date = datetime.date.today() >>> date datetime.date(2013, 11, 12)
1It can also be used as below:
from datetime import datetime start_date = datetime(2016,3,1) end_date = datetime(2016,3,10)
ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaHJxY2l%2FdYSOp5imnZWnv7C%2BjKeYpp1dma61sdOipJ5lmah6r7vTZpuenpmjsqU%3D