Skip to content
Advertisement

“from . import views”: Unresolved import

I’m following the Django 1.8 tutorial. In my project mysite, there is a source folder polls. In the folder there is views.py module where a index function is defined. And there is a urls.py file:

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

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

This is what the tutorial suggests, and Django works well with the codes. However Eclipse (PyDev) complains of unresolved imports for views. If I remove from . import views and use auto-correction function, PyDev would recommend import views, now Django complains that “the name views is not defined“. I tried with and without __init__.py in the folder, both give the same results.

I am using the latest version for both Django (1.8) and PyDev (4.2).

Thanks!

Advertisement

Answer

The project was created with the PyDev wizard as a Django project. When it was created, the folder polls is not a source folder. As a result, no code analysis was performed. So I changed the folder polls (which is inside the project folder mysite) to a source folder. Now the code was analyzed and unresolved import error was raised.

The fix is to change polls back to a normal folder (removed from PYTHONPATH), and instead set the top-level project folder mysite as a source folder. Now both PyDev and Django work well.

Advertisement