我在教自己Python,只是在“探索”。谷歌说datetime是一个全局变量但是当我尝试在终端中找到今天的日期时,我在问题标题中收到了NameError?
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
>>>
答案 0 :(得分:70)
您需要先导入模块datetime
:
>>> import datetime
之后它起作用:
>>> import datetime
>>> date = datetime.date.today()
>>> date
datetime.date(2013, 11, 12)
答案 1 :(得分:0)
它也可以如下使用:
from datetime import datetime
start_date = datetime(2016,3,1)
end_date = datetime(2016,3,10)