[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Structure of packages

Ben Fisher

1/9/2008 8:07:00 PM

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?
2 Answers

Raymond Hettinger

1/9/2008 8:57:00 PM

0

[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

Scott David Daniels

1/10/2008 5:28:00 AM

0

Ben Fisher wrote:
> 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
Scott.Daniels@Acm.Org