Python 时间增量未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16782682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:41:03  来源:igfitidea点击:

Timedelta is not defined

pythonpython-2.7nameerror

提问by Mitchell

Below is the code I am working on. From what I can tell there is no issue, but when I attempt to run the piece of code I receive an error.

下面是我正在处理的代码。据我所知,没有问题,但是当我尝试运行这段代码时,我收到一个错误。

import os 
import datetime

def parseOptions():

    import optparse
    parser = optparse.OptionParser(usage= '-h')
    parser.add_option('-t', '--type', \
                      choices= ('Warning', 'Error', 'Information', 'All'), \
                      help= 'The type of error',
                      default= 'Warning')
    parser.add_option('-g', '--goback', \
                      type= 'string')
    (options, args) = parser.parse_args()
    return options

options = parseOptions() now = datetime.datetime.now() subtract = timedelta(hours=options.goback) difference = now - subtract

if options.type=='All' and options.goback==24:
    os.startfile('logfile.htm')

else: 
    print
    print 'Type =', options.type,
    print
    print 'Go Back =', options.goback,'hours'
    print difference.strftime("%H:%M:%S %a, %B %d %Y")
    print

Error is as followed:

错误如下:

Traceback (most recent call last):
  File "C:\Python27\Lib\SITE-P~1\PYTHON~2\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\user\Desktop\Python\python.py", line 19, in <module>
    subtract = timedelta(hours=options.goback)
NameError: name 'timedelta' is not defined

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Peter DeGlopper

You've imported datetime, but not defined timedelta. You want either:

您已导入日期时间,但未定义时间增量。你想要:

from datetime import timedelta

or:

或者:

subtract = datetime.timedelta(hours=options.goback)

Also, your goback parameter is defined as a string, but then you pass it to timedelta as the number of hours. You'll need to convert it to an integer, or better still set the typeargument in your option to intinstead.

此外,您的 goback 参数被定义为一个字符串,但随后您将其作为小时数传递给 timedelta。您需要将其转换为整数,或者最好还是将type选项中的参数设置为int

回答by jamylak

It should be datetime.timedelta

它应该是 datetime.timedelta

回答by Zamphatta

Where you have timedelta, you need to put datetime.before it, so it's actually datetime.timedelta

你有的地方timedelta,你需要放在datetime.它之前,所以它实际上是datetime.timedelta

回答by FisherMartyn

I find this problem may occur in some of my sutpid action. I create a file named datetime.py and even I rename the file, I still leave the datetime.pyc in the folder... So every file that import dateime will use this file and timedelta cannot be found. After I delete the file:datetime.pyc, It works.

我发现这个问题可能出现在我的一些愚蠢的行动中。我创建了一个名为 datetime.py 的文件,即使我重命名了该文件,我仍然将 datetime.pyc 保留在文件夹中...因此每个导入 dateime 的文件都将使用该文件,并且找不到 timedelta。在我删除文件:datetime.pyc 后,它起作用了。