18

I am trying to import the cProfile module into Python 3.3.0, but I got the following error:

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'

The complete code (cProfile_try.py) is as follows

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)')
Asclepius
  • 57,944
  • 17
  • 167
  • 143
alittleboy
  • 10,616
  • 23
  • 67
  • 107
  • 2
    Do you have a `cProfile.py` in the current directory or elsewhere on `sys.path` that's searched before the standard library? Print the value of `cProfile.__file__`. – Eryk Sun Apr 15 '13 at 02:37
  • @eryksun: I think cProfile is a module to be imported in the session, right? After importing, `cProfile.run` should be available... – alittleboy Apr 15 '13 at 02:50
  • @eryksun: thanks! I got this `/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py` as the `print(cProfile.__file__)` output. – alittleboy Apr 15 '13 at 06:31
  • @eryksun: yes... I use python 2.7 instead. For python 3.3, I didn't get any output – alittleboy Apr 15 '13 at 07:01
  • Since 3.3 is your problem it doesn't help to show 2.7. They're completely separate installations. OK, try this instead in 3.3: `import imp; print(imp.find_module('cProfile')[1])` – Eryk Sun Apr 15 '13 at 07:06
  • @eryksun: I got `/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/cProfile.py` – alittleboy Apr 15 '13 at 07:13
  • Compare that file to the [original](http://hg.python.org/cpython/file/bd8afb90ebf2/Lib/cProfile.py). Is it missing the `run` function? It could also be a bad `cProfile.cpython-33.pyc` in the `__pycache__` subdirectory. There isn't much else I can do to help. You need someone experienced with OS X to talk you through fixing this -- that or try reinstalling 3.3. – Eryk Sun Apr 15 '13 at 07:40
  • @eryksun: I checked the file and it has `def run(self, cmd): ` in it... Anyway, thanks so much for your help ;-) – alittleboy Apr 15 '13 at 15:14
  • 23
    Make sure you don't have a `profile.py` somewhere in your directory, because `cProfile` itself will then import the wrong profile module. – chtenb Dec 29 '14 at 10:24

2 Answers2

41

As noted in a comment, it is likely that there exists a file named profile.py, possibly in the current directory. This file is being unintentionally used by cProfile, thereby masking Python's profile module.

A suggested solution is:

mv profile.py profiler.py

Next, for good measure,

If using Python 3:

rm __pycache__/profile.*.pyc

If using Python 2:

rm profile.pyc
Asclepius
  • 57,944
  • 17
  • 167
  • 143
-2

try to use "import profile as cProfile"

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 24 '21 at 08:55
  • Why would they want to do that? They are trying to import the ***built-in*** module `cProfile`. How would it help to import something completely different and name it the same? – Tomerikoo Oct 24 '21 at 11:28