4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonのimportについて整理

Last updated at Posted at 2019-10-21

Pythonの importの使い方が分かってなかったので整理。

import

import module_name
モジュールを指定して読み込む。


import utils
↑utilsを読み込む

モジュールの関数を呼び出し

import utils

utils.get_data()

公開されているライブラリも同じように読み込む。

###from import

import buildhouse

buildhouse.floor.material(wood='hinoki')

↑通常のimport。架空のライブラリbuildhouseを読み込んでいます。

from buildhouse.floor import material

material(wood='hinoki')

fromを使うと、短縮できる。

import xx as xx

import buildhouse as bl

bl.floor.material(wood='hinoki')

asでモジュール名を省略できる。

##参考サイト
https://docs.python.org/ja/3/reference/import.html
https://ai-inter1.com/python-module_package_library/
https://pg-chain.com/python-from-import#toc9

追記

コメント欄で教えて頂いているように、__import__ 関数を使う書き方もあるそうです。

4
2
3

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?