On Jan 6, 2009, at 10:45 AM, Bob Sysero llc Dev wrote:

>       Let us say that I set my PYTHONPATH=/home/sysero/project/sound, which
> is my working directory. In my working directory I have two
> sub-directories called format and effects.  In my main.py I have:
>
> import sound.effects.echo
>
> sound.effects.echo.echoFilter(input, output, delay=0.7, atten=4)
>
> I think that python will look in all my sub-directory in my working
> directory for the __init__.py and see if inside the script if it has  
> an
> from statement that has:
>
> from sound.effects.echo import echoFilter
>   or should it be
> from sound.effects import echo
> ???


        If it is the __init__.py in the effects directory, any imports are  
relative to that module location. So the import would look more like:  
import echo

>       Then Python will load the echo module that is within the effects
> directory.  To put it in another way the effects directory is in a  
> way a
> module that contains other modules and the __init__.py is to list the
> modules and classes within the module. This is why one does not need  
> to
> include all the sub-directories in the PYTHONPATH.  Am I correct about
> this idea?

        Yes. It finds the 'effects' directory, sees that it has an  
__init__.py file, so Python knows that that is a module, and treats  
references accordingly.

>       I also think, if this is correct that you must have a:
>   from sound.effects.echo import echoFilter

        Only if you want 'echoFilter' in the current namespace. IOW, if you  
will be writing code that calls:

echoFilter(foo, bar, baz)

instead of:

effects.echo.echoFilter(foo, bar, baz)

>       My next question is that in the folder ui I have a module MyForm.py
> that uses the sub-directory formats wavread.py getWavFile class and  
> the
> sub-directory effects echo.py echoFilter class.  In my MyForm.py I  
> would
> use:
>
> from sound.formats.wavread import getWavFile
> from sound.effects.echo import echoFilter

        If 'sound' is your current directory, the code should be: from  
formats.wavread import getWavFile

> getWavFile("sametest.wav")
> echoFilter(input, output, delay=0.7, atten=4)
>
>       the working dir is set in:
> PYTHONPATH=/home/sysero/project/sound.  Python will look in all my
> sub-directory formats and effects to check each __init__.py for my:
>
> from sound.formats.wavread import getWavFile
> from sound.effects.echo import echoFilter
>   or
> from sound.formats.wavread import *
> from sound.effects.echo import *
> ???

        Namespaces are a good thing; don't use the second form! It can  
pollute the namespace with duplicate names. Let's say that your  
'formats.wavread' has a play() method, and so does 'effects.echo'.  
When you run the first format, the two methods would be  
distinguishable because of the namespaces: formats.wavread.play and  
effects.echo.play. If you use the second form, the unqualified 'play'  
will refer to the second module's play() method, and you won't be able  
to access the first module's play().


-- Ed Leafe





_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to