[Python] 12. 만든 모듈 추가하기 From Import

2019. 4. 2. 14:28Python관련/Python

모듈 추가하기 from import 

 

프로그램을 만들다 보면 코드가 엄청 길어질 수가 있다 이때 너무 길어지는 것보다는 기능별로

 

파일로 쪼개는게 관리하기 편하고 또한 재사용하기 쉬운 방법인 것 같다. 

 

 

01. 모듈 생성

module_A.py

 

#!/usr/bin/python3 python3
import subprocess


def execute(cmd) :
    fd = subprocess.Popen(cmd, shell=True,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
    return fd.stdout, fd.stderr

 

 

02. 모듈 사용

같은 위치에 있을 때를 가정한다. 

 

구조는 이렇다 앞에 from 모듈 위치 from 함수명 이때 모든 함수를 사용할 경우 *를 하면 된다.

예제)

from .module_A from execute

 

03. 함수 사용

 

execute()

 

'Python관련 > Python' 카테고리의 다른 글

[Python] 11.Thread  (0) 2019.03.14
[Python] 10. PIPE  (0) 2019.03.14
[Python] 09. 함수  (0) 2019.03.14
[Python] 08. 반복문  (0) 2019.03.14
[Python]07. 컨테이너  (0) 2019.03.11