[Tutor] Help with packages and namespaces please

Andrew Wu andrewwu at gmail.com
Tue Oct 16 01:03:10 CEST 2007


Hmmm ... thank you very much for the detailed explanations.  This is a very
simplified version of some actual code I'm currently using and attempting to
rework on each individual file (about 60+ of them).  If it's not feasible
then I guess I'm stuck with the rework.  =(

More comments inline ...

On 10/13/07, Kent Johnson <kent37 at tds.net> wrote:
>
> Andrew Wu wrote:
>
> > Let's say I have these files in one directory:
> >
> > PrintBase.py
> > PrintHello.py
> > PrintBye.py
>
> > I'd like to reorganize the files so they're like the Sound example in
> > the Python tutorial:
> >
> > PrintMe/
> > PrintMe/PrintBase/PrintBase.py
> > PrintMe/PrintHello/PrintHello.py
> > PrintMe/PrintBye/PrintBye.py
>
> Will there be more modules in each subdirectory? I'm not sure if this is
> just for learning or if it is a simplification of a real problem, but
> from your description of the classes it sounds like they belong in the
> same package.


Yes - there will be more modules in each subdirectory (I only had one in the
example for my own testing purposes and for simplification).



> I've created empty __init__.py files in each of the subdirectories.
> > Here I run into a number of problems and get confused - my goal is to
> > keep the import statements as they are (the actual files I'd be editing
> > are many and I'd rather avoid having to edit them all) - is there
> > something I can put in the __init__.py files so that the modules are
> > brought into namespace w/o having to use absolute module notation?
>
> You can bring names into the package namespace, but the actual code will
> still run in the module where it is defined. For example, in
> PrintMe/PrintBase/__init__.py you can say
>    from PrintBase import Printbase
>
> Hmm. I'm not sure if this will work the way you want because of the name
> collisions. Using the sound example where the names are unique, suppose
> wavread.py includes a function read_wav(). Then in Formats/__init__.py
> you can say
>    from wavread import read_wav




This imports the name read_wav into the Formats package namespace so
> other code will be able to say
>    from Sound.Formats import read_wav
>
> In your example, *without* the import in PrintBase/__init__.py it is
> already valid to say
>    from PrintMe.PrintBase import PrintBase
> which will access the *module* PrintBase. When you add the line
>    from PrintBase import Printbase
> to __init__.py then there are two possible meanings for
> PrintMe.PrintBase.PrintBase and I don't know which one will have
> priority. My guess is that the module import will win but I don't know.
>
> At best this seems like a recipe for endless confusion - you have three
> entities named PrintBase - a package, a module and a class.


Hmmm ... this could be due to the poor naming convention (which also
afflicts the actual code I'm modeling - the use of the same name for the
class and file) ...



If the import in __init__ promotes the class as you want, it still won't
> help with the imports in PrintHello.py - it will still have to say at
> least
>    from PrintMe.PrintBase import PrintBase
>
> Why do you want to do this? It looks like a confusing mess to me.
> >
> > As simple tests, I've tried running different import commands in the
> > python interpreter:
> >
> > I've tried adding the absolute directory paths to __path__ in each
> > __init__.py per subdirectory, and adding the paths to sys.path.
>
> Yuck. What is the point of the directories and packages if you are going
> to put every thing on sys.path anyway? But I think it should work if you
> put each of the subdirectories of PrintMe on sys.path.


My hope was to avoid having to edit the individual files but if that leads
to a gobbled mess then I'd rather do it more correctly.


> When I
> > do that and run the python intepreter and try 'from PrintMe.PrintHello
> > import PrintHello' I get an error saying 'Error when calling the
> > metaclass bases, module.__init__() takes at most 2 arguments (3
> > given)'.  The same code functioned fine when all the files were in the
> > same directory, so I'm confused about why the interpreter thinks I'm
> > passing 3 arguments along.
>
> You get an error on the import? Please show the traceback.


It could be I'm doing it all wrong.

Hmmm ... I can't get it to happen again.  If / when I do I'll post the
traceback.  Thanks!



Andrew



Kent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071015/c408d1e5/attachment.htm 


More information about the Tutor mailing list