Instead of using run() use runctx() , which allows you to provide local and global variables. For instance:
>>> import cProfile >>> def g(x): ... print "g(%d)" % x ... >>> x=100 >>> cProfile.runctx('g(x)', {'x': x, 'g': g}, {}) g(100) 3 function calls in 0.000 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 <stdin>:1(g) 1 0.000 0.000 0.000 0.000 <string>:1(<module>) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} >>>
See also runctx() in this document .
source share