Outils pour utilisateurs

Outils du site


python:modules_packages_import

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
python:modules_packages_import [2020/11/28 08:35]
marclebrun [Exemple 2]
python:modules_packages_import [2021/05/15 06:52] (Version actuelle)
Ligne 5: Ligne 5:
 [[https://​towardsdatascience.com/​learn-python-modules-and-packages-in-5-minutes-bbdfbf16484e]] [[https://​towardsdatascience.com/​learn-python-modules-and-packages-in-5-minutes-bbdfbf16484e]]
  
-===== Exemple 1 =====+[[https://​stackoverflow.com/​questions/​8953844/​import-module-from-subfolder]]
  
-<​code>​ +<​code>​add __init__.py ​to every subfolder you are importing from</​code>​
-/​home/​marc/​test +
-├── app +
-│   ​├── controllers +
-│   ​│ ​  ​├── a.py +
-│   ​│ ​  ​└── b.py +
-│   ​├── services +
-│   ​│ ​  ​└── ... +
-│   ​├── ​__init__.py +
-│   ​└── main.py +
-└── run.py +
-</​code>​+
  
-<code python run.py> 
-from app import app 
  
-if __name__ == "​__main__":​ +===== Exemple ​=====
-    print(app.name) +
-    app.show() +
-</​code>​ +
- +
-<code python app/​__init__.py>​ +
-from .main import Application +
- +
-app = Application() +
-</​code>​ +
- +
-<code python 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() +
-</​code>​ +
- +
-<code python app/​controllers/​a.py>​ +
-class ControllerA:​ +
- +
-    def hello(self):​ +
-        print("​Hello,​ I'm Controller A") +
-</​code>​ +
- +
-<code python app/​controllers/​b.py>​ +
-class ControllerB:​ +
- +
-    def hello(self):​ +
-        print("​Hello,​ I'm Controller B") +
-</​code>​ +
- +
-===== Exemple ​=====+
  
-<code python ​DOSSIER>+<code python>
 #/​home/​marc/​test #/​home/​marc/​test
 ├── # app  ​ ├── # app  ​
Ligne 81: Ligne 28:
 │   ​│ ​                  ​print("​Hello,​ I'm Controller B") │   ​│ ​                  ​print("​Hello,​ I'm Controller B")
 │   │ │   │
-│   ​├── # __init__.py+│   ​├── # main.py
 │   │ │   │
-│   ​│ ​      ​from .main import ​Application+│   ​│ ​  ​from .controllers.a import ControllerA 
 +│   ​│ ​  from .controllers.b ​import ​ControllerB
 │   │ │   │
-│   ​│ ​      app = Application()+│   ​│ ​  class Application
 +│   ​│ ​      def __init__(self)
 +│   ​│ ​          ​self.name = "​Mauricette"​
 │   │ │   │
-│   └── # main.py +│   ​│ ​      def show(self): 
-│ +│   │           ControllerA() 
-│       from .controllers.import ​ControllerA +│   │           a.hello() 
-│       from .controllers.b import ControllerB +│   ​│           b = ControllerB() 
- +│   │           b.hello() 
-│       class Application:​ +│   │ 
-│           def __init__(self): +│   ​└── # __init__.py
-│               self.name = "​Mauricette"​+
  
-│           def show(self): +│           from .main import Application 
-│               a = ControllerA() +│     
-│               a.hello() +│           app Application()
-│               ​b ​ControllerB() +
-│               ​b.hello()+
  
 └── # run.py └── # run.py
python/modules_packages_import.1606552517.txt.gz · Dernière modification: 2020/11/28 08:35 (modification externe)