richban
richban

Reputation: 101

WSGI Application - sqlalchemy can't create_engine

I'm trying to build a small WSGI application with Werkzeug. I have followed this tutorial it's so simple and clear. However I encounter this error:

File "/Users/username/my_app/assigment2/app.py", line 38, in __init__
self.database_engine = create_engine(db_uri)
File "/Users/Data/lib/python3.4/site-packages/sqlalchemy/engine/__init__.py", line 386, in create_engine
return strategy.create(*args, **kwargs)
File "/Users/../lib/python3.4/site-packages/sqlalchemy/engine/strategies.py", line 51, in create
dialect_cls = u.get_dialect()
AttributeError: 'function' object has no attribute 'get_dialect'

I have imported all SqlAlchemy modules

from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import create_session, scoped_session

The application fails at the init of the app.

class App(object):
def __init__(self):
    local.app = self
    self.database_engine = create_engine(SQLALCHEMY_DATABASE_URI )
    self.jinja_env = Environment(loader=PackageLoader('app', '/templates'))
    self.dispatch = SharedDataMiddleware(self.dispatch, {'/static':  STATIC_PATH})

def init_database(self):
    metadata.create_all(self.database_engine)

def dispatch(self, environ, start_response):
    local.app = self
    request = Response(environ)
    local.url_= adapter = url_map.bind_to_environ(environ)
    #adapter = url_map.bind_to_environ(environ)
    try:
        endpoint, value = adapter.match()
        handler = getattr(views, endpoint)
        response = handler(request, **values)
    except HTTPException as e:
        response = e
    return ClosingIterator(response(environ, strat_response),
                            [session.remove, local_manager.cleanup])

def __call__(self, environ, start_response):
    return self.dispatch(environ, start_response)

the databse_uri looks like this

SQLALCHEMY_DATABASE_URI = 'postgresql://user:password@localhost:5432/name_db'

STATIC_PATH = path.join(path.dirname('/static'), 'static')

Why I can't create my app like this:

def make_app():
   my_app = app.App()
   return my_app

I am running on Gunicorn

gunicorn -b 127.0.0.1:9000 app:make_app

New error:

TypeError: make_app() takes 0 positional arguments but 2 were given

Upvotes: 0

Views: 483

Answers (1)

Messa
Messa

Reputation: 25191

This is wrong:

gunicorn -b 127.0.0.1:9000 app:App

What happens when there is a request:

app.App(environ, start_response)

But what you really want is:

your_app = app.App(path, db_uri)
your_app(environ, start_response)

Change gunicorn invocation to something like this:

gunicorn -b 127.0.0.1:9000 'app:App("/static/path", "postgresql://...")'

Upvotes: 1

Related Questions