2011-05-04 11 views
5

con Python 2.7 ottengo questo errore:AttributeError: oggetto 'modulo' non ha nessun attributo 'maketrans' durante l'esecuzione 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' 

durante l'esecuzione di questo semplice codice:

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

utilizzando questa chiamata:

$ python -m cProfile string.py 

sto usando Ubuntu Natty Narwhal, e installato il pacchetto python-profiler (non so se questo è neces Sary).

risposta

7

Come il Python tutorial on modules spiega:

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 fa import string. Lo script è denominato string.py e viene prima (o almeno prima delle directory stdlib) nel percorso di ricerca, quindi viene importato. Ma non definisce le funzioni e le costanti attese, ad es. non ha un modulo maketrans. Questo è ciò che ti dice l'errore.

(Lo stesso errore dovrebbe verificarsi solo se si esegue lo script, senza profilazione.)

+8

Morale della storia: mai chiamare il programma stesso come modulo stdlib. – jathanism

+0

Wow, questo è un punto intelligente, lo controllerò domani e segnerò come risposta corretta. – Doppelganger

Problemi correlati