导入错误和PythonPath

时间:2010-11-06 23:58:44

标签: python django

我试图了解问题所在:

我正在尝试导入模块:

from main.models import Main
from django.contrib import admin

admin.site.register(Main)

然而,当我尝试点击管理网站时,我收到了一个django错误页面:

ImportError at /admin/
cannot import name Main

I noticed that it provides a dump of the **Python Path:**

Python Path:    ['/Users/brian/src/SampleApp/src/SampleApp', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Python/2.6/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode']

然而,我不明白的是这条Python路径在哪里设置?

执行export PYTHONPATH不会返回任何内容,因为它未在环境变量中设置。

我需要导入一个位于/Users/brian/src/SampleApp/src/SampleApp/main/models.py

的模块

由于

2 个答案:

答案 0 :(得分:4)

PYTHONPATH可以通过以下方式访问:

import sys
print sys.path

尝试一些调试就是使用:

from main import models
from django.contrib import admin

admin.site.register(models.Main)

并查看是否可以提供更多信息。

答案 1 :(得分:1)

__init__.py中有/Users/brian/src/SampleApp/src/SampleApp/main/吗?这是考虑用于搜索的main目录所必需的。空__init__.py即可。

您还可以验证Main是否已定义。简单的import main.models是否有效?