30

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.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 1
    It's really important for you to take a step back and understand properly how `import` works. Otherwise you will waste lots of time randomly changing your code trying to get it to work. Maybe you should ask another question about what you don't understand about `import` – John La Rooy Aug 06 '10 at 06:34
  • 2
    @gnibbler: I definitely understand the import command, you would use `import spam` to get a full module, then call each function as `spam.eggs()`, and you would use `from spam import eggs` to get a specific function and the `spam` namespace so you could call the function `eggs()`. – Rafe Kettler Aug 06 '10 at 16:30

8 Answers8

54

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

Jerub
  • 41,746
  • 15
  • 73
  • 90
  • Yeah I have no idea what happened, I'm certain this code produced an error last night, but no matter, it works now.... Thanks for convincing me to try running it again. – Rafe Kettler Aug 06 '10 at 16:32
  • Actually, I know exactly what happened.... I had done some things with `time` in the interpreter, and when I called `knights_of_ni` it must have gotten confused. – Rafe Kettler Aug 06 '10 at 16:33
  • 3
    6 years later, I got this same error message. And this is the exact mistake I had made. – SauceCode Nov 21 '16 at 23:50
4

These code below both work properly:

import time

time.sleep(3)
from time import sleep

sleep(3)
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
2

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

Emily
  • 119
  • 1
  • 3
  • 13
2

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 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.

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.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
shaun m
  • 440
  • 1
  • 5
  • 15
0

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!
Katriel
  • 120,462
  • 19
  • 136
  • 170
0

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.

0

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.

Rick
  • 138
  • 1
  • 10
0

If you want to use time.sleep(n) then you first need to add an import statement import time

PRao
  • 1,439
  • 1
  • 8
  • 2