import demo_package1.module1 as d1m
NB: Demonstrations of Import and init.py()
Demo 1: Empty __init__.py
d1m.welcome1()
Hi, I'm from Demo 1!
from demo_package1.module1 import welcome1
welcome1()
Hi, I'm from Demo 1!
Demo 2: Edited __init__.py
You can allow the users to import a module function directly from a package by simply adding:
from package.module import func # or class
or
from .module import func # or class
to your __init__.py
file.
For example, our Demo2 __init__().py
contains:
from demo_package2.module2 import welcome2
This allows me to do this:
import demo_package2 as d2
d2.welcome2()
Hi, I'm from Demo 2!
Or this:
from demo_package2 import welcome2
welcome2()
Hi, I'm from Demo 2!