私はこのファイル構造を持っています(ドットは私の作業ディレクトリです):
.
+-- testpack
+-- __init__.py
+-- testmod.py
testmod
ステートメントを使用してモジュールをロードすると、次のimport
中で宣言されている関数を呼び出すことができます。
>>> import testpack.testmod
>>> testpack.testmod.testfun()
hello
しかし、関数を使用して同じことをしようとすると、うまくいき__import__()
ません:
>>> __import__("testpack.testmod").testfun()
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
__import__("testpack.testmod").testfun()
AttributeError: 'module' object has no attribute 'testfun'
実際、testpack
モジュールの代わりにパッケージを返しますtestmod
:
>>> __import__("testpack.testmod").testmod.testfun()
hello
どうして?