Help us understand the problem. What is going on with this article?

Pythonのimportについて整理

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__ 関数を使う書き方もあるそうです。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした