Python:“全局名称‘时间’”未定义”
我正在为一位朋友用 python 编写一个愚蠢的程序,打印“我们是说‘Ni’的骑士!”。然后休眠3秒,然后打印“Ni!”使用 random
模块的 uniform()
方法以随机间隔二十次。这是我的代码:
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))
我尝试通过在解释器中输入 fromsillyimportknights_of_ni()
和 importsilly
来导入此模块,然后调用函数使用 knights_of_ni()
或 silly.knights_of_ni()
(分别),但我总是得到相同的异常:
NameError: global name 'time' is not defined
是什么导致了这个错误以及如何修复我的错误代码?
编辑:坦率地说,我也不确定我遇到了什么问题。第二天早上我运行了代码,效果很好。我发誓昨晚的代码产生了错误......无论如何,感谢您的见解。
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 random
module's uniform()
method. Here's my code:
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()
and import silly
, then calling the function with either knights_of_ni()
or silly.knights_of_ni()
(respectively), but I always get the same exception:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
那是不可能的。您的代码示例与产生该错误的代码不同。
也许您使用的是
time.sleep(..)
而不是sleep(..)
。您已完成from time import sleep
。要使用time.sleep(..)
表单,您必须导入时间
That's impossible. Your code example isn't the same as the code that produced that error.
Perhaps you had
time.sleep(..)
instead ofsleep(..)
. You have donefrom time import sleep
. To use thetime.sleep(..)
form you mustimport time
下面的这些代码都可以正常工作:
These code below both work properly:
为死尸帖子道歉,但我也遇到了这个问题,尽管方式略有不同。
我在 Apache 和 Python 下使用 mod_python 运行
time.time()
。如果我尝试加载带有time.time()
的页面,它将失败,并抱怨“全局名称'time'未定义”。但是,如果我通过 ssh 进入我的网络服务器并从命令行运行完全相同的方法,它就会起作用。最后,重新启动 Apache2 服务解决了问题。我不知道为什么这有帮助。我猜想该模块在某个时刻被卸载,然后就不会重新加载,尽管有 import time 命令。
这很奇怪,而且有点神秘。抱歉,我从未找到真正的原因,但希望这对下一个人有帮助。
Apologies for the necropost but I ran into this problem too though in a slightly different way.
I was running
time.time()
with mod_python under Apache and Python . If I attempted to load the page withtime.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.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.
It's strange and a bit mysterious. Sorry I never hunted down the actual cause but hopefully this helps out the next person.
通过在调用函数之前将其导入到另一个文件中,您只是导入了该函数的内容。该文件顶部的导入不会导入到您的其他文件中。您应该将两个导入都放入该函数中,使其看起来像这样:
这将验证您想要的导入在您调用该函数的位置是否可用。不用担心双重导入,因为 python 不允许这样做 - 如果在导入此函数的文件中导入时间,则不会重新导入
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:
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
耶路所说的话。我运行了你的确切代码并且它有效:
What Jerub said. I ran your exact code and it worked:
我已经得到答案了!我遇到了同样的问题,只需重新启动 Canopy 即可。我不太擅长 python 或理解计算机,但我的程序认为我仍然在某个地方调用“时间”,即使它不在代码中。
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.
在 jupyter 笔记本中使用 autoreload 时,我遇到了同样的错误,其中某些代码在后台线程上运行。删除自动重新加载/在线程运行时不更改文件为我解决了这个问题。
I had the same error when using autoreload in a jupyter notebook where some code was running on a background thread. Removing autoreload / not changing files while the thread was running fixed it for me.
如果你想使用 time.sleep(n) 那么你首先需要添加一条 import 语句
导入时间
If you want to use time.sleep(n) then you first need to add an import statement
import time