How to structure packages

rantingrick rantingrick at gmail.com
Wed Sep 7 13:56:53 EDT 2011


On Sep 7, 10:56 am, bclark76 <bclar... at gmail.com> wrote:
> I'm learning python, and was playing with structuring packages.
>
> Basically I want to have a package called mypackage that defines a
> number of classes and functions.
>
> so I create:
>
> mypackage
>     __init__.py
>     myfunc.py
>     MyClass.py

Don't tell me you create a module called myfunc.py??? Stuff that
function in __init__! Also don't name modules MyClass either. Both the
spelling and grammar is incorrect. ALL modules use lowercase (and only
integrate underscores in the most dire of need!)

geom
   __init__.py
   vector3d.py
   point3d.py
   transformation.py

from geom.transformation import Transformation
from geom.vector3d import Vector3d
from geom.point3d import Point3d

or alternatively:

geom
   __init__.py
       from __vector3d import Vector3d
       from __point3d import Point3d
       from __transformation import Transformation
   __vector3d.py
   __point3d.py
   __transformation.py

from geom import Transformation
from geom import Vector3d
from geom import Point3d

Although this method can be brittle due to the fact that one buggy
module can break all the imported code in the __init__ module. I
usually opt for specifying the module in the import path.



More information about the Python-list mailing list