Cannot import name views

Macuser picture Macuser · Sep 21, 2014 · Viewed 26.9k times · Source
ImportError at /

cannot import name views

Request Method:     GET
Request URL:    http://127.0.0.1:8000/
Django Version:     1.7
Exception Type:     ImportError
Exception Value:    

cannot import name views

Exception Location:     /Users/adam/Desktop/qblog/qblog/urls.py in <module>, line 1
Python Executable:  /Users/adam/Desktop/venv/bin/python
Python Version:     2.7.8
Python Path:    

['/Users/adam/Desktop/qblog',
 '/Users/adam/Desktop/venv/lib/python27.zip',
 '/Users/adam/Desktop/venv/lib/python2.7',
 '/Users/adam/Desktop/venv/lib/python2.7/plat-darwin',
 '/Users/adam/Desktop/venv/lib/python2.7/plat-mac',
 '/Users/adam/Desktop/venv/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/adam/Desktop/venv/lib/python2.7/lib-tk',
 '/Users/adam/Desktop/venv/lib/python2.7/lib-old',
 '/Users/adam/Desktop/venv/lib/python2.7/lib-dynload',
 '/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/adam/Desktop/venv/lib/python2.7/site-packages']

Server time:    Sun, 21 Sep 2014 15:12:22 +0000

Here is urls.py located in qblog/qblog/:

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


urlpatterns = patterns(
    '',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^markdown/', include('django_markdown.urls')),
    url(r'^', include('blog.urls')),
)

Also, if I add "library" to the first import statement (which I don't need) it will give me the same error but with library, "Cannot import name library".

Here is urls.py located in qblog/blog/:

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

urlpatterns = patterns(
    '',
    url(r'^$', views.BlogIndex.as_view(), name="index"),
)

Going to the url http://127.0.0.1:8000/index provides the same error.

I do not get any errors in the terminal when running ./manage.py runserver

Project structure:

.
├── blog
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── 0002_auto_20140921_1414.py
│   │   ├── 0002_auto_20140921_1414.pyc
│   │   ├── 0003_auto_20140921_1501.py
│   │   ├── 0003_auto_20140921_1501.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── db.sqlite3
├── manage.py
├── qblog
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── static
│   ├── css
│   │   ├── blog.css
│   │   └── bootstrap.min.css
│   ├── icons
│   │   └── favicon.ico
│   └── js
│       ├── bootstrap.min.js
│       └── docs.min.js
└── templates
    ├── base.html
    ├── home.html
    └── post.html

Answer

Daniel Roseman picture Daniel Roseman · Sep 21, 2014

There is no need to import the views in your project-level file. You are not using them there, so no reason to import them.

If you did need to, you would just to from blog import views, because the views are in the blog directory and manage.py puts the top-level directory into the Python path.