bitmoe
bitmoe

Reputation: 391

Reference another class in python

I'm just starting out with Python for Google App Engine. I have a file notifications.py, and in here, I will be creating User entities, which are specified in users.py. How can I do this? I've tried import users, but I get an error: NameError: global name 'User' is not defined

Upvotes: 0

Views: 329

Answers (3)

Talisin
Talisin

Reputation: 2490

Oh, I just had this problem too! After you do:

import users

to get User you have to type users.User

Alternatively you could import it like:

from users import User

then reference it as just User but if you do it this way you'll have to list every bit from users that you want in the following format:

from users import User, Somthingelse, Somthing

If you're feeling super lazy and you don't want to type in any prefixes or list all the things you want, just type

from users import *

Upvotes: 2

David Robinson
David Robinson

Reputation: 78610

Instead of

import users

do

from users import User

Upvotes: 1

Daniil Ryzhkov
Daniil Ryzhkov

Reputation: 7596

# module.py
foo = "bar"
# main.py
import module
print foo # This will cause error because foo is not located in the current namespace
print module.foo # this will print "bar"
from module import foo # But you can import contents of module "module" in the current namespace

http://docs.python.org/tutorial/modules.html

Upvotes: 0

Related Questions