473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 1419
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Skip Montanaro | last post by:
At work we have a package structure for our homegrown code. Developers each have their own sandboxes where they work on the stuff they are responsible for (mixtures of C++ libraries, SWIG wrappers...
0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
3
by: Charles Krug | last post by:
List: I'm trying to use the example files from Programming Python, 2nd Ed. I've copied them into c:\Python24\Examples\PP2E. Launching any of the examples programs by themselves seems to work...
14
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.