Wednesday, December 28, 2016

I continued to work for #1285, but did not yet commit. Details later.

About packages

James asked:

As I work through the tutorials, I like to see with my own eyes the module and class/attribute that I am inheriting from calling or assigning. However, I went to the right place, but the files seem to be missing. For example, in Django tutorial Part 1:

from django.conf.urls import include urls

So I go to Github django code and I find:

What I find is that urls is a directory with only files : __init__.py, i18n.py and static.py. There is no urls.py file which might have url() or include() methods.

Same with models.Models. I see some code line from django.db import models and on django Github site I navigate until django/django/db/models. I see that models is a directory, not a file with a class Model.

So, what am I missing here?

What you are missing is related to packages. Admittedly the corresponding doc section is not very clear about your problem. I make a summary:

  • A package is a module which can contain submodules
  • A package consists of a directory containing a file __init__.py and optinally other .py files (which are then submodules of that package).
  • Everything defined in the __init__.py file is considered part of the package.

This last point is important in your case. When using from package import item, the item can be either a submodule (or subpackage) of the package, or some other name defined in the package.

Rule of thumb : when you want to see the code which defines a.b.c, then it can be either in a file a/b/c.py or in a file a/b/c/__init__.py.