Python: モジュールの実装パス
モジュール名からファイルを見つける Pythonのモジュール名が与えられた時、そのモジュールの実装されているファイルのパスを調べるには、以下のようにします。 1 2 3 4 5 6 7 import importlib.util def get_module_path(module_name): spec = importlib.util.find_spec(module_name) if spec is None: return None return spec.origin 上記のように、importlib.util.find_spec()関数を使って、モジュール名からspecオブジェクトを取得し、spec.origin属性を参照することで、モジュールが実装されているファイルのパスを取得することができます。 ソース: Bing との会話 2023/4/23 (1) 6. モジュール — Python 3.11.3 ドキュメント. https://docs.python.org/ja/3/tutorial/modules.html. (2) Pythonで独自モジュールのディレクトリパスを通す方法 …. https://lightgauge.net/language/python/add-module-path. (3) 【Python】パッケージ・モジュールの検索と import - Qiita. https://qiita.com/shinsa82/items/5004a3ea63594f20190d. Djangoのアプリケーションの場合 1 2 3 4 5 6 7 8 from django.apps import apps class Def: @property def path(self): app_label = self.__module__.split(".")[0] conf = apps.get_app_config(app_label) return conf.path