83일차 2023-07-05

2023. 7. 6. 01:22Python

PYTHON

ModuleNPackage07 폴더 생성

 

module1.py

#※같은 모듈을 여러번 import해도 의미 없다 딱 한번만 import된다
print('module1.py:',__name__,sep='')#핸재 파일을 import하지 않고 실행시(__main__)
#변수
PI=['파이',3.14]

결과)

 

module_basic.py

'''
모듈 도큐먼트화 주석
현재 모둘(파일)에 대한 소개글을 적는다
'''
print('[현재 모듈의 이름공간(Namespace) 출력]')
x=10#x라는 변수명으로 이름공간에 추가됨
print(dir())#현재 모듈의 이름공간을 리스트로 반환
print(__file__)#현재 파일(혹은 모듈)의 위치
print(__doc__)#현재 모듈의 도큐먼트화 주석 출력
print(__name__)#현재 파일 실행시는 __main__ ,다른 스크립트 파일(.py)에서 import해서 모듈로 사용시는 파일명(.py제외)
'''
import시 모듈 읽어오는 순서
1.현재 디렉토리
2.환경변수
3.sys.path에 있는 내장 모듈 및 표준 라이브러리 경로
'''
import sys,os
print(dir(sys))
#print(sys.__file__)#__file__이 없다 즉 sys라는 모듈는  C언어로 만들어져 파이썬에 내장된 모듈로 .py파일(모듈형태)로 제공되지 않는다
print(sys.__doc__)
print(sys.__name__)#sys
print(dir(os))
print(os.__file__)
print(os.__doc__)
print(os.__name__)#os
print(sys.path)

 

결과)

 

usemodule1.py

#use_module1.py:모듈 module1.py를 import해서 사용하기
#모듈 불러오기:import 모듈명(.py를 제외한 파일명)
import module1
print('-' * 30)
import  module1#최초 import한 것만 유효. 의미없다.즉 한번만 import하면 된다
print(dir(module1))
#print(PI)#NameError: name 'PI' is not defined
print(module1.PI)

결과)

 

use_module1.py

 

결과)

 

module2.py

 

결과)

 

use_module2.py

 

결과)

 

usemodule2.py

 

결과)

 

use_module3.py

 

결과)

 

use_moduleNo3.py

 

결과)

 

use_module4.py

 

결과)

 

usemodule4.py

 

결과)

 

exception1.py

 

결과)

 

exception2.py

 

결과)

 

exception3.py

 

결과)

 

exception4.py

 

결과)

 

exception5.py

 

결과)

 

file1.py

 

결과)

 

file2.py

 

결과)

 

file3.py

 

결과)

 

file4.py

 

결과)

 

file5.py

 

결과)

 

file6.py

 

결과)

 

file7.py

 

결과)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'Python' 카테고리의 다른 글

85일차 2023-07-07  (0) 2023.07.07
84일차 2023-07-06  (0) 2023.07.06
82일차 2023-07-04  (0) 2023.07.04
81일차 2023-07-03  (0) 2023.07.03
80일차 2023-06-30  (0) 2023.06.30