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:24]
marclebrun
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 1 =====
-    print(app.name) +
-    app.show() +
-</​code>​+
  
-<code python ​app/​__init__.py+<code python>​ 
-from .main import Application +#/home/​marc/​test 
- +├── # app   
-app = Application() +│   ​├── # controllers 
-</code> +│   ​│ ​  ​├── # a.py 
- +│   ​│ ​  │ 
-<code python ​app/​main.py>​ +│   ​│ ​  ​│ ​      class ControllerA:​ 
-from .controllers.a import ControllerA +│   ​│ ​  ​│ ​          def hello(self):​ 
-from .controllers.b import ControllerB +│   ​│ ​  ​│ ​              print("​Hello,​ I'm Controller A") 
- +│   ​│ ​  │ 
-class Application:​ +│   ​│ ​  ​└── # b.py 
-    def __init__(self):​ +│   │ 
-        self.name = "​Mauricette"​ +│   ​│ ​          class ControllerB:​ 
- +│   ​│ ​              def hello(self):​ 
-    def show(self):​ +│   ​│ ​                  print("​Hello,​ I'm Controller B") 
-        a = ControllerA() +│   │ 
-        a.hello() +│   ├── ​# main.py 
-        b = ControllerB() +│    
-        b.hello() +│   ​│ ​  from .controllers.a import ControllerA 
-</​code>​ +│   ​│ ​  from .controllers.import ControllerB 
- +│   │ 
-<code python app/​controllers/​a.py> +│   ​│ ​  class Application:​ 
-class ControllerA:​ +│   ​│ ​      def __init__(self):​ 
- +│   ​│ ​          self.name = "​Mauricette"​ 
-    ​def hello(self):​ +│    
-        print("​Hello,​ I'm Controller A") +│   │       def show(self):​ 
-</​code>​ +│   │           a = ControllerA() 
- +│   ​│ ​          a.hello() 
-<code python app/​controllers/​b.py> +│   │           b = ControllerB() 
-class ControllerB:​ +│   ​│ ​          b.hello() 
- +│   │ 
-    ​def hello(self):​ +│   ​└── ​# __init__.py 
-        print("​Hello,​ I'm Controller B") +│ 
-</​code>​ +│           from .main import Application 
- +│     
-===== Exemple 2 ===== +│           app = Application() 
- + 
-<code python DOSSIER>​ +└── ​run.py
-/​home/​marc/​test +
-├── ​app  ​ +
-│   ├── controllers +
-│   ​│ ​  ├── a.py +
-│   ​│ ​  └── ​b.py +
-│   ├── services +
-│   ​│ ​  └── ..+
-│   ├── __init__.py +
-│   ​└── ​main.py +
-└── run.py+
  
         from app import app         from app import app
Ligne 83: Ligne 57:
             app.show()             app.show()
 </​code>​ </​code>​
- 
-<​code>​ 
-/​home/​marc/​test 
-├── app 
-│   ​├── controllers 
-│   ​│ ​  ​├── a.py 
-│   ​│ ​  ​└── b.py 
-│   ​├── services 
-│   ​│ ​  ​└── ... 
-│   ​├── __init__.py 
-│   ​└── main.py 
-└── run.py 
-</​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>​ 
- 
  
python/modules_packages_import.1606551857.txt.gz · Dernière modification: 2020/11/28 08:24 (modification externe)