About Python external module import <For super beginners>

Introduction

When writing python, the following description written as import almost always appears.

import sys

I used to copy and paste it as a magic, but gradually I couldn't avoid it, so I checked it properly.

table of contents

--What is a module? --Basic import --import using from --How to import multiple objects (functions) -Import using * (wildcard) --Difference between module loading and object loading --Import by alias


What is a module?

In Python, a file in which functions and classes are written together is called a module. It's a .py file. It seems that a collection of multiple modules is called a library. There is a standard library in Python, which can be used by importing it.

Basic import

It is ʻimport to load that module. If you do ʻimport <module name> as shown below, the module will be imported as a module type object.

import math

You can see that math is imported as a module by printing with type.

import math
print type(math)

>>>  
<type 'module'> 
>>>

import using from

It is also possible to directly import functions and classes in the module. You can import with from <module name> import <function name>. The imported function can be executed by specifying the name. Not only functions but also variables and classes may be imported, so they are referred to as below.

from math import pi 
print(pi) 
>>>
3.141592653589793
>>>

If you specify an object and import it, the math module is not imported.

from math import pi 
print type(math)

>>> 
NameError: name 'math' is not defined
>>> 

How to import multiple objects

It is also possible to import multiple objects at the same time.

from math import pi, radians

When loading the module, please note that the following description is recommended in PIP8.

# NG 
import os, sys 
# OK 
import os 
import sys

Import using * (wildcard)

All objects can be imported using * (wildcard). However, it seems that it is not recommended because it is a slightly rough import method.

from math import *

Difference between module loading and object loading

The description at runtime differs between when only the module is loaded and when the object is loaded.

When loading only modules

When only the module is loaded, it is necessary to specify <module name>. <Object name> when executing the function.

import math
print(math.pi)#<Module name>.<Object name>

When loading an object

When an object is read, it can be executed by specifying only <object name>.

from math import pi
print(pi)#<Object name only>

Mistake example

If only the module is loaded and only the object name is specified directly, an error will occur.

import math
print(pi)

Import by alias

It is also possible to call a module or object with any name.

import math as m
print(m.pi)
from math import pi as pai
print(pai)

Referenced site

https://note.nkmk.me/python-import-usage/

Recommended Posts

About Python external module import <For super beginners>
Python for super beginners Python #functions 1
Python #list for super beginners
Python #index for super beginners, slices
Python #len function for super beginners
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
Python module import
Let's put together Python for super beginners
About Python for loops
Python for super beginners Python for super beginners # Easy to get angry
python original module import
About Python, for ~ (range)
python textbook for beginners
Easy understanding of Python for & arrays (for super beginners)
OpenCV for Python beginners
How to convert Python # type for Python super beginners: str
Python # How to check type and type for super beginners
How Python module import works
About Fabric's support for Python 3
About the Python module venv
About python beginner's memorandum function
Python3 environment construction (for beginners)
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
~ Tips for beginners to Python ③ ~
About "for _ in range ():" in python
Check for external commands in python
Python Exercise for Beginners # 2 [for Statement / While Statement]
About Python, from and import, as
<For beginners> python library <For machine learning>
Beginners use Python for web scraping (1)
[Python] Reasons for overriding using super ()
Run unittests in Python (for beginners)
Beginners use Python for web scraping (4) ―― 1
Looking for location for mercurial python module
INSERT into MySQL with Python [For beginners]
[Kaggle for super beginners] Titanic (Logistic regression)
[Python] Minutes of study meeting for beginners (7/15)
python3 How to install an external module
Memorandum of python beginners About inclusion notation
[Python] Import the module one level higher
Use Python external module on Sakura Internet
[Python] Read images with OpenCV (for beginners)
About import
Atcoder standard input set for beginners (python)
Module import and exception handling in python
[For beginners] Try web scraping with Python
Import Error in Python3: No module named'xxxxx'
A textbook for beginners made by Python beginners
Memo # 4 for Python beginners to read "Detailed Python Grammar"
Solution for "Import Error: No module named requests"
The fastest way for beginners to master Python
Causal reasoning and causal search with Python (for beginners)
python> import seiral> ImportError: No module named serial
For new students (Recommended efforts for Python beginners Part 1)
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Play with Lambda layer (python) for about 5 minutes
Memo # 1 for Python beginners to read "Detailed Python Grammar"
~ Tips for Python beginners from Pythonista with love ① ~
[Python of Hikari-] Chapter 08-04 Module (Installation of external library)