モジュールをインポート

Python

カテゴリー: Data Structures  閲覧数:265 配信日:2018-02-15 11:20


インポート実行


作成したモジュール
・他のモジュールからimportを使ってインポートすることが出来る
import モジュール名

具体例
・a.pyをインポート
・拡張子の.pyは不要
import a      # モジュールをインポート

インポート後


クラスを使う場合
・モジュール名を先頭に付けクラス名をドットでつなげる
モジュール名.クラス名

メソッドを使う場合
・モジュール名を先頭に付けメソッド名をドットでつなげる
モジュール名.メソッド名

.変数を使う場合
・モジュール名を先頭に付け.変数名をドットでつなげる
モジュール名..変数名

具体例


▼greeting_module.py
class GreetingClass:
   def sayStr(self, str):
       print str


▼index.py
import greeting_module      # モジュールをインポート

greeting = greeting_module.GreetingClass()
greeting.sayStr("Hello")       # Hello



__main__ — トップレベルのスクリプト環境
モジュール (module)

週間人気ページランキング / 6-21 → 6-27
順位 ページタイトル抜粋 アクセス数
1 Python用語 16
2 インポート | インポートシステム 1
2 AttributeError: type object 'datetime.datetime' has no attribute 'datetime' | AttributeError(エラー) 1
2026/6/28 5:05 更新