creating a module

Christopher N. Deckard cnd at ecn.purdue.edu
Mon Apr 1 10:22:29 EST 2002


I'm having some difficulties making my python module available to
other programs besides itself.

Say I have a module called Thinger, and a basic file layout like:

Thinger (a directory)
       |- Thinger.py
          from Thinger_definitions import *
          from Commands.Command import Command
          from Commands.Command1 improt Command1
          class Thinger:
            methods...

       |-  Thinger_definitions.py (global definitions for Thinger 
                                   services.  Every other file has
                                   import * from
Thinger_definitions)

       |-  ThingerConnection.py (handles connections to a server)
           class ThingerConnection:
              methods...

       |-  Commands (a directory)
                   |- Command.py (base command class)
                      from Thinger_definitions import *
                      class Command:
                        methods...
                   |- Command1.py
                      from Thinger_definitions import *
                      class Command1(Command):
                        methods...

If I create a script in the Thinger directory, which uses the
Thinger class, then everything works fine.  If I make a script
outside of Thinger (parent directory) and do something like

test_script:
  from Thinger.Thinger import Thinger
  my_thing = Thinger()

Then this fails when the files in the Command directory try to
import Thinger_definitions.

Really, what I'd like to do is to be able to say in Thinger.py

from Commands import *

and that would import all of the Commands that were in there and
make them directly useable as Command(), Command1(), etc.

Another question along these lines is what shouls go in the
__init__.py script in each directory?  Right now I just have the
keyword 'pass' and that seems to do fine for most things.

Thanks,
-Chris

P.S.  If you could, please send mail to me as well as the group.



More information about the Python-list mailing list