dir dir __init__ py dir __init__ py mod py container on module search path the __init__ py files can contain python codejust like normal module files their names are special because their code is run automatically the first time python program imports directoryand thus serves primarily as hook for performing initialization steps required by the package these files can also be completely emptythoughand sometimes have additional roles--as the next section explains as we'll see near the end of this the requirement of packages to have file named __init__ py has been lifted as of python in that release and laterdirectories of modules with no such file may be imported as single-directory namespace packageswhich work the same but run no initialization-time code file prior to python thoughand in all of python xpackages still require __init__ py files as described aheadin and later these files also provide performance advantage when used package initialization file roles in more detailthe __init__ py file serves as hook for package initialization-time actionsdeclares directory as python packagegenerates module namespace for directoryand implements the behavior of from ( from import *statements when used with directory importspackage initialization the first time python program imports through directoryit automatically runs all the code in the directory' __init__ py file because of thatthese files are natural place to put code to initialize the state required by files in package for instancea package might use its initialization file to create required data filesopen connections to databasesand so on typically__init__ py files are not meant to be useful if executed directlythey are run automatically when package is first accessed module usability declarations package __init__ py files are also partly present to declare that directory is python package in this rolethese files serve to prevent directories with common names from unintentionally hiding true modules that appear later on the module search path without this safeguardpython might pick directory that has nothing to do with your codejust because it appears nested in an earlier directory on the search path as we'll see laterpython ' namespace packages obviate much of module packages