0

学習用に自分で作成したカスタム モジュールをインストールしました。私が行った場合

from my_first_module import test
test.thisprintssomething()

関数は機能しますが、そうすると

import my_first_module
test.thisprintssomething()

Python が吐き出す、NameError: name 'test' is not defined. 「from」を使用せずにインポートするにはどうすればよいですか?

編集:

私はそれを自分で修正しました。my_first_module モジュールのinit .py に「インポート テスト」行を追加するのを忘れていました。

4

2 に答える 2

6

インポートしているのでmy_first_module 、コードを伝える必要があります,,testに属していますmy_first_module

import my_first_module

my_first_module.test.thisprintssomething()

より明確にするために、Python モジュールのインポートをご覧ください。

于 2012-08-27T09:53:37.500 に答える
3
import my_first_module

my_first_module.test.thisprintssomething()

これは機能します。詳細については、http://docs.python.org/tutorial/modules.htmlを参照してください。

于 2012-08-27T09:54:40.730 に答える