Why does django contain a lot of "__init__.py"?

Many directories in the django project contain __init__.py, and I think that it will be used as initialization for something. Where is this used __init__.py?

+3
source share
2 answers

Python does not accept every subdirectory of each directory in sys.path, to be necessarily a package: only those that have a file with a name __init__.py. Consider the following shell session:

$ mkdir adir
$ echo 'print "hello world"' > adir/helo.py
$ python -c 'import adir.helo'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named adir.helo
$ touch adir/__init__.py
$ python -c 'import adir.helo'
hello world

? adir helo.py, import adir.helo . __init__.py adir, , Python , adir - , .

+10

. ?

__init__.py , , , :

from myapp.models import Something
+7

Source: https://habr.com/ru/post/1727024/


All Articles