Import로 함수 불러오기

황인환·2024년 6월 5일

tm.py

def hello():
    print("test success")

tmtest.py

import tm
tm.hello()
  • tm.py에 테스트할 함수를 만들고 tmtest.py에 import로 불러온 함수이다.

  • 파일명.함수명(파일명 안에 있는)으로 사용할 수 있다.

  • 파일명.함수명 말고 함수명만 쓰는법 ex) tn.hello() ->hello()
    from 파일명 import * 파일명에 있는 모든 걸 가져오겠다는 뜻이고
    from 파일명 import 가져올 함수명 으로도 사용할수 있지만 추천하지는 않는다. 다른 폴더에서도 가져오기 시작하면 헷갈릴 수도 있다.

  • 파일명이 길땐 as를 사용하면 된다.
    ex) tm 대신 a로 바꿈

import tm -> import tm as a  
tm.hello() -> a.hello()
  • import할때 import 폴더이름.py이름
    제일 큰폴더 순으로
    ex) test1 폴더 안에 test2 폴더가 있고 그안에 module.py라는 것이 있다면
    import test1.test2.module

--normaltic study 7주차--

0개의 댓글