3

I am trying to learn my way around Cython, and I am following the official documentation. Recently, i tried to do the tutorial provided in "http://docs.cython.org/en/latest/src/tutorial/profiling_tutorial.html". The objective here is to profile a Cython document. This is where I got into trouble.

The function to be profiles is (file "calc_pi.py"):

def recip_square(i):
    return 1./i**2

def approx_pi(n=10000000):
    val = 0.
    for k in range(1,n+1):
        val += recip_square(k)
    return (6 * val)**.5

The script to profile the functions (as posted in the document) is:

import pstats, cProfile

import calc_pi

cProfile.runctx("calc_pi.approx_pi()", globals(), locals(), "Profile.prof")

s = pstats.Stats("Profile.prof")
s.strip_dirs().sort_stats("time").print_stats()

I am not exactly sure which command to run, and if this is what raises the error. However, in their page, there is no reference to thisi. So I simply run "python3 profile.py", which yields the follwing error:

AttributeError: module 'cProfile' has no attribute 'runctx'

I know that probably my error is stupid and minimum, but after googleing and checking stackoverflow for a while, I could not find the answer.

Thank you for your help.

Rafael Marques
  • 1,335
  • 4
  • 22
  • 35
  • 2
    Is it [this issue](https://stackoverflow.com/questions/16006602/cannot-import-cprofile-in-python-3) - i.e. are you importing a different module called `cProfile`? – DavidW Apr 01 '18 at 20:56

1 Answers1

9

I faced the same issue here.

The problem was the name of the file profile.py.

Just use a different name (as suggested in here)

cfcm
  • 139
  • 2
  • 10