[Tutor] __init__.py question

Alexandre Conrad alexandre.conrad at gmail.com
Wed Jun 1 20:33:32 CEST 2011


2011/5/31 Marilyn Davis <marilyn at pythontrainer.com>:
> I don't really understand why __init__.py is necessary -- except that it
> makes the packaging scheme work.
>
> The Python Manual by Guido van Rossum and Fred L. Drake says:
>
> ... this is done to prevent directories with a common name, such as
> string, from unintentionally hiding valid modules that occur later on in
> the module search path.

I think it is just to avoids unexpected conflicting namespaces that
could happen by accidentally importing non-Python directories. By
explicitly putting a __init__.py module in your directory, you are
telling Python you want your directory to be considered as a Python
package and to be imported in your namespace. If you have a
conflicting namespace, it means you explicitly wanted this to happen,
then you have to deal with it (or fix it if unintended).

-- 
Alex | twitter.com/alexconrad


More information about the Tutor mailing list