Search code examples
pythonpyramidmako

Split models into multiple files Python import package/subpackage


I am receiving the following error:

Error: ImportError: No module named models.account

I am trying to split up my models.

Here is my project structure:

site
  | site
  |  | models
  |  |  | __init__.py
  |  |  | account.py
  |  | views
  |     | __init__.py
  |     | site.py
  | __init__.py
  | resources.py
  | routes.py
  | security.py

site/site/views/site.py

from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound
from site.models.account import User


@view_config(context='pyramid.httpexceptions.HTTPForbidden',
         renderer='generic/login.mako')
@view_config(route_name='generic_login', renderer='generic/login.mako')
def login(request):
  if request.scheme == 'http':
    request.scheme = 'https'
    #return HTTPFound(location=request.url)

  if 'form.submitted' in request.params:
    uemail = request.params['email']
    pw = request.params['pass']

    user = User.objects(email=uemail).first()

  return {}

Path

/Volumes/workspace/py/website/site/bin
/Volumes/workspace/py/website/site/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Volumes/workspace/py/website/site/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg
/Volumes/workspace/py/website/site/lib/python2.7/site-packages/WebError-0.10.3-py2.7.egg
/Volumes/workspace/py/website/site/lib/python2.7/site-packages/Pygments-1.6-py2.7.egg
/Volumes/workspace/py/website/site/lib/python2.7/site-packages/Tempita-0.5.1-py2.7.egg
/Volumes/workspace/py/website/site/site
/Volumes/workspace/py/website/site/lib/python2.7/site-packages/WebHelpers-1.3-py2.7.egg
/Volumes/workspace/py/website/site/lib/python27.zip
/Volumes/workspace/py/website/site/lib/python2.7
/Volumes/workspace/py/website/site/lib/python2.7/plat-darwin
/Volumes/workspace/py/website/site/lib/python2.7/plat-mac
/Volumes/workspace/py/website/site/lib/python2.7/plat-mac/lib-scriptpackages
/Volumes/workspace/py/website/site/lib/python2.7/lib-tk
/Volumes/workspace/py/website/site/lib/python2.7/lib-old
/Volumes/workspace/py/website/site/lib/python2.7/lib-dynload
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Volumes/workspace/py/website/site/lib/python2.7/site-packages

I'am new to python so trying figure this out.


Solution

  • site is the name of one of standard Python modules, which is, to make matters worse, is a special case and is imported automatically during interpreter initialization. This is likely to be the cause of the problem if the system site module overrides yours.

    You may try to work around the problem by using something like

    from ..models.account import User
    

    but ultimately it would be worthwhile to rename your project.

    Another common problem of confusing import errors in Python is circular module dependencies - say, if you have

    from site.views import blah
    

    somewhere in your site.models module, and at the same time trying to do from site.models.account import User from site.views that would result in a circular dependency which basically manifests in anything below the offending import statement to be not defined. If you have trouble finding a circular import please update your question listing all places where site.models and site.views are imported.