2011-05-04 8 views
5

을 실행하는 동안 '모듈'개체가 어떤 속성 'maketrans'을주지 않습니다AttributeError :이 오류 파이썬 2.7을 사용하여 cprofile 명령을

Traceback (most recent call last): 
    File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main 
    "__main__", fname, loader, pkg_name) 
    File "/usr/lib/python2.7/runpy.py", line 72, in _run_code 
    exec code in run_globals 
    File "/usr/lib/python2.7/cProfile.py", line 199, in <module> 
    main() 
    File "/usr/lib/python2.7/cProfile.py", line 165, in main 
    from optparse import OptionParser 
    File "/usr/lib/python2.7/optparse.py", line 77, in <module> 
    import textwrap 
    File "/usr/lib/python2.7/textwrap.py", line 32, in <module> 
    class TextWrapper: 
    File "/usr/lib/python2.7/textwrap.py", line 74, in TextWrapper 
    whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace)) 
AttributeError: 'module' object has no attribute 'maketrans' 

이 간단한 코드를 실행하는 동안이 호출을 사용하여

def blah(): 
    orig = "" 
    for i in range(1000000): 
     orig += "zim"; 
blah() 

를 :

$ python -m cProfile string.py 

나는이 neces 경우 나도 몰라 (우분투 단정 한 일각 고래를 사용하여 패키지 파이썬 - 프로파일을 설치하고있어 sary). Python tutorial on modules으로

답변

7

설명 :

Actually, modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script (or the current directory), PYTHONPATH and the installation- dependent default. This allows Python programs that know what they’re doing to modify or replace the module search path. Note that because the directory containing the script being run is on the search path, it is important that the script not have the same name as a standard module, or Python will attempt to load the script as a module when that module is imported.

textwrapimport string 않습니다. 스크립트의 이름은 string.py이며 검색 경로에서 첫 x 째 (또는 적어도 stdlib 디렉토리 앞에 있 음)이므로 가져옵니다. 그러나 예상되는 함수 및 상수를 정의하지는 않습니다. maketrans 모듈이 없습니다. 그것이 오류가 말하는 것입니다.

(방금 프로파일없이 스크립트를 실행하는 경우 같은 오류가 발생합니다.) 이야기의

+8

도덕적 : 당신의 프로그램 다음 stdlib 모듈과 동일한 이름을하지 마십시오. – jathanism

+0

와우, 그건 영리한 점입니다. 내일 확인하고 올바른 대답으로 표시하겠습니다. – Doppelganger

관련 문제