[Python] Loading multi-level self-made modules

Premise

A memo on how to write __init__.py when the structure is as follows.


root/  ├ __init__.py  ├ dir_a/  │  ├ __init__.py │ └ test_a.py (func_a is defined)  └ dir_b/     ├ __init__.pytest_b.py (func_b is defined)


After doing ʻimport root, I want to be able to call it with root.func_a`. Since there was such a thing, I will realize this.

init directly under root

For root / __ init__.py

root/__init__.py


import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from root.dir_a import *
from root.dir_b import *

dir_a init

For root / dir_a / __ init__.py

root/dir_a/__init__.py


from dir_a.test_a import *

dir_b init

Make root / dir_b / __ init__.py similar to root /dir_a/__init__.py

root/dir_b/__init__.py


from dir_b.test_b import *

Summary

All you are doing is calling in sequence. The pass is troublesome.

Recommended Posts

[Python] Loading multi-level self-made modules
List of python modules
Python self-made class sort
Python packages and modules
Dynamic loading of modules
x86 compiler self-made with python
Understand Python packages and modules
Faster loading of Python images
Introductory Python Modules and conditional expressions
Python Basic Course (14 Modules and Packages)
RPC completed with standard Python3 modules
Logistic regression analysis Self-made with python
Check installed modules from Python scripts