I am getting import error even when I am following the tutorial http://www.django-rest-framework.org/tutorial/quickstart/ line by line.
from tutorial.quickstart import views
ImportError: No module named 'tutorial.quickstart'
where my urls.py file looks like
from django.conf.urls import url, include
from rest_framework import routers
from tutorial.quickstart import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
Note: I have the project in Rest_Tutorial folder which consist of virtual enviroment - env
and project tutorial
. This tutorial consist of quickstart
and tutorial
Change from tutorial.quickstart import views
to from quickstart import views
in both tutorial/url.py
file as well as tutorial/quickstart/views.py
file. This should work.
This is required as python automatically adds your current directory to sys path.
Just change tutorial.quickstart import views
to from quickstart import views
in both tutorial/url.py file as well as tutorial/quickstart/views.py file.
Make sure your tutorial.quickstart is in the same folder as your project. Also make sure it is unzipped ! Otherwise use a absolute path.
Hope it helps !
You should use from quickstart import views
instead of from tutorial.quickstart import views
if you use PyCharm or other IDE. And don't forget change code where use from tutorial.quickstart.[xxx] import [xxx]
.
In quickstart/views.py: from .serializers import UserSerializer, GroupSerializer in tutorial/urls.py:
from quickstart import views
That worked for me.
It works for me by modification tow importing files
from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
to
from quickstart.serializers import UserSerializer, GroupSerializer
from tutorial.quickstart import views
to
from quickstart import views
USE:
django-admin startproject tutorial .
INSTEAD of:
django-admin startproject tutorial
while you start the project.
It is mentioned in the documentation.(Read carefully)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With