Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@naogify

Pythonのimportについて整理

More than 1 year has passed since last update.

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

2
Help us understand the problem. What is going on with this article?
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
naogify
GISや人工衛星のデータを触ってます。本職はWebのエンジニアです。/OpenStreetMap/Osumium/Tellus/

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?