2012-04-15 60 views
0

我剛剛開始使用Python for Google App Engine。我有一個文件notifications.py,在這裏,我將創建User.py中指定的用戶實體。我怎樣才能做到這一點?我試過import users,但我得到一個錯誤:NameError: global name 'User' is not defined引用Python中的另一個類

+2

http://docs.python.org/tutorial/modules html的 – 2012-04-15 02:58:22

回答

2

哦,我只是有這個問題太!在此之後:

import users 

得到User你必須鍵入users.User

另外,您可以導入它喜歡:

from users import User 

然後引用它只是User,但如果你做這種方式您必須按照以下格式列出您想要的用戶的所有位:

from users import User, Somthingelse, Somthing 

如果你感覺超級懶惰,你不想在鍵入任何前綴或列出所有你想要的東西,只需鍵入

from users import * 
0
# 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

1

而不是

import users 

from users import User 
相關問題