2013-04-15 13 views
7

próbuję zaimportować moduł cProfile do Pythona 3.3.0, ale mam następujący błąd:Nie można importować cProfile w Pythonie 3

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' 

Kompletny kod (cProfile_try.py) jest następująca

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)') 
+2

Do masz 'cProfile.py' w bieżącym katalogu lub w innym gdzie na 'sys.path', który jest przeszukiwany przed biblioteką standardową? Wydrukuj wartość 'cProfile .__ file__'. – eryksun

+0

@eryksun: Myślę, że cProfile to moduł do zaimportowania w sesji, prawda? Po zaimportowaniu, powinien być dostępny plik 'cProfile.run' ... – alittleboy

+0

@eryksun: thanks! Mam '/ System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py' jako plik wyjściowy print (cProfile .__ __). – alittleboy

Odpowiedz

8

Jak wspomniano w komentarzu, istnieje prawdopodobieństwo, że plik o nazwie profile.py zostanie niespodziewanie znaleziony w bieżącym katalogu. Plik ten jest niechcący używany przez cProfile, zamaskowując w ten sposób moduł Pythona profile.

Sugerowane rozwiązanie:

mv profile.py profile_action.py 

Następnie, na dokładkę,

przypadku korzystania Python 3:

rm __pycache__/profile.*.pyc 

przypadku korzystania Python 2:

rm profile.pyc 
Powiązane problemy