creating a module
Andrei Kulakov
ak at silmarill.org
Tue Apr 2 10:18:58 EST 2002
In article <3CA87B34.CF1128F7 at ecn.purdue.edu>, Christopher N. Deckard wrote:
> 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.
>
Set PYTHONPATH env variable to the list of directories you want to
import modules from.
>
> 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.
--
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org
More information about the Python-list
mailing list