470,613 Members | 1,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

importing package contents from multiple places in PYTHONPATH

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:

pythonimport 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
Mar 24 '08 #1
1 1328
En Mon, 24 Mar 2008 15:06:51 -0300, <jo************@gmail.comescribió:
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:

pythonimport 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.
Yes. Note that dir2/foo/__init__.py is not used at all and it's just
confusing. And a.py (in dir2) won't be found inside the package but any
"import a" from c.py will import that one instead of the one at dir1.
dir2/foo is just a "bag", not a "package" :)
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.
I think it is the simplest approach, if not the only one...
Perhaps there's a
way to do this with import hooks?
Perhaps... But appending a single item to __path__ is simple enough to
stop further thinking from my side :)

Anyway, why do you want to do that? Can't use a different name for
dir2/foo, and perhaps import its modules from inside dir1/foo/__init__.py?
--
Gabriel Genellina

Mar 24 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Skip Montanaro | last post: by
reply views Thread by Mark English | last post: by
3 posts views Thread by Charles Krug | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.