개발(IT)/Python(파이썬)

파이썬(Python) 패키지(Package)

isony 2024. 11. 16. 10:02
반응형

파이썬(Python) 패키지(Package)

- 패키지(Package)는 하나의 폴더에 여러 모듈을 모아 놓아 사용하는 구조입니다.

- 프로젝트성 프로그램을 만들때 패키지로 만들면 각 기능을 여러 모듈로 체계적으로 나누어서 관리할 수 있어 효율적으로 프로그램을 작성할 수 있습니다.

 

< 패키지(Package) 구조 >

 

(형식1)
import 패키지명[.하위폴더명].모듈명

패키지명[.하위폴더명].모듈명.변수
패키지명[.하위폴더명].모듈명.함수()
패키지명[.하위폴더명].모듈명.클래스()


(형식2)
from 패키지명[.하위폴더명] import 모듈명

모듈명.변수
모듈명.함수()
모듈명.클래스()


(형식3)
from 패키지명[.하위폴더명].모듈명 inport 변수명/함수명/클래스명

변수명
함수명()
클래스명()


(형식4)
from 패키지명[.하위폴더명].모듈명 import *


(형식5)
import 패키디명[.하위폴더명].모듈명 as 별명
from 패키지명[.하위폴더명] import 모듈명 as 별명
from 패키지명[.하위폴더명].모듈명 import 변수명/함수명/클래스명 as 별명

 

(예제1)

%%writefile c:/Test/io_file/img_read.py

default_image = "=> variable 'default_image' in img_read module"

def png_read():
    print("- png_read() in img_read module")

def jpg_read():
    print("- jpg_read() in img_read module")

(예제2)

%%writefile c:/Test/pkg_test.py

from io_file import img_read

img_read.png_read()
img_read.jpg_read()

print(img_read.default_image)


import io_file.img_read as io_read

io_read.png_read()
io_read.jpg_read()


from io_file import img_read as img

img.png_read()
img.jpg_read()


from io_file.img_read import png_read as png_img
from io_file.img_read import jpg_read as jpg_img
from io_file.img_read import default_image as dft_img

png_img()
jpg_img()

print(dft_img)



(결과)
- png_read() in img_read module
- jpg_read() in img_read module
=> variable 'default_image' in img_read module
- png_read() in img_read module
- jpg_read() in img_read module
- png_read() in img_read module
- jpg_read() in img_read module
- png_read() in img_read module
- jpg_read() in img_read module
=> variable 'default_image' in img_read module

 

반응형