What is the __init__.py file used for in modules in python?

6

I'm starting to work deeper with Python and have noticed many projects with __int__.py files in folders, and are often blank.

What is the purpose of these files?

    
asked by anonymous 05.11.2015 / 23:25

2 answers

5

It is part of a package. See Python documentation .

  

__init__.py files are required to make Python handle   the directories that contain the packages, this is done to avoid   directories with common names, such as string , that involuntarily   hidden in the valid modules, those that will be searched in the   way. In the simplest case, the __init__.py file can only be one   file, but it also runs the boot code for the   package or set the variable __all__ , described later.

    
06.11.2015 / 20:30
1

The __init__.py files are used to declare to the python interpreter that the directory they are in contains python files in the program. Example:

diretoria/
    __init__.py
    ficheiro_a.py
    ficheiro_b.py
    ficheiro_c.py
    
06.11.2015 / 10:14