Outils pour utilisateurs

Outils du site


python:modules_packages_import

Ceci est une ancienne révision du document !


Modules, Packages, Import

Exemple 1

/home/marc/test
├── app
│   ├── controllers
│   │   ├── a.py
│   │   └── b.py
│   ├── services
│   │   └── ...
│   ├── __init__.py
│   └── main.py
└── run.py
run.py
from app import app
 
if __name__ == "__main__":
    print(app.name)
    app.show()
app/__init__.py
from .main import Application
 
app = Application()
app/main.py
from .controllers.a import ControllerA
from .controllers.b import ControllerB
 
class Application:
    def __init__(self):
        self.name = "Mauricette"
 
    def show(self):
        a = ControllerA()
        a.hello()
        b = ControllerB()
        b.hello()
app/controllers/a.py
class ControllerA:
 
    def hello(self):
        print("Hello, I'm Controller A")
app/controllers/b.py
class ControllerB:
 
    def hello(self):
        print("Hello, I'm Controller B")

Exemple 2

DOSSIER
#/home/marc/test
├── # app  
│   ├── # controllers
│   │   ├── # a.py
 
                class ControllerA:
                    def hello(self):
                        print("Hello, I'm Controller A")
 
│   │   └── # b.py
 
                class ControllerB:
                    def hello(self):
                        print("Hello, I'm Controller B")
 
│   ├── # __init__.py
│   │
│   │       from .main import Application
│   │
│   │       app = Application()
│   │
│   └── # main.py
│
│       from .controllers.a import ControllerA
│       from .controllers.b import ControllerB
│
│       class Application:
│           def __init__(self):
│               self.name = "Mauricette"
│
│           def show(self):
│               a = ControllerA()
│               a.hello()
│               b = ControllerB()
│               b.hello()
│
└── # run.py
 
        from app import app
 
        if __name__ == "__main__":
            print(app.name)
            app.show()
python/modules_packages_import.1606552432.txt.gz · Dernière modification: 2020/11/28 08:33 par marclebrun