Package __init__ question

David LeBlanc whisper at oz.net
Thu May 23 18:51:39 EDT 2002


> -----Original Message-----
> From: Chris Liechti [mailto:cliechti at gmx.net]
> Sent: Thursday, May 23, 2002 12:39
> To: David LeBlanc
> Subject: Re: Package __init__ question
>
>
> "David LeBlanc" <whisper at oz.net> wrote in
> news:mailman.1022178791.2145.python-list at python.org:
>
> > I downloaded a package - let's call it "fumble" - and when I installed
> > it, it put itself into site-packages directly and not a sub-directory
> > of it. Since I want to keep the site-packages directory clean, I
> > created a subdirectory called fumble and copied all the fumble files
> > into it. I added an empty __init__.py file and it didn't work. Only
> > after I added the line "from fumble import *" to the __init__ file did
> > it work.
>
> did't it work with an empty __init__.py but importing "fumble.fumble" ?
> the import * you use is needed no get rid of the additional
> hiearchy level
> which you created throug you subdir.

Nope - it would only work that way if I did "import fumble.fumble as fumble"
and that's too much fumbling for me :o). The (demo) app using fumble expects
fumble objects in the fumble namespace, not imported into the main
namespace. I guess it is (semi) proper to do the "from fumble import * in
the init file to get rid of the extra level of "indirection" (as it where).

> > Is this the correct way that packages are meant to work? I kind of
> > doubt it, but looking over the pythondoc failed to suggest any
> > alternative (my solution was a lucky guess). Comments and pointers to
> > good doc appreciated.
>
> i'm using something like that in my pyserial package. __init__.py decides
> which platform its running on and imports * from a platform dependant
> source file.
>
> chris
>
>
> --
> Chris <cliechti at gmx.net>
>

So it puts the platform dependent code into the common namespace "pyserial"?

I'm curious to know where I can find general information about how to build
packages so that I can put "mypackage" in a "mypackage" dir and expect
"import mypackage" to work with it's objects in the "mypackage" namespace
(or is the "from mypackage import *" the canonical way to do this?). To me
"from mypackage import mypackage" is clumsy. I can see "from mypackage
import mysubpackage" though...

Also, thanks for the help Chris

Dave LeBlanc
Seattle, WA USA






More information about the Python-list mailing list