Problem with packages and modules

Denis S. Otkidach ods at fep.ru
Tue Apr 9 10:08:12 EDT 2002


On 9 Apr 2002, [ISO-8859-1] JЬrgen Hansen wrote:

JH> Hello,
JH>
JH> I have been working for some time now with Python and really
JH> enjoy it.
JH> Right now I am having serious difficulties crasping several
JH> issues
JH> when using packages.
JH>
JH>
JH> This is an example of directory hierachy:
JH>
JH> company/                   Top-level package
JH>     __init__.py            Initialize the company package
JH>     plot/                  A plot package
JH>         __init__.py
JH>         axes.py            Modules in the plot package
JH>         axis.py
JH>         bar.py
JH>         figure.py
JH>         line.py
JH>         plotobject.py
JH>         points.py
JH>         tick.py
JH>         utils.py
JH>     log.py                 Modules in the company package
JH>     utils.py
JH>
JH>
JH> If I were to use the plot package I would type in
JH>    >>> from company import plot
JH>
JH> and assume that I could access modules as:
JH>    >>> plot.figure.Figure()    # where Figure is a class
JH> name.
JH>
JH> but this is not the case. Neither of the modules in the plot
JH> package
JH> are available, and it doesn't matter if I use the __all__
JH> attribute in
JH> __init__.py and type in all the module names.

Certanly, module figure is not imported. You should write "import
figure" in __init__.py or use "from company.plot import figure"
(or "from company.plot import *" if you know what you are doing).

JH> Further if I typed in:
JH>    >>> import company
JH>
JH> I would assume that I could access all subpackages and their
JH> modules
JH> with a dotted name (company.plot.figure.Figure() for
JH> instance), but
JH> this is obviously not the case.

Similar, write "import plot" in company/__init__.py or import
modules directly.






More information about the Python-list mailing list