LoginSignup
14
13

More than 3 years have passed since last update.

Pythonで"TypeError: 'module' object is not callable"エラー

Last updated at Posted at 2019-05-25

サブフォルダimportでハマりました。。。
解決法備忘録。

結論

親ファイルからexecute01()のように呼び出さない。
execute01.main()で呼び出す。

例で説明

フォルダとファイル構成

Hogefolder
mainHoge.py
Executefolder
 ┣execute01.py
 ┣execute02.py
 ┣execute03.py

mainHoge.pyからExecutefolder内のpytyonファイルを呼び出す構成。

pythonコード

OK例とNG例。

これだといけない :cry:

親ファイルからexecute01()のように呼び出す。
エラー吐きます。

mainHoge.py
from Executefolder import execute01, execute02, execute03 

execute01()
execute02()
execute03()
execute01.py

# 処理を記述
num = 1+2

実行します。エラー吐きます。

cmd
$ python3 mainHoge.py


TypeError: 'module' object is not callable

これだといける :smile:

親ファイルからexecute01()と呼び出さない。
execute01.main()で呼び出す。

mainHoge.py
from Executefolder import execute01, execute02, execute03 

execute01.main()
execute02.main()
execute03.main()
execute01.py
def main():
    # 処理を記述
    num = 1+2

if __name__ == '__main__':
    main()

実行します。正常に動くはずです。

cmd
$ python3 mainHoge.py

if __name__ == '__main__':はよく理解できてない。。。omajinai

14
13
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
14
13