[Tutor] __init__.py question
Alexandre Conrad
alexandre.conrad at gmail.com
Wed Jun 1 21:50:06 CEST 2011
2011/6/1 Marilyn Davis <marilyn at pythontrainer.com>:
> Maybe I'm getting what you say, Alexandre and Ramit.
>
> When you import logging, it imports string, but it won't if you have a
> string of your own already imported.
>
> So, when logging depends on string, it'll get mine and crash.
>
> But __init__.py helps what in this scenario?
Nothing directly. I started my email with "As a side note" :)
> The only scenario it helps, that I can understand so far, is if I make a
> string directory that isn't part of a package that I'm making, but is in
> its directory structure, accidentally using a library name that I want.
Correct.
> I guess I don't feel very satisfied because I would be happy with the
> crash so that I can fix up my naming/architecture mistake rather than
> depend on, and struggle with, the __init__.py scheme.
>
> I teach Python and this is always a hard part for people. It might help
> if I understand it better.
Don't try to over-understand! :)
If an __init__.py file is present in your directory, it will be
considered by Python as a package. __init__.py also avoids non-Python
directories to potentially pollute/conflict with your namespace.
That's pretty much it.
Additionally, when importing a package, anything in the __init__.py
module will be executed during import. Just as you can see from the
previous "logging" traceback I sent.
--
Alex | twitter.com/alexconrad
More information about the Tutor
mailing list