2011-05-04 21 views
5

Używanie Pythona 2.7 otrzymuję ten błąd:AttributeError: 'moduł' obiekt ma atrybut 'maketrans' podczas uruchamiania 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' 

podczas uruchamiania ten prosty kod:

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

za pomocą tego połączenia:

$ python -m cProfile string.py 

używam Ubuntu Natty Narwhal, a zainstalowany pakiet python-Profiler (nie wiem, czy to jest niez sary).

Odpowiedz

7

Jako Python tutorial on modules wyjaśnia:

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.

textwrap robi import string. Twój skrypt nazywa się string.py i jest pierwszym (lub przynajmniej przed katalogiem stdlib) na ścieżce wyszukiwania, więc jest importowany. Nie określa jednak oczekiwanych funkcji i stałych, np. nie ma modułu maketrans. To właśnie mówi ci ten błąd.

(Ten sam błąd powinno nastąpić, jeśli tylko uruchomić skrypt bez profilowania.)

+8

Morał z tej historii: Nigdy nie nazwać swój program, tak samo jako moduł stdlib. – jathanism

+0

Wow, to sprytny punkt, sprawdzę to jutro i oznaczy to jako poprawną odpowiedź. – Doppelganger

Powiązane problemy