0

我收到以下错误:

Error: ImportError: No module named models.account

我正在尝试拆分我的模型。

这是我的项目结构:

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

站点/站点/视图/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 {}

小路

/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

我是 python 新手,所以想弄清楚这一点。

4

1 回答 1

0

site是标准 Python 模块之一的名称,更糟糕的是,它是一种特殊情况,会在解释器初始化期间自动导入。site如果系统模块覆盖您的模块,这可能是问题的原因。

您可以尝试通过使用类似的东西来解决这个问题

from ..models.account import User

但最终值得重命名您的项目。

在 Python 中混淆导入错误的另一个常见问题是循环模块依赖关系——比如说,如果你有

from site.views import blah

在你的site.models模块中的某个地方,同时尝试这样做from site.models.account import Usersite.views导致循环依赖,它基本上表现在有问题的 import 语句下面的任何内容中都没有定义。如果您在查找循环导入时遇到问题,请更新您的问题,列出所有导入的位置site.modelssite.views位置。

于 2013-02-11T01:28:12.180 回答