Ci-dessous, les différences entre deux révisions de la page.
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:33] 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 | + | |
- | + | ||
- | 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 2 ===== | + | |
- | + | ||
- | <code python DOSSIER> | + | |
#/home/marc/test | #/home/marc/test | ||
├── # app | ├── # app | ||
│ ├── # controllers | │ ├── # controllers | ||
│ │ ├── # a.py | │ │ ├── # a.py | ||
- | + | │ │ │ | |
- | class ControllerA: | + | │ │ │ class ControllerA: |
- | def hello(self): | + | │ │ │ def hello(self): |
- | print("Hello, I'm Controller A") | + | │ │ │ print("Hello, I'm Controller A") |
+ | │ │ │ | ||
│ │ └── # b.py | │ │ └── # b.py | ||
- | |||
- | class ControllerB: | ||
- | def hello(self): | ||
- | print("Hello, I'm Controller B") | ||
- | |||
- | │ ├── # __init__.py | ||
│ │ | │ │ | ||
- | │ │ from .main import Application | + | │ │ class ControllerB: |
+ | │ │ def hello(self): | ||
+ | │ │ print("Hello, I'm Controller B") | ||
│ │ | │ │ | ||
- | │ │ app = Application() | + | │ ├── # main.py |
│ │ | │ │ | ||
- | │ └── # main.py | + | │ │ from .controllers.a import ControllerA |
- | │ | + | │ │ from .controllers.b import ControllerB |
- | │ from .controllers.a import ControllerA | + | │ │ |
- | │ from .controllers.b import ControllerB | + | │ │ class Application: |
- | │ | + | │ │ def __init__(self): |
- | │ class Application: | + | │ │ self.name = "Mauricette" |
- | │ def __init__(self): | + | │ │ |
- | │ self.name = "Mauricette" | + | │ │ def show(self): |
+ | │ │ a = ControllerA() | ||
+ | │ │ a.hello() | ||
+ | │ │ b = ControllerB() | ||
+ | │ │ b.hello() | ||
+ | │ │ | ||
+ | │ └── # __init__.py | ||
│ | │ | ||
- | │ def show(self): | + | │ from .main import Application |
- | │ a = ControllerA() | + | │ |
- | │ a.hello() | + | │ app = Application() |
- | │ b = ControllerB() | + | |
- | │ b.hello() | + | |
│ | │ | ||
└── # run.py | └── # run.py |