importing package contents from multiple places in PYTHONPATH

joshetomlinson at gmail.com joshetomlinson at gmail.com
Mon Mar 24 14:06:51 EDT 2008


Hi all,

I'm new to python, and am trying to determine if it's possible to do
the following...

I have a directory structure like this, with both 'dir1' and 'dir2' in
my PYTHONPATH

dir1/
  foo/
    __init__.py
    a.py
    b.py

dir2/
  foo/
    __init__.py
    a.py
    c.py

I'd like to be able to:

python> import foo.a, foo.b, foo.c

I'd hope for package 'foo.a' to come from dir1 since it was first on
the path, with 'foo.b' and 'foo.c' coming form dir1 and dir2
respectively.

I understand that python stops once it encounters the first 'foo'
package in PYTHONPATH, but I was wondering if there was a way around
this.  I've had some success modifying __path__ in the foo/__init__.py
files, but am unsure if this is the best approach.  Perhaps there's a
way to do this with import hooks?

Is there a precedent for this type of thing?

Thanks in advance,
Josh



More information about the Python-list mailing list