NameError:名称“ datetime”未定义
问题描述:
我正在自学Python,只是在探索。 Google说datetime是一个全局变量,但是当我尝试在终端中查找今天的日期时,我在问题标题中收到NameError吗?
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
>>>
答
您需要导入模块 datetime
首先:
You need to import the module datetime
first:
>>> import datetime
在此之后它起作用:
>>> import datetime
>>> date = datetime.date.today()
>>> date
datetime.date(2013, 11, 12)