2011-05-04 22 views
5

çalıştırırken 'modülü' nesne hiçbir özellik 'maketrans' vardır:AttributeError: bu hatayı alıyorum piton 2.7 Kullanarak 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' 

bu basit kod çalıştırırken: bu çağrıyı kullanarak

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

:

$ python -m cProfile string.py 

bu neces olup olmadığını bilmiyorum (Ubuntu Natty Narwhal kullanarak ve paket piton-profilleyiciyi yüklü ediyorum lamaktadır). Python tutorial on modules gibi

cevap

7

açıklıyor:

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 yapar. Komut dosyanız string.py olarak adlandırılır ve arama yolunda ilk olarak (veya en az stdlib dizinlerinden önce gelir) gelir, böylece içe aktarılır. Ancak, beklenen işlevleri ve sabitleri tanımlamaz, örn. maketrans modülüne sahip değil. Bu, hatanın size söylediği şey.

(sadece profilleme olmadan komut dosyasını çalıştırın, aynı hata ortaya olmalıdır.) Hikayenin

+8

Ahlak: Programınızı bir stdlib modülü ile aynı isim vermeyin. – jathanism

+0

Vay, bu akıllı bir nokta, yarın kontrol edip doğru cevap olarak işaretleyeceğim. – Doppelganger

İlgili konular