2013-10-24 4 views
0

다시로드 할 때 많이 읽었지만 reload 함수를 사용할 수 없습니다. imp.py 자체에 오류가 있습니다. 변경하지 않았습니다.Python 3.3.2에서 다시로드 함수 사용

>>> import imp 
>>> imp.reload('fileread') 
Traceback (most recent call last): 
    File "<pyshell#1>", line 1, in <module> 
    imp.reload('fileread') 
    File "C:\Python33\lib\imp.py", line 258, in reload 
    raise TypeError("reload() argument must be module") 
TypeError: reload() argument must be module 

fileread는 python의 적절한 디렉토리에 저장됩니다.

답변

3

imp.reload()에 실제 모듈 개체를 전달해야합니다. 당신은 단지 모듈 이름이있는 경우

sys.modules mapping에서 모듈 오브젝트 조회 : 이미 수입 된 모듈에

import sys 
import imp 

imp.reload(sys.modules['fileread']) 

이 유일한 작품; 귀하의 항목 중 일부는 아직 수입되지 않은 경우, 최소한의 KeyError 다음을 건너 잡을 :

try: 
    imp.reload(sys.modules[modulename]) 
except KeyError: 
    # not loaded, no point in reloading 
    pass 

선택적으로, 대신 같은 모듈을로드 importlib.import_module()를 사용할 수 있습니다.

+0

>>> 수입 SYS >>> 수입 꼬마 도깨비 >>> imp.reload (sys.modules에 [ 'FILEREAD']) 역 추적 (마지막으로 가장 최근 통화) : 파일 "", 줄 1에서 imp.reload (sys.modules [ 'fileread']) KeyError : 'fileread'오류가 계속 발생합니다. – tcp

+0

@PavanRavishankar : 모듈을 가져 오지 않았습니다 *. 먼저'fileread'를 임포트하십시오; 그 시점에서 다시로드 할 필요가 없습니다. –

관련 문제