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

#File: handle_input.py

class handle_input:
...

The file defining the class...

#File: control.py

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

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()
input.get_command()

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.

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



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

    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
                '''
                
        self.get_command()
        
    def show_used(self):
        print '\nThe cards that have already been dealt are\n',self.card_handle.used
        
        self.get_command()
        
    def show_hit_count(self):
        print '\nYou have received',self.play.hit_count,'hits'
        
        self.get_command()
    
    def show_count(self):
        print '\nThe current count is',self.count_handle.count
        
        self.get_command()
    
    def show_value(self):
        print '\nYour current hand is worth is',self.card_handle.value
        
        self.get_command()
        
    def show_dealer(self):
        print '\nDealer:',self.play.dealer_cards
        print '\nDealer Score:',self.card_handle.dealer_score
        
        self.get_command()

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.