Python search path wrong, but only in Windows
Question: I am creating a larger logical package spread out over many directories, like
so:

    
    
    [projects root]/projectname1/lib/python/logicalpackage/__init__.py
    [projects root]/projectname1/lib/python/logicalpackage/projectname1/__init__.py
    [projects root]/projectname2/lib/python/logicalpackage/__init__.py
    [projects root]/projectname2/lib/python/logicalpackage/projectname2/__init__.py
    

The idea is to be able to do this:

    
    
    import logicalpackage.projectname1 as p1
    import logicalpackage.projectname2 as p2
    

after having a script in .bashrc or $profile (bash and PowerShell,
respectively) that will glob over `[projects root]/*/lib/python/` and import
the packages it finds.

I know that [pkgutil](http://docs.python.org/2/library/pkgutil.html) is used
for this, by sticking that snippet in `__init__.py` (`from pkgutil import
extend_path; __path__ = extend_path(__path__, __name__`), and I can get
everything to work fine across all systems when I do so. My question, though,
is why when I _don't_ use pkgutil, this still works fine, but only on some
platforms--particularly, in OSX and Ubuntu (10 and 12 that I've seen), it
works, but in Windows (7) it doesn't. What I worry is that there is some side-
effect of using pkgutil that I'm not considering.

The specific non-working behavior is that PYTHONPATH appears to be constructed
correctly after that script is run (in the `.bashrc` equivalent in
PowerShell), i.e. I can debug print PYTHONPATH from within Python and it is
identical to the path constructed on another platform. However, `from
projectname1 import foo` succeeds, and `from projectname2 import bar` fails
(presumably because projectname2 is globbed later by alpha). That's actually
the behavior I would expect without pkgutil. Why isn't that the behavior in
OSX and Ubuntu? Is this problem from some pathing mechanism of Windows or
PowerShell, or the Python binaries as compiled on Windows, or something else
entirely?

EDIT: Adding the below for more clarity:

    
    
    # d:\projects> [Environment]::SetEnvironmentVariable("PYTHONPATH","d:\\projects\\projectname1\\lib\python;d:\\projects\\projectname2\\lib\\python;")
    # d:\projects> echo $env:PYTHONPATH
    # d:\\projects\\projectname1\\lib\python;e:\\projects\\projectname2\\lib\\python;
    # d:\projects> python
    
    import sys
    sys.path
    # => ['', '..python install dir..\\lib\\site-packages\\pip-1.2.1-py2.7.egg', 'd:\\projects\\projectname1\\lib\\python', 'd:\\projects\\projectname2\\lib\\python', ...usual stuff...]
    import logicalpackage
    logicalpackage.__path__
    # => ['d:\\projects\\projectname1\\lib\\python\\logicalpackage']
    import logicalpackage.projectname1 as p1
    import logicalpackage.projectname2 as p2
    
    # Traceback (most recent call last):
    #   File "<stdin>", line 1, in <module>
    # ImportError: No module named projectname2
    # exit()
    

replace the empty `__init__.py` with the pkgutils code mentioned above

    
    
    # d:\projects> python
    import sys
    sys.path
    # => ['', 'D:\\usr\\Python27_32bit\\lib\\site-packages\\pip-1.2.1-py2.7.egg', 'd:\\projects\\projectname1\\lib\\python', 'd:\\projects\\projectname2\\lib\\python', 'd:\\projects', '...etc...']
    import logicalpackage
    logicalpackage.__path__
    # => ['d:\\projects\\projectname1\\lib\\python\\logicalpackage', 'd:\\projects\\projectname2\\lib\\python\\logicalpackage']
    import logicalpackage.projectname1 as p1
    import logicalpackage.projectname2 as p2
    

Note: No exception. Having the exception go away after appending to the module
path via pkgutil is the expected behavior (to me, and to [the
documentation](http://docs.python.org/2/tutorial/modules.html))--Python
shouldn't append to the module path unless I explicitly declare it. What I am
wondering is why no exception occurs on Ubuntu (in other words, why it appends
to the module path **without** being explicitly declared), whether or not I
include the pkgutils snippet.


Answer: So, here's what ended up being the issue. It turns out that
`pkgutils.extend_path` will work for EVERY module within an extended logical
package, as long as it is contained within the FIRST instance of that logical
package in `PYTHONPATH`. In both environments, `PYTHONPATH` was being
prepended with an `environment` module contained within `logicalpackage`,
which included `extend_path`. In the Windows environment, that prepended path
didn't actually contain anything. My failure was basically in not recognizing
that `extend_path` will extend all over logical packages in `PYTHONPATH` even
if particular modules within that logical package don't contain `extend_path`,
as long as the first instance contains the `pkgutils` snippet.

