gpt4 book ai didi

python - 名称错误 : name 'datetime' is not defined

转载 作者:IT老高 更新时间:2023-10-28 21:50:51 26 4
gpt4 key购买 nike

我正在自学 Python,只是在“探索”。 Google 说 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
>>>

最佳答案

您需要导入模块datetime第一:

>>> import datetime

之后就可以了:

>>> import datetime
>>> date = datetime.date.today()
>>> date
datetime.date(2013, 11, 12)

关于python - 名称错误 : name 'datetime' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19934248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com