2013-04-15 18 views
7

Sto cercando di importare il modulo Cprofile in Python 3.3.0, ma ho ottenuto il seguente errore:non può importare Cprofile in Python 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' 

Il codice completo (cProfile_try.py) è il seguente

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 hai un 'cProfile.py' nella directory corrente o altro dove su 'sys.path' che è cercato prima della libreria standard? Stampa il valore di 'cProfile .__ file__'. – eryksun

+0

@eryksun: Penso che cProfile sia un modulo da importare nella sessione, giusto? Dopo l'importazione, 'cProfile.run' dovrebbe essere disponibile ... – alittleboy

+0

@eryksun: grazie! Ho ottenuto questo '/ System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py' come output' print (cProfile .__ file __) '. – alittleboy

risposta

8

Come indicato in un commento, è probabile che esista inaspettatamente un file denominato profile.py, possibilmente nella directory corrente. Questo file viene utilizzato involontariamente da cProfile, mascherando in tal modo il modulo profile di Python.

Una suggerito soluzione è:

mv profile.py profile_action.py 

Avanti, per buona misura,

Se si utilizza Python 3:

rm __pycache__/profile.*.pyc 

Se si utilizza Python 2:

rm profile.pyc 
Problemi correlati