tags:

views:

108

answers:

1

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()
                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

Am I missing an import? Is there a better way to do this?

Thanks.

+1  A: 

You need to qualify the name with the module (or package) it's in, so:

        try:
            project.model.connectCentral()

etc.

Alex Martelli
When I do this, I get:NameError: name 'project' is not defined
ensnare
Weird, the `import project.model` you have at the start should define it. Try `import project.model as prom` instead, then use `prom.connectCentral()` -- what does it say then?
Alex Martelli
Weird ... from project.model import * works but import project.model as proj yields AttributeError: 'module' object has no attribute 'model'
ensnare
Does directory `project` have an `__init__.py` too? What does it contain, exactly?
Alex Martelli
No, there is no __init__.py in project/ ... only in project/model/
ensnare
Then make an empty `project/__init__.py`!
Alex Martelli