ImportError at / No module named urls

Posted

技术标签:

【中文标题】ImportError at / No module named urls【英文标题】: 【发布时间】:2012-08-12 06:19:06 【问题描述】:

我在 virtualenv 下创建了一个基本的 Django 安装,启动了一个项目并启用了管理员和管理员文档。当我运行服务器时,出现以下错误:

Request URL: http://127.0.0.1:8000/

Django Version: 1.4.1
Python Version: 2.7.2
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')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Users/farneman/Projects/project/project_env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  101.                             request.path_info)
File "/Users/farneman/Projects/project/project_env/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  298.             for pattern in self.url_patterns:
File "/Users/farneman/Projects/project/project_env/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  328.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/farneman/Projects/project/project_env/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  323.             self._urlconf_module = import_module(self.urlconf_name)
File "/Users/farneman/Projects/project/project_env/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)

Exception Type: ImportError at /
Exception Value: No module named urls

我的 urls.py 文件看起来像:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'projectname.views.home', name='home'),
    # url(r'^projectname/', include('projectname.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include('admin.site.urls')),
)

我的 settings.py 文件的已安装应用部分如下所示:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.admindocs',
)

我在这里和其他地方查看了许多其他帖子,但找不到解决方案。知道发生了什么吗?

【问题讨论】:

您是否将项目名称添加到INSTALLED_APPS 【参考方案1】:

在您的 settings.py 文件中,确保 ROOT_URLCONF 正确。

它应该看起来像:

ROOT_URLCONF = '<INSERT PROJECT NAME>.urls'

根据您提供的内容判断,您可能希望确保它看起来像:

ROOT_URLCONF = 'projectname.urls'

【讨论】:

以上是关于ImportError at / No module named urls的主要内容,如果未能解决你的问题,请参考以下文章

Django + mod_wsgi + apache:ImportError at / No module named djproj.urls

ImportError: No module named PIL

Python ImportError: No module named Image

ImportError : No module named graphics

python27 ImportError: No module named site

Python中ImportError: No module named request