tags:

views:

41

answers:

2

Using pylons 0.9.7, I'm trying to make a function that connects to a database on demand. I'd like it to be accessible from all functions within all model classes.

In model/__init__.py, I have:

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

This function is accessible everywhere. However, when I try to run it from within a class specified in model/class.py, it returns:

NameError: global name 'connectCentral' is not defined

Do I have to do any kind of special import? Is there a better way to do this?

Thanks.

+1  A: 

Have you done import init ? Or rather from init import connectCentral?

If you did, that such name should be defined. If not, you can try writing global connectCentral in method's body, but I believe it is only for using global variables.

Are you sure, this modules is supposed to have name init.py rather than _init_.py? Could you post some more code, where you are trying to use your function?

EDIT

So you are having __init__.py, fine. Are you doing import from model import connectCentral? Don't you have any circural imports (like import from __init__.py in class.py and import from class.py in __init__.py)?

gruszczy
Hi -- I'm trying to use my function in a class called model/user.py. I believe __init__.py is correct as this is how pylons created the default template. It works when I do import __init__.
ensnare
Could you post some code from user.py and init.py?
gruszczy
+1  A: 
from model import connectCentral
Ignacio Vazquez-Abrams