Understanding python imports

后端 未结 3 1777
有刺的猬
有刺的猬 2021-01-06 21:10

In the process of learning Django and Python. I can\'t make sense of this.

(Example Notes:\'helloworld\' is the name of my project. It has 1 app called \'app\'.)

3条回答
  •  一个人的身影
    2021-01-06 22:05

    from helloworld.views import *          # <<-- this works
    from helloworld import views            # <<-- this doesn't work
    from helloworld.app import views        # <<-- but this works.  why?
    

    #2 and #3 are not the same.

    The second one imports views from the package helloworld. The third one imports views from the package helloworld.app, which is a subpackage of helloworld. What it means is that views are specific to your django apps, and not your projects. If you had separate apps, how would you import views from each one? You have to specify the name of the app you want to import from, hence the syntax helloworld.app.

提交回复
热议问题