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