简体   繁体   中英

Django cannot import app

Following the django-rest tutorial

app/urls.py:

from django.conf.urls import url, include
from rest_framework import routers
from app.abbr import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

Directory structure:

在此处输入图片说明

Error:

File "..../app/app/urls.py", line 3, in from app.abbr import views ImportError: No module named 'app.abbr'

So, sigh...

It would have been useful if you pointed to the tutorial that showed you to do this.

You should not import from app ; that refers to the inner directory containing your urls.py. Just import from abbr .

from abbr import views

And what if you change import like this? from app.app.abbr import views ?

I am considering that you are using django 1.9 +

Try this

from . import views

The root directory folder named App in your case is named after your project name by default when you start a new project via the django-admin startproject command.

you can rename your root directory folder to whatever you want and it won't affect your project.

when in your code are importing from app, it is actually looking inside the 'app' folder containig the 'settings.py' file.

the django-rest tutorial you are following contains an error when they are doing from tutorial.quickstart import views which should be from quickstart import views

so the same goes for you, you should do from abbr import views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM