[Tutor] Sanity check -- making packages

Kirby Urner urnerk@qwest.net
Wed, 23 Jan 2002 16:45:49 -0800


At 07:14 PM 1/23/2002 -0500, Jeff Grimmett wrote:
>I am beating my head against the wall with regards to packages, desperately
>need a sanity check here.

<<SNIP>>


>Any comments links or pointers are appreciated.
>

I think you should separate the two challenges:
(1) getting a package to work on your own box and
(2) packaging it up for distribution via disutils.

Regarding (1), the purpose of __init__.py in the
package subdirectory is to do the importing of
what you want your public (or yourself) to see of
your package.

For example, I have a subdirectory called
mathobjects under which I have such .py files as
polynomial.py simplematrix.py and fraction.py.

My __init__.py looks like this:

====
"""
By K. Urner
Last modified: May 20, 2001
"""
from simplematrix import Matrix, Sqmatrix
from pyfraction import Fraction
from polynomial import Poly, deriv
====

So say I'm in the shell, having just booted Python,
and I go:

   >>> dir()
   ['__builtins__', '__doc__', '__name__']

OK, the basics.  Clean slate.  Now I go:

   >>> from mathobjects import *

And check my namespace:

   >>> dir()
   ['Fraction', 'Matrix', 'Poly', 'Sqmatrix', '__builtins__',
   '__doc__',   '__name__', 'deriv', 'polynomial', 'pyfraction',
   'simplematrix']

Aha, so the things I imported in __init__.py have now
become top level.   So in the shell, I can now use Poly
directly:

    >>> p = Poly([1,2,3,4,5])  # fancier version than on tutor list
    >>> p
    x**4 + 2*x**3 + 3*x**2 + 4*x + 5

Regarding (2), using dsutils, that's a different challenge.
There, you want to create setup.py in a directory just above
the package subdir, which will look something like:

#!/usr/bin/env python

from distutils.core import setup

setup(name="mathobjects",
       version="1.1",
       description="Math Objects: Fraction, Matrix, Polynomial",
       author="Kirby Urner",
       author_email="urnerk@qwest.net",
       url="http://www.intearena.com/~pdx4d/ocn/",
       packages=['mathobjects'],
      )

Then the command:  python setup.py sdist

should create a .zip file (the default on Windows -- check
docs for other params).  If your end user then goes:

python setup.py install

from within the unzipped zip, the package will be installed.

Kirby