Cannot import cProfile in Python 3
Asked Answered
U

2

19

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)')
Uncurl answered 15/4, 2013 at 2:3 Comment(9)
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__.Sphenic
@eryksun: I think cProfile is a module to be imported in the session, right? After importing, cProfile.run should be available...Uncurl
@eryksun: thanks! I got this /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py as the print(cProfile.__file__) output.Uncurl
@eryksun: yes... I use python 2.7 instead. For python 3.3, I didn't get any outputUncurl
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])Sphenic
@eryksun: I got /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/cProfile.pyUncurl
Compare that file to the original. 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.Sphenic
@eryksun: I checked the file and it has def run(self, cmd): in it... Anyway, thanks so much for your help ;-)Uncurl
Make sure you don't have a profile.py somewhere in your directory, because cProfile itself will then import the wrong profile module.Sorgo
D
42

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
Dyl answered 6/3, 2017 at 18:46 Comment(1)
gosh, I called my test script profile.py. Good thing there wasn't a function run in it!Ungrudging
K
-2

try to use "import profile as cProfile"

Klusek answered 24/10, 2021 at 8:4 Comment(2)
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.Monogamist
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?Frolicsome

© 2022 - 2025 — McMap. All rights reserved.