0

My project is based on Pyramid.

Pyramid's default scaffold is very simple: view.py, models.py, and so on.

I created some directories to replace them. For example:

view.py --> view directory

models.py --> model directory

Then I create a file named login.py:

from pyramid.httpexceptions import HTTPForbidden
from pyramid.response import Response
from pyramid.view import view_config
import logging

from ..model import (
    DBSession,
    DynUser,
    )

log = logging.getLogger(__name__)

def find_user(account):
    #user = None
    try:
        user = DBSession.query(DynUser).filter(DynUser.username==account).first()
        #one = DBSession.query(MyModel).filter(MyModel.name=='one').first()
    except ValueError:
        #log.warning("invalidate id %s input." % request.matchdict['id'])
        log.warning("invalidate id %s input.")
    except Exception:
        log.error("database error!")

    if not user:
        return HTTPForbidden()

    return dict(user=user)

I have imported DBSession. Why do I still get this error?

tip
--------------------------------------------------------------------------
Undefined variable from import: DBSession

Undefined variable from import: DBSession

DBSession Found at: dyncms.model.meta


DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
deadly
  • 1,194
  • 14
  • 24
robinson.L
  • 83
  • 2
  • 11

2 Answers2

0

You didn't mention it, but I'll assume you moved models.py to model/__init__.py.

from ..models only works if login.py is in a subdir (like login/login.py). If it's not the case, you should use from .models import.

madjar
  • 12,691
  • 2
  • 44
  • 52
  • In model directory __init__.py # package from .meta import DBSession from .meta import Base meta.py from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import ( scoped_session, sessionmaker, ) from zope.sqlalchemy import ZopeTransactionExtension DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) Base = declarative_base() – robinson.L Oct 10 '12 at 12:56
  • http://stackoverflow.com/questions/2112715/how-do-i-fix-pydev-undefined-variable-from-import-errors – robinson.L Oct 10 '12 at 13:29
  • Try running the code directly on the command line to make sure. If the problem is due to pydev, you should close this question. – madjar Oct 10 '12 at 18:32
0

In model directory

__init__.py 
# package
from .meta import DBSession
from .meta import Base
from .book import Book

meta.py
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import (
    scoped_session,
    sessionmaker,
    )

from zope.sqlalchemy import ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
robinson.L
  • 83
  • 2
  • 11