package import question

P

Phoe6

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.

Please let me know.

Thank you.
Senthil
 
D

Diez B. Roggisch

Phoe6 said:
Hi all,
I have the following directory structure:

wallpaper/
-main.py
-ng/
-- __init__.py
-- setdesktop.py
-yb/
-- __init__.py
-- setdesktop.py

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.

Please let me know.

You need to import the setdesktop-module in the respective __init__.py-files
to make them part of the names known in there.

Diez
 
P

Peter Otten

Phoe6 said:
Hi all,
I have the following directory structure:

wallpaper/
-main.py
-ng/
-- __init__.py
-- setdesktop.py
-yb/
-- __init__.py
-- setdesktop.py

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
 
P

Phoe6

Put the line

from . import setdesktop

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

Thanks a lot for your replies. I had a thought that I can do this way,
but as Python Documentation did not mention it, I was 'thinking' that
submodules will automatically get imported and __init__.py usually
blank.

Thanks again,
Senthil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top