这感觉就像其中一个人无法回答的问题,但无论如何我都会问。我有一个Django 1.6项目,当我尝试通过Django开发服务器访问我的网站的主页时引发导入错误:
ImportError at /
No module named photo
错误发生在我的一个观点顶部的这一行:
# apps/photos/views.py
from classes.photo import Photo
这是stacktrace:
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.6.2
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'apps.admin',
'apps.account',
'apps.home',
'apps.members',
'apps.messages',
'apps.payment',
'apps.photos',
'apps.signin',
'apps.profile',
'scripts',
'apps.try',
'favicon',
'django_nose',
'django_extensions')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.middleware.common.BrokenLinkEmailsMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/Users/me/venv/yearbook/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
101. resolver_match = resolver.resolve(request.path_info)
File "/Users/me/venv/yearbook/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "/Users/me/venv/yearbook/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/me/venv/yearbook/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "/Users/me/venv/yearbook/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
40. __import__(name)
File "/www/yearbook/conf/urls.py" in <module>
16. url(r'^photos/', include('apps.photos.urls')),
File "/Users/me/venv/yearbook/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
26. urlconf_module = import_module(urlconf_module)
File "/Users/me/venv/yearbook/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
40. __import__(name)
File "/www/yearbook/apps/photos/urls.py" in <module>
5. from apps.photos.views import ack_photos_uploaded
File "/www/yearbook/apps/photos/views.py" in <module>
11. from classes.photo import Photo
Exception Type: ImportError at /
Exception Value: No module named photo
通常这种类型的问题很容易解决,但这个问题令我感到困惑。我的“年鉴”项目中有很多应用程序,包括相关照片应用程序:
yearbook (Django project)
├── admin
├── apps (My various apps... see below)
├── bin
├── classes (photo.py in here)
├── conf (my settings.py and config files)
├── manage.py
├── scripts
├── static
└── templates
apps
├── __init__.py
├── __init__.pyc
├── account
├── home
├── members
├── messages
├── photos (<- problem occurs here)
├── profile
└── signin
classes / photo.py是Python模块,它包含Photo类以及操作照片所需的各种实例,静态和类方法。我使用的是类而不是模型,因为我不需要在我的数据库中存储与照片相关的信息。
# classes/photo.py
class Photo:
# instance variables and various methods here
pass
这个错误的奇怪之处在于我可以将“from classes.photo import Photo”添加到我的其他任何应用程序的视图中,但我没有收到此错误。实际上,我的“管理员”和“个人资料”应用程序会导入并广泛使用照片模块的Photo类。只有当我将它添加到我的照片view.py文件时才会发生。感觉就像是某种名称冲突,但我不知道这是怎么回事,因为我的类模块的路径是/www/yearbook/classes/photo.py但尝试导入它的视图在/ www / yearbook /apps/photos/views.py。类和apps / photos目录中都包含空__init__.py
个文件。
有谁知道问题可能是什么?这特别令人沮丧,因为这意味着我的照片管理应用程序无法使用我为支持管理照片而构建的照片类来操作照片。
谢谢!
答案 0 :(得分:1)
我发现了问题所在。我也碰巧在我的年鉴/ apps / photos目录中有一个classes.py文件。当我尝试从apps / photos / views.py中的classes.photo模块导入Photo时,Django似乎一直把它与我的年/类目录混淆。
答案 1 :(得分:-2)
尝试在文件夹类中添加__init__.py
。这样python就知道它是你项目的一部分。