Python packages - problems, pitfalls.

Bernhard Herzog bh at intevation.de
Tue Nov 6 20:56:49 CET 2001


Prabhu Ramachandran <prabhu at aero.iitm.ernet.in> writes:

> >>>>> "BH" == Bernhard Herzog <bh at intevation.de> writes:
> 
>     >> pkg_root/ __init__.py app.py # app is the application that is
>     >> not part of the package
> 
>     BH> If it's not part of the package, why is it in pkg_root? Put it
>     BH> somewhere outside of pkg_root, make sure pkg_root is in a
>     BH> directory on Python's path and everything should be fine.
> 
> Easier said than done.  If I wanted to do this I'd have to re-nest
> every blessed file in CVS.  This is something I want to avoid.

Do you mean that pkg_root is the CVS module? Ouch!

Well, there's a way to achieve it without much renaming in CVS with CVS'
modules file. If you create a new CVS module named app with app.py and a
line like

app app &pkg_root

a cvs checkout app should create an app directory with pkg_root as
subdirectory. Caveat: I'm not a CVS expert and I haven't used this
feature myself yet, but it's used quite a bit in GNOME's CVS.


[ni could refer to the parent module with __]
> Yes, unfortunately, this way of referring to parent packages was
> removed!   From: http://python.org/doc/essays/packages.html
> 
>    "This feature was dropped because of its awkwardness; since most
>    packages will have a relative shallow substructure, this is no big
>    loss.)"
> 
> If you ask me that is a bad assumption.  "most packages will have a
> relative shallow substructure" - where was that pulled out from??

Well, in your pkg_root example the packages were nested two deep. I'd
count that as "relative shallow".

> Let me try to be a little clearer.  This is what Python does now:
> 
>   (1) Check if module is in same directory (sibling module).
> 
>   (2) If not found - look at the global level for modules in any
>   directories found in sys.path.

Correct.

> There is *absolutely* no in between here.  Its either local or global.
> What I suggest.
> 
>   (1) Check if module is available in the same directory, if available
>   - use it.
> 
>   (2) If not found walk up to parent dir.  check there, if not there
>   go up until out of package.
> 
>   (3) If not found in (2) look at sys.path for module.

I don't think that will help with your problem. At least not as far as
the app.py part is concerned. If you execute app.py in pkg_root, all
sub-packages are on the top-level, i.e. there is not parent package,
because pkg_root is in sys.path.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                               http://mapit.de/



More information about the Python-list mailing list