Thanks. You're right, it all becomes clear once one understands the Python
package/module locating algorithm. Just in case anyone else might find this
useful, the behavior I wanted can be achieved by including the following in
each __init__.py file:
# Incantation to ensure that python.path acts like Java classpath
import os, sys
__path__ = [x + os.sep + __name__.replace('.', os.sep) for x in sys.path]
Samuele Pedroni wrote:
From: "Robert Wentworth" <firstname.lastname@example.org>
> Synopsis: Would I be correct in inferring that the Jython path mechanism
> only works if the different nodes along the path contain completely
> non-overlapping package structures? Is there any way around this?
> I had been planning on encouraging the use of "python.path" in our
> Jython development environment. Developers would work in a local image
> of the Jython directory hierarchy, but would also reference a standard
> version of the same directory hierarchy. (In a C/C++ environment, you
> might refer to this as developing using a "view path", with transparent
> overlapping "layers" of directories and files.) It turns out that this
> usage apparently doesn't work in Jython, at least not in an environment
> where we want to use a non-trivial package structure to organize our
> Let me provide some examples. For all examples, suppose python.path has
> been set such that sys.path == ['/A', '/B'].
> Suppose that /A contains a directory /A/X with files __init__.py and
> xena.py, while /B contains a directory /B/Y with files __init__.py and
> yankee.py. Then, as expected, one can "import X.xena" and "import
> Y.yankee". So far so good.
> But, if instead /A contains directory /A/R with file __init__.py and
> directory /A/R/X with files __init__.py and xena.py, while /B contains
> directory /B/R with file __init__.py and directory /B/R/Y containing
> __init__.py and yankee.py. Then "import R.X.xena" succeeds, but "import
> R.Y.yankee" fails. Interestingly, both "import R.X" and "import R.Y"
> I've tried tinkering with not including the (empty) __init__.py file in
> some directories, but this hasn't improved the situation.
> So, it appears that the Jython path mechanism only works if the
> different nodes along the path contain completely non-overlapping
> package structures.
> Am I missing anything?
jython package behavior comes straight from Python.
Basically a package maps to the first __init__.py in an appropriate named
directory found along sys.path search.
OTOH when searching for modules inside a package an optional list of paths
bound to __path__ in __init__.py
module will be searched.
So you can have
and have in /A/X/__init__.py
__path__ = ['/A/X','/B/X']
Btw at the beginning of __init__.py execution __path__ is automatically bound
by default to a list with only the containing dir:
print __path__ at the top of __init__.py would show that.
See also http://www.python.org/doc/essays/packages.html