Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cProfile has no attribute runctx

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.

like image 282
Rafael Marques Avatar asked Sep 03 '25 06:09

Rafael Marques


1 Answers

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)

like image 196
cfcm Avatar answered Sep 04 '25 18:09

cfcm