So I'm writing a small Blackjack game and divided a class up into 2 to keep everything tidy but keep getting this little error:
global name 'handle_input' is not defined
The code:
The class being called...


class handle_input:

The file defining the class...


from card import *
from count import *
from handle_input import *

class control:
    def __init__(self):
        self.card_handle = card()
        self.count_handle = count()
        self.input_handle = handle_input() #<< This is the 'undefined' error

I use similar code else where and it works fine, however its not in a class. I'm thinking that might be contributing to the error but as you can see I'm defining other instances there (which aren't causing errors) so it can't be the problem.

Any comments appreciated,

There doesn't seem to be an error. This kind of errors happen sometimes with IDE like idle when a module like 'handle_input' was previously loaded and then modified, but the modified version is not reloaded by idle.

However, since we don't know the context, I suggest for debugging that you replace from handle_input import * by from handle_input import handle_input . This way we can be sure that the global name handle_input exists.

Hm, now I'm getting
ImportError: cannot import name handle_input

What does that mean? Why can't it import it?

I'm pretty new to python so you'll have to excuse my ignorance :P

What? But there is, whats going on? Why doesn't python like me? :(
I have

from handle_input import *

input = handle_input()

On another page and it works just fine

In fact there is another possibilty, if the handle_input module contains a statement like this

__all__ = [ "hello", "world" ]

then only the symbols "hello" and "world" can be imported

One thing you can try is this:

import handle_input
print handle_input.__file__

It will print you the path from where the handle_input module was imported. Make sure it's the path to your file

It reads handle_input.pyc Is that 'c' supposed to be there?

The rest of the location is as it should be.

Attatched is the handle_input file, its a .txt because .py aren't accepted.

from card import *
from count import *
from control import *

class handle_input:
    def __init__(self): = control()
    def get_command(self):
        cmd = str(raw_input('\nWhat would you like to do?\n>>> '))
        if cmd == 'deal':
        elif cmd == 'hit':
        elif cmd == 'end':
        elif cmd == 'help':
        elif cmd == 'dealt':
        elif cmd == 'hits':
        elif cmd == 'count':
        elif cmd == 'score':
        elif cmd == 'dealer':
            print 'WTF is that?'

    def help(self):
        print   '''
                The available options are:
                1) 'deal' To begin the game
                2) 'hit' To receive a card
                3) 'end' To end the game
                4) 'dealt' To see the list of dealt cards
                5) 'hits' To see how many hits you have received
                6) 'count' To see the current count
                7) 'score' To see the value of your current hand
    def show_used(self):
        print '\nThe cards that have already been dealt are\n',self.card_handle.used
    def show_hit_count(self):
        print '\nYou have received',,'hits'
    def show_count(self):
        print '\nThe current count is',self.count_handle.count
    def show_value(self):
        print '\nYour current hand is worth is',self.card_handle.value
    def show_dealer(self):
        print '\nDealer:',
        print '\nDealer Score:',self.card_handle.dealer_score

I think I found it: if control imports handle_input, then handle_input must not import control so remove the

from control import *

Thats it! :)

It had to be something simple :P

Thanks for your help, I greatly appreciate it :)

This question has already been answered. Start a new discussion instead.