[Python] How to import the library

What is a Python module?

A function or class once created is divided into files for reuse. Each .py file is called module or library. In Python, the module name is the part of the file name without the extension. By importing each .py file, it is possible to use the functions and classes in the module.

Module import method (basic)

Here's the most basic import method.

test.py


class AAA:
  def __init__(self, a):
    self.a = a
  def get_a(self):
    return self.a
  def add_a(self):
    self.a += 1

main.py


# import "Module name"
import test

#Functions of the imported module(Method)And when using classes
A = test.AAA(1) #Module name.Class name or function name
A.add_a() #instance.Method name
b = A.get_a # b=2

Give it a name when importing

By giving a name, you can omit long module names. Example) import pandas as pd   import numpy as np

main.py


# import "Module name" as "The name you want to give"
import test as t

A = t.AAA(1)  #Name given.Class name or function name
A.add_a() #instance.Method name
b = A.get_a # b=2

Import directly

Since the notation of the module name can be omitted, it is done in the class that is frequently used. Example) from keras.layers import Conv2D   from PIL import Image, ImageDraw

main.py


# from "Module name" import "Class name or function name"
from test import AAA

A = AAA(1)  #Class name or function name

Import all

Easy to import in large quantities from modules. All functions and classes in the module are imported. Example) from PIL import *

main.py


# from "Module name" import *
from test import *

A = AAA(1)  #Class name or function name

Import from package

A directory containing multiple modules is called package.

main.py


# ~~~Directory structure~~~
#Current directory-src-test.py
#                   └main.py

# import "package name.Module name"
import src.test
A = src.test.AAA(1)  #package name.Module name.Class name or function name

# or

# from "package name" import "Module name"
from src import test
A = test.AAA(1)  #Module name.Class name or function name

Python standard library

The one who seems to use it often

Module name Overview
os OS related
os.path Path related
sys Python runtime environment
io Input / output
datetime Date and time
calendar calendar
time time
re Regular expression
math Math
random random number
statistics statistics
json json
csv csv
sqlite3 SQLite
zipfile zip file
tarfile tar file
html HTML
html.paser HTML analysis
http HTTP
urllib URL
urllib.request URL request
socket socket

Recommended Posts

[Python] How to import the library
How to use the C library in Python
[Reintroduction to python] How to import via the parent directory
How to use Requests (Python Library)
How to get the Python version
[Python] How to use the graph creation library Altair
How to debug the Python standard library in Visual Studio
How to import Python library set up in EFS to Lambda
[python] How to use the library Matplotlib for drawing graphs
[Python] How to use import sys sys.argv
How to install Python
How to install python
How to use import
[Beginner memo] How to specify the library reading path in Python
Summary of how to import files in Python 3
How to use Python Image Library in python3 series
How to use the graph drawing library Bokeh
[Algorithm x Python] How to use the list
How to erase the characters output by Python
How to get the files in the [Python] folder
[AWS / Lambda] How to load Python external library
[2020.8 latest] How to install Python
How Python module import works
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
[Python] Another way to import
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to increase the axis
How to start the program
How to use Python bytes
[Python] How to remove duplicate values from the list
How to retrieve the nth largest value in Python
How to get the variable name itself in python
Think about how to program Python on the iPad
[Introduction to Python] How to iterate with the range function?
[Python] How to specify the download location with youtube-dl
I wanted to use the Python library from MATLAB
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Python] Summary of how to specify the color of the figure
How to use the model learned in Lobe in Python
[Introduction to Python] How to stop the loop using break?
[Python] How to rewrite the table style with python-pptx [python-pptx]
How to enjoy Python on Android !! Programming on the go !!
How to use the Rubik's Cube solver library "kociemba"
[Introduction to Python] Basic usage of the library matplotlib
[Python] How to output the list values in order