[Tutor] __init__.py question
Marilyn Davis
marilyn at pythontrainer.com
Wed Jun 1 19:37:29 CEST 2011
Thank you Válas Péter. I still don't get it, or how __init__.py alters
the scenario you describe.
I know that sometimes I'm really dense so please be patient with me.
If I have a string.py in my local directory, it will be imported when I
"import string" because the search path, sys.path, (which I have control
of), has my local directory as the first place to search by default, and
no __init__.py is involved. If my string is not a .py but
package/directory itself, it will be imported and searched into if it has
a __init__.py. In either case, how can I get to the system's string.py by
putting __init__.py somewhere?
If my string.py is in a package, I have to put its parent directory on my
sys.path for it to be found, and the parent directory won't need an
__init__.py
There's something I'm missing because I think you simply can't call
something string.py unless you are willing to give up the library
string.py.
I can see that a person could have a local string directory that they
don't want imported because it has some non-code in it, or something. But
the __init__.py facility is a big deal to be there for the purpose of
allowing this when it is, IMHO, an architectural mistake.
There must be something I'm missing or some wrong thinking described here
because Guido is so smart, and it's still in 3.x.
If anyone has an insight, I would really appreciate some help on this
point, which has bothered me a bit for years.
Marilyn Davis
On Wed, June 1, 2011 12:26 am, Válas Péter wrote:
> I think it means that you may have a subdirectory named "string" in your
> actual directory where you store e.g. your string-related scripts, and you
> have a Python package named "string" _somewhere_else_, but in the module
> search path. The subdirectory of the actual directory will have priority
> bacause of its place, and will prevent you of importing the string
> package.
>
> 2011/6/1 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.
>>
>> An example would probably set me straight. Can anyone help with that?
>>
>>
>> Marilyn Davis
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
More information about the Tutor
mailing list