使用Pylons + Python无法访问__init__.py中的全局函数

时间:2010-03-02 04:09:31

标签: python pylons

我在创建可从所有类中访问的全局函数时遇到问题。我在user.py中收到一条错误消息:

NameError: global name 'connectCentral' is not defined

这是我目前的代码。

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)

项目/模型/ user.py

    import project.model

    class User(object):
        def emailExists(self):
            try:
                connectCentral()
                emails = 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

我错过了一个导入?有更好的方法吗?

感谢。

1 个答案:

答案 0 :(得分:2)

您需要使用其所在的模块(或包)来限定名称,因此:

        try:
            project.model.connectCentral()