Python packages - problems, pitfalls.
Robert Roy
rjroy at takingcontrol.com
Sun Nov 4 22:35:53 EST 2001
On Sun, 4 Nov 2001 12:07:50 +0530, Prabhu Ramachandran
<prabhu at aero.iitm.ernet.in> wrote:
>hi,
>
>Recently I discovered that there is some very unfortunate behaviour
>with python packages.
>
>Lets say I have a directory 'pkg' that is the root of a package.
>Inside this directory I have a sub-package, called sub. So its
>something like:
>
>pkg/
> __init__.py
> a.py
> sub/
> __init__.py
> b.py
>
>Now, from b I'd expect to be able to import 'a' straight away.
>
Why would you expect that? a.py has no relationship to b.py. It is not
a sibling module, it is not a parent.
>Say in b.py I do
>
>import a
>
>This will not work! I know why it happens but shouldn't Python be
>smart enough to avoid such problems? Why I consider this a major
>problem is that the importing of a sub-package depends on where its
>parent package is!!
Its a good thing it doesn't. It would be a maintenance nightmare.
See Tim Peter's rules of python programming: Explicit is better than
implicit
Frankly if I had a situation as you describe above, I would take a
good look at why I had structured my modules that way. Most likely I
would find that the module structure had outlived its usefulness and
needed to be redefined/refactored.
> So if pkg is no longer the root of the package
>and say its put inside another BigPkg then to make pkg's sub packages
>work you'd have to edit *all* the sub packages and change every
>reference to pkg.a to BigPkg.pkg.a. This is a huge pain.
Use a ".pth" file to tell it where the package resides. Say you
re-nest pkg into BigPkg, place a file called pkg.pth in the root
directory of your distribution and put the location of pkg in it. See
the documentation on module "site".
for example (not tested!):
Python root
pkg.pth (contents would be Lib/BigPkg/pkg)
Lib/
BigPkg/
__init__.py
pkg/
__init__.py
This will allow old pkg references to continue working.
>
>Are there better ways to get around this packaging problem? Is this a
>know problem that folks are working on?? Why is it that Python does
>not deal with this issue more sensibly?
>
Python does, it is not terribly well documented though...
bob
More information about the Python-list
mailing list