[Python-checkins] peps: Added examples.

Eric V. Smith eric at trueblade.com
Thu May 24 15:22:46 CEST 2012


On 05/24/2012 08:47 AM, Nick Coghlan wrote:
>> +    >>> import sys
>> +    >>> sys.path += ['Lib/test/namespace_pkgs/parent1/parent', 'Lib/test/namespace_pkgs/parent2/parent']
> 
> The trailing "/parent" shouldn't be there on either of these paths.
> The comments that refer back to these also need the same adjustment.

True. I had stupidly started with PYTHONPATH including the parent1 and
parent2 directories, so this really wasn't doing anything.

> 
>> +    # now add parent3 to the parent's __path__:
>> +    >>> parent.__path__.append('Lib/test/namespace_pkgs/parent3/parent')
> 
> This modification is incorrect, it should be:
>     sys.path.append('Lib/test/namespace_pkgs/parent3')

Either one will work.

>>> import unittest
>>> unittest.__path__
['/home/eric/local/python/pep-420/Lib/unittest']

Note that unittest.__path__ ends in Lib/unittest, despite it being only
Lib that was on PYTHONPATH. The same holds true for modifying
parent.__path__: it needs to list the directory names where child
portions will be found.

>>> sys.path += ['Lib/test/namespace_pkgs/project1',
'Lib/test/namespace_pkgs/project2']
>>> import parent
>>> parent.__path__
_NamespacePath(['Lib/test/namespace_pkgs/project1/parent',
'Lib/test/namespace_pkgs/project2/parent'])

Here, the paths contain the places from which portions of parent will be
loaded. So to find more portions of parent in project3, it's
project3/parent that has to be added to parent.__path__.

Maybe it would be clearer if I included other modules in parent itself,
not just in parent.child.

Possibly I am being too tricky here by modifying parent.__path__, and I
should just modify sys.path again, as you suggest. But I was trying to
show that modifying parent.__path__ will also work.

Eric.


More information about the Python-checkins mailing list