[Django]-Elementary, unresolved import from my_app.views in Django (PyDev)

1πŸ‘

βœ…

OK,looks like I solved this problem.
Issue was in the way I made this Django project in Eclipse.
I was used python virtualenv which makes a little enviroment with its own env/bin/python2.7 interpreter and this interpreter supposed to be selected in creating project dialog.
This Python have no Django preinstalled (of course), you have to install it if you want it.
I created this project in my env/ made with virtualenv but leave interpreter settings on β€œDefault” so PyDev was using python interpreter from system not env/bin/python2.7 and could not found env/django_bookmarks/bookmarks app on his PYTHONPATH which caused ImportError.
Conclusion is actually logical, if you make Django project in virtual enviroment you use python interpreter from virtual enviroment.
Thank you Paul Renton for your time.

πŸ‘€yujaiyu

2πŸ‘

You need to inform the Python interpreter to look one directory above.

Try a relative path import

from ..bookmarks.views import main_page

The β€˜..’ says to look a directory above to find the bookmarks package.

May I suggest to you a more robust pattern to accomplish this?

django_bookmarks/ #project
django_bookmarks/
    __init__.py
    settings.py
    urls.py
    wsgi.py
bookmarks/ #made with python manage.py startapp bookmarks
    __init__.py
    models.py
    test.py
    views.py
    urls.py # ADD another urls.py to your bookmarks app
manage.py

In django_bookmarks/urls.py

from django.conf.urls import patterns, include, url
# from bookmarks.views import main_page # Remove this



# This directs Django to the urls.py within the bookmarks app
urlpatterns = patterns('',
    (r'^$', include('bookmarks.urls'))
)

In bookmarks/urls.py

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



# This directs Django to the urls.py within the bookmarks app
urlpatterns = patterns('',
    (r'^$', views.main_page)
    # Now you can add more bookmark urls to match to bookmark views
)

This pattern is more maintainable and allows all the bookmark url patterns to live inside bookmarks/urls.py.

πŸ‘€Paul Renton

Leave a comment