Reply To: why __init__.py is required in your path ??

Home Forums Python Programming why __init__.py is required in your path ?? Reply To: why __init__.py is required in your path ??

#2121
Humble
Keymaster

From : http://docs.python.org/2/tutorial/modules.html#SECTION008400000000000000000

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

Yes, normally when you try to import a package , python look for the below path:

>>> sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
>>> 




If your package is in some other directory in your filesystem you can make use of __init__.py to tell python that , the directory is python package.. 

Well, in one of the earlier topics I mentioned you have to tweak PYTHONPATH variable for the same purpose.. Then why you need __init__.py in your new directory ?

Suppose you have a directory called  mypackages inside your home directory. inside that submodules , and then module1, module2 directories..etc..

.
.....
└── submodules
    ├── __init__.py
    ├── module1
    │   ├── __init__.py
    │   └── module1.py
    └── module2
        ├── __init__.py
        └── module2.py



You have added ~/mypackage to your PYTHONPATH variable , so python will look into this path when trying to import a module.. You have __init__.py in the path, so python will be able to import modules from the same.. 

>>> from mypackage.submodules import module1
>>> module1
<module 'mypackage.submodules.module1' from 'mypackage/submodules/module1/__init__.pyc'>



__init__.py can be an empty file , but at the same time, it is used for doing lots of startup work as well.. 


Now, lets look into unittest module and its __init__.py file:

[Not a complete one though]
..............
__all__ = ['TestResult', 'TestCase', 'TestSuite',
           'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
           'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
           'expectedFailure', 'TextTestResult', 'installHandler',
           'registerResult', 'removeResult', 'removeHandler']

# Expose obsolete functions for backwards compatibility
__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])

__unittest = True

from .result import TestResult
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
                   skipUnless, expectedFailure,
                   _skipInRpmBuild, _expectedFailureInRpmBuild)
from .suite import BaseTestSuite, TestSuite
from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames,
                     findTestCases)
from .main import TestProgram, main
from .runner import TextTestRunner, TextTestResult
from .signals import installHandler, registerResult, removeResult, removeHandler