Structure of packages

B

Ben Fisher

I am trying to learn the best way to do intra-package references. My
package looks like this:

PackageName
__init__.py
/a
__init__.py
a.py
...
/b
__init__.py
...
/c
__init__.py
...
/d
__init__.py
...
I have layered the dependencies so that a depends on b, b depends on
c, and c depends on d. There are no circular references.

Now I would like to be able to refer to the subpackage b from inside
the subpackage a. In effect, I would like to say "from '../b' import
*"

I had thought that "from PackageName.b import *" would work. This
works for a file in the directory /PackageName, but not for a file in
the directory /PackageName/a. It's like when you are running a Python
file in the directory /PackageName/a it doesn't know about PackageName
- No module named "PackageName".

Is there a solution to this, or a better way to structure the directories?
 
R

Raymond Hettinger

[Ben Fisher]
I am trying to learn the best way to do intra-package references.

IMO, the email package is a stellar example of best practices using
packages.

I have layered the dependencies so that a depends on b,
b depends on c, and c depends on d.

For the most part, I think packages tend to be over-used and can
create more problems than they solve. They were added as tool for
managing *very* large code bases and for helping resolve namespace
collisions between tools with similiar APIs. Your layering
application may also be a good use but I haven't seen packages used
that way before.


Raymond
 
S

Scott David Daniels

Ben said:
I am trying to learn the best way to do intra-package references. My
package looks like this: ....
I had thought that "from PackageName.b import *" would work....
In an attempt to hand oyu a net, rather than an answer:
Try using command line:

python -v whatever.py

You can see all the imports that are done and in what order.

Also realize that import a file executes it, so you can sprinkle
prints at points where you are confused.

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top