Problem with packages and modules

Jørgen Hansen jorgenhansen at hotmail.com
Wed Apr 10 02:07:37 EDT 2002


Max M <maxm at mxm.dk> wrote in message news:<3CB2FA14.1050708 at mxm.dk>...
> Jørgen Hansen wrote:
> 
> > This is an example of directory hierachy:
<snip example hierachy>
> > 
> > 
> > If I were to use the plot package I would type in
> >    >>> from company import plot
> > 
> > and assume that I could access modules as:
> >    >>> plot.figure.Figure()    # where Figure is a class name.
> 
> 
> You will need to add the full path to a module.
> 
> from company.plot.figure import Figure
> fig = Figure()
> 
> or
> 
> import company.plot.figure
> fig = company.plot.figure.Figure()
> 
> or
> 
> from company.plot import figure
> fig = figure.Figure()
> 
> 
> > Further if I typed in:
> >    >>> import company
> > 
> > I would assume that I could access all subpackages and their modules
> > with a dotted name (company.plot.figure.Figure() for instance), but
> > this is obviously not the case.
> >
> 
> You would need to import the modul in company/__init__.py:
> import plot
> 
> to do:
> 
> import company
> fig = company.plot.figure.Figure()
> 
> 
> Hmm... I hope this helps a bit.

It does. Thanks a lot. After I wrote, I got thinking, that my approach
would have meant that I would import the entire hierachy. Not very
smart if the hierachy, where larger.

Well thanks again, for the prompt answer.

regards
Jorgen



More information about the Python-list mailing list