2013-04-15 36 views
7

Python 3.3.0 içine Cprofile modülü almak çalışıyorum ama şu hata var:Python Cprofile alınamıyor 3

import cProfile 
help(cProfile.run) 

L = list(range(10000000)) 
len(L) 
# 10000000 

def binary_search(L, v): 
    """ (list, object) -> int 

    Precondition: L is sorted from smallest to largest, and 
    all the items in L can be compared to v. 

    Return the index of the first occurrence of v in L, or 
    return -1 if v is not in L. 

    >>> binary_search([2, 3, 5, 7], 2) 
    0 
    >>> binary_search([2, 3, 5, 5], 5) 
    2 
    >>> binary_search([2, 3, 5, 7], 8) 
    -1 
    """ 

    b = 0 
    e = len(L) - 1 

    while b <= e: 
     m = (b + e) // 2 
     if L[m] < v: 
      b = m + 1 
     else: 
      e = m - 1 

    if b == len(L) or L[b] != v: 
     return -1 
    else: 
     return b 

cProfile.run('binary_search(L, 10000000)') 
aşağıdaki
Traceback (most recent call last): 
    File "<pyshell#7>", line 1, in <module> 
    import cProfile 
    File "/.../cProfile_try.py", line 12, in <module> 
    help(cProfile.run) 
AttributeError: 'module' object has no attribute 'run' 

tam kodu (cProfile_try.py) olarak

+2

Do: Python 2'yi kullanarak

rm __pycache__/profile.*.pyc 

ise Geçerli dizinde veya başka bir "cProfile.py" var Standart kütüphaneden önce aranan 'sys.path' nerede? CProfile .__ file__'nin değerini yazdırın. – eryksun

+0

@eryksun: cProfile oturumda içe aktarılacak bir modül olduğunu düşünüyorum, doğru mu? İçe aktardıktan sonra "cProfile.run" mevcut olmalıdır ... – alittleboy

+0

@eryksun: teşekkürler! Bu/System/Library/Frameworks/Python.framework/Sürümleri/2.7/lib/python2.7/cProfile.py' 'print (cProfile .__ file __)' çıktı olarak aldım. – alittleboy

cevap

8

Bir yorumda belirtildiği gibi, muhtemelen şu anki dizinde profile.py adlı bir dosya beklenmedik şekilde var olabilir. Bu dosya istemeden cProfile tarafından kullanılıyor, böylece Python'un profile modülünü maskeliyor.

bir çözümdür önerdi:

mv profile.py profile_action.py 

Sonraki, iyi ölçmek için,

Eğer kullanarak Python 3:

rm profile.pyc