package import question

Peter Otten __peter__ at web.de
Mon Oct 22 04:24:15 EDT 2007


Phoe6 wrote:

> Hi all,
> I have the following directory structure:
> 
> wallpaper/
>               -main.py
>               -ng/
>                  -- __init__.py
>                  -- setdesktop.py
>               -yb/
>                  -- __init__.py
>                  -- setdesktop.py
> 
>>From main.py, I would like to do:
> import ng
> import yb
> ng.setdesktop.run()
> yb.setdesktop.run()
> 
> But it is not working! when I import the package, the modules present
> inside the package are getting imported.
> 
> However, following the python document if I do
> import ng.setdesktop
> import yb.setdesktop
> 
> ng.setdesktop.run()
> yb.setdesktop.run()
> Works fine.
> 
> I would like to use the notation.
> import <my_package_name>
> and use the modules inside the package in the dotted module notation.
> What should I do the enable such a kind of imports.

Put the line

from . import setdesktop

into both __init__.py files. Importing the package will then trigger the
submodule to be imported.

Peter



More information about the Python-list mailing list