Python/Language

[Python] 모듈

오늘도개발 2023. 8. 3. 16:58

1. 모듈이란?

  - 프로그램을 기능별로 파일을 나누어 유지보수의 효율성을 높일 수 있는 기능

 

2. 모듈 사용법

 

 - 다른 파일을 사용하여 코드를 작성 후 import 모듈이름 을 사용하여 불러온다.

 

   ex) import math

         print(math.pi)

 

   #부분적인 기능만 불러올 경우 

   ex) from math import pi

         print(pi)

   

3. 외부 모듈 사용법

 - 터미널에서 명령어 pip install 모듈이름 으로 해당 모듈을 다운로드 가능하다.

   

   ex) pip install scipy

 

4. as

 - 모듈을 불러올 때, 다음과 같이 모듈명을 변경하여 사용할 수 있다.

 

  ex) import math as m

         print(m.pi)

 

   ex) from math import pi as p

         print(p)

 

 

- 추가내용 (패키지내 모듈 불러오는 방법)

 

1. import 패키지 (패키지 내 모든 모듈 불러오기)

 

    import member.student

    member.student.view()

 

2. import 패키지.모듈 (패키지 내 지정 모듈 불러오기)

 

    import member.student

    member.student.view()

 

3. from 패키지 import * (패키지 내 모든 모듈 불러오기)

 

    from member import *

    student.view()

 

4. from 패키지 import 모듈 (패키지 내 지정 모듈 불러오기)

 

    from member import student

    student.view()

 

 

 

 

'Python > Language' 카테고리의 다른 글

[python] 함수  (0) 2023.08.15
[python] 메모리 할당과 복사  (0) 2023.08.10
[Python] 예외처리  (0) 2023.08.04
[Python] 클래스 상속  (0) 2023.08.03
[Python] 클래스  (0) 2023.08.03