import 구문을 어디서나 사용
>>> def loadMathMod():
print("import math")
import math
print(dir(math))
>>> loadMathMod()
import math
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
from <모듈> import <어트리뷰트>
모듈 안에 정의된 어트리뷰트 중 해당하는 어트리뷰트를 현재의 이름 공간에 임포트함
바로 참조 가능
>>> from simpleset import union
>>> union([1,2,3],[3],[3,4])
[1, 2, 3, 4]
>>> intersect([1,2],[1]) << union만 사용할 수 있다.
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
intersect([1,2],[1])
NameError: name 'intersect' is not defined
새로 임포트되는 이름이 이미 이름공간에 있는 경우
>>> def union(a):
print(a)
>>> union
<function union at 0x103781848>
>>> union("test")
test
>>> from simpleset import union
>>> union
<function union at 0x10377aea8>
>>> union("test")
['t', 'e', 's']
새로이 임포트된 이름으로 대체
from <모듈> import *
모듈 내 이름 중 밑줄(_)로 시작하는 어트리뷰트(함수, 데이터)를 제외하고 모든 어트리뷰트를 현재의 이름공간으로 임포트함
>>> import simpleset
>>> dir(simpleset)
['WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '__builtins__', '__cached__', '__doc__', '__file__', '__intersectSC', '__name__', '__package__', 'cmp_to_key', 'difference', 'intersect', 'lru_cache', 'partial', 'reduce', 'total_ordering', 'union', 'update_wrapper', 'wraps']
>>> from simpleset import *
>>> dir()
['WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '__builtins__', '__doc__', '__name__', '__package__', 'cmp_to_key', 'difference', 'intersect', 'loadMathMod', 'lru_cache', 'partial', 'reduce', 'setA', 'setB', 'simpleset', 'total_ordering', 'union', 'update_wrapper', 'wraps']
import <모듈> as <별칭>
<모듈> 이름을 <별칭>으로 변경해서 임포트
>>> import xml.sax.handler as handler
>>> handler
<module 'xml.sax.handler' from '/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/xml/sax/handler.py'>