Python:“未定义全局名称‘时间’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3421290/
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 10:59:30  来源:igfitidea点击:

Python: "global name 'time' is not defined"

pythontime

提问by Rafe Kettler

I'm writing a silly program in python for a friend that prints "We are the knights who say 'Ni'!". then sleeps for 3 seconds, and then prints "Ni!" twenty times at random intervals using the randommodule's uniform()method. Here's my code:

我正在用 python 为一个朋友写一个愚蠢的程序,上面写着“我们是说 'Ni' 的骑士!”。然后休眠 3 秒,然后打印“Ni!” 使用random模块的uniform()方法以随机间隔进行 20 次。这是我的代码:

from time import sleep
import random

def knights_of_ni():
    generator = random.Random()
    print "We are the knights who say 'ni'."
    sleep(3)
    for i in range(0,20):
        print "Ni!"
        sleep(generator.uniform(0,2))

I've tried to import this module by typing in the interpreter from silly import knights_of_ni()andimport silly, then calling the function with either knights_of_ni()or silly.knights_of_ni()(respectively), but I always get the same exception:

我试图通过在解释器中输入from silly import knights_of_ni()and来导入这个模块import silly,然后用knights_of_ni()silly.knights_of_ni()(分别)调用函数,但我总是得到相同的异常:

 NameError: global name 'time' is not defined

What is causing this error and how can I fix my code?

是什么导致了这个错误,我该如何修复我的代码?

Edit: quite frankly, I'm not sure what problem I was having either. I ran the code the next morning and it worked just fine. I swear that the code produced errors last night... Anyway, thanks for your insight.

编辑:坦率地说,我也不确定我遇到了什么问题。第二天早上我运行了代码,它运行得很好。我发誓昨晚的代码产生了错误......无论如何,感谢您的洞察力。

采纳答案by Jerub

That's impossible. Your code example isn't the same as the code that produced that error.

这不可能。您的代码示例与产生该错误的代码不同。

Perhaps you had time.sleep(..)instead of sleep(..). You have done from time import sleep. To use the time.sleep(..)form you must import time

也许你有time.sleep(..)而不是sleep(..). 你已经完成了from time import sleep。要使用该time.sleep(..)表格,您必须import time

回答by Katriel

What Jerub said. I ran your exact code and it worked:

耶鲁布所说的话。我运行了您的确切代码并且它有效:

>>> import silly
>>> silly.knights_of_ni()
We are the knights who say 'ni'.
Ni!
Ni!
Ni!
Ni!
Ni!
Ni!

回答by shaun m

Apologies for the necropost but I ran into this problem too though in a slightly different way.

为 necropost 道歉,但我也遇到了这个问题,但方式略有不同。

I was running time.time()with mod_python under Apache and Python . If I attempted to load the page with time.time()on it, it would fail complaining that "global name 'time' is not defined". However, if I ssh'd into my webserver and ran the exact same method from the command line, it would work.

time.time()在 Apache 和 Python 下使用 mod_python运行。如果我尝试加载页面time.time(),它会抱怨“未定义全局名称‘时间’”而失败。但是,如果我通过 ssh 连接到我的网络服务器并从命令行运行完全相同的方法,它将起作用。

In the end, restarting the Apache2 service fixed the issue. I'm not sure why this helped. I guess the module was unloaded at some point and then wouldn't reload, despite the import time command.

最后,重新启动 Apache2 服务解决了该问题。我不确定为什么这有帮助。我猜该模块在某个时候被卸载,然后不会重新加载,尽管导入时间命令。

It's strange and a bit mysterious. Sorry I never hunted down the actual cause but hopefully this helps out the next person.

这很奇怪,也有点神秘。抱歉,我从来没有追查过真正的原因,但希望这能帮助下一个人。

回答by Appie Kalac

I have got the answer! I had the same problem, just restart your Canopy. I am not that good at python or understanding computers, but my program thought i still called 'time' somewhere, even though it was not in the code.

我得到了答案!我遇到了同样的问题,只需重新启动 Canopy。我不太擅长 python 或理解计算机,但我的程序认为我仍然在某处调用了 'time',即使它不在代码中。

回答by Emily

By importing the function into another file before calling it, you're only importing the contents of that function. The imports at the top of that file are not imported into your other file. You should put both of your imports into the function so that it looks like this:

通过在调用之前将函数导入另一个文件,您只是在导入该函数的内容。该文件顶部的导入不会导入到您的其他文件中。您应该将两个导入都放入函数中,使其看起来像这样:

def knights_of_ni():
    from time import sleep
    import random
    <the function contents>

This will verify that the imports that you want are available in the location that you call the function. There is no worry of double importing because python doesn't allow that - if time is imported in the file where this function is imported, it doesn't re

这将验证您想要的导入在您调用该函数的位置是否可用。不必担心双重导入,因为 python 不允许 - 如果在导入此函数的文件中导入时间,则不会重新