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

Asked

Viewed 2,267 times

8

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

What is the purpose of these files?

2 answers

6


It is part of a package. See the python documentation.

The archives __init__.py are required to make Python handle directories containing packages, this is done to avoid directories with common names, such as string, that unintentionally hides in the validated modules, those that will be searched in the path. In the simplest case, the file __init__.py can only be a empty file, but it also executes the initialization code for the package or set variable __all__, described later.

2

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

diretoria/
    __init__.py
    ficheiro_a.py
    ficheiro_b.py
    ficheiro_c.py

Browser other questions tagged

You are not signed in. Login or sign up in order to post.