LoginSignup
4
6

More than 5 years have passed since last update.

Pythonでサブディレクトリにある.pyファイルをよみこみたいとき

Last updated at Posted at 2018-01-28

前提条件

たとえば./pyfile/readfile.pyがあって中で class readdataが定義してあるとき、./main.pyからよびだす方法

方法1

./main.pyでpathを追加

import sys,os
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/pyfile')

from readfile import readdata

方法2

追記 コメントありがとうございます。こっちのほうがいい

空のファイル./pyfile/__init__.pyを作成する。./main.pyはこれだけ


from pyfile.readfile import readdata
4
6
1

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
4
6