LoginSignup
1
2

More than 3 years have passed since last update.

【Python】1つ上の階層のモジュールをimport

Posted at

mydir
└ parent.py
  └ child.py

といったフォルダ構造にて,child.pyからparent.pyをimportする方法

child.py

import sys
import pathlib

# ひとつ上の階層の絶対パスを取得
parent_dir = str(pathlib.Path(__file__).parent.parent.resolve())

# モジュール検索パスに,ひとつ上の階層の絶対パスを追加
sys.path.append(parent_dir)

import parent

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2