PYTHONPATH issue with sibling package names

Diez B. Roggisch deets at nospam.web.de
Thu Sep 10 16:12:20 CEST 2009


Stuart Moffatt wrote:

> Environment: Eclipse 3.4.2, Windows XP Pro SP2, Pydev 1.4.4, python
> 2.6
> 
> When I work in eclipse with java, I like to break up my client and
> server packages, like this:
> 
> client-project/src/org/me/client
> 
> server-project/src/org/me/api
> server-project/src/org/me/dao
> server-project/src/org/me/entity
> server-project/src/org/me/<etc>
> 
> Then, when I need to call API code from the client, I make sure the
> API src is on the path.
> 
> I am trying setup pydev projects to do the same thing, but running
> into an ImportError because my client code can't see the server src,
> even though it is on the path.
> 
> Specifically, I am trying to import an entity from the server code
> into the client, like this:
> 
>    from org.me.entity import MyEntity
> 
> If I do this from any module in the server project it is fine (because
> the src path is in the same eclipse project). But if I do it from
> anywhere in the client code I get the ImportError
> 
> From what I can tell, python asks for the "closest" module path, which
> is the current project. It finds org.me, but there is only the client
> sub-package. The org.me.entity sibling is in another eclipse project,
> but even though that path is on the PYTHONPATH, python stopped looking
> after it found a similarly named parent package.
> 
> Is there a trusted way to make sure python looks through all paths for
> sibling packages? Can I load 'org.me.client' from one path in
> PYTHONPATH and 'org.me.*' from another path in PYTHONPATH? I imagine
> if I try to force python to load the second package first that the
> same thing will happen in reverse.

The solution you are searching for is called "namespace packages", and you
can read more about it here:

  http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages


Do yourself a favor though, and don't use those several-steps-namespaces.
This is Python, not Java - two levels at *most*, normally a
project-namespace should be enough.

Diez



More information about the Python-list mailing list