Global function in __init__.py not accessible using Pylons + Python
I'm having trouble creating a global function accessible from within all classes. I receive an error from within user.py that says:
NameError: global name 'connectCentral' is not defined
Here is my current code.
project/model/__ init __.py:
"""The application's model objects"""
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy import engine_from_config
from pylons import config
import central
#Establish an on-demand connection to the central database
def connectCentral():
engine = engine_from_config(config, 'sqlalchemy.central.')
central.engine = engine
central.Session.configure(bind=engine)
project/model/user.py
import project.model
class User(object):
def emailExists(self):
try:
connectCentral()
email开发者_开发问答s = central.Session.query(User).filter_by(email=self.email).count()
if (emails > 0):
return False
else:
return True
except NameError:
self.errors['email'] = 'E-Mail not set'
return False
Am I missing an import? Is there a better way to do this?
Thanks.
You need to qualify the name with the module (or package) it's in, so:
try:
project.model.connectCentral()
etc.
精彩评论