Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Global variables not seen by #131

fperez opened this Issue · 5 comments

2 participants


The core problem, as reported by Benjamin Root, is that this code doesn't work in IPython trunk:

import cProfile
import math

x = 25"y = math.sqrt(x)")

This throws an exception NameError: name 'math' is not defined

In plain Python it runs OK.

For further details:

dwf commented

The problem is that cProfile (and also regular old profile) imports __main__ and examines uses its __dict__ for both globals and locals in "exec cmd in globals, locals". __main__ being a FakeModule instance without any of the IPython prompt namespace available is what makes it fail.

I know the reason that FakeModule exists has something to do with the namespace created for %run executions, but I'm not really sure if there's a way to override its...blankness, when running in interactive mode, without having all hell break loose. I should note also that putting that in a file and %running it works fine, but that's not really a surprise.

I was looking for some low-hanging fruit like the other pull requests I've sent but I think this one is out of my league... hopefully my detective work will let someone pick this up more quickly.


Thanks, David! I've bumped the priority for this one and made FakeModule label so we track all bugs related to this. It's true that this is not low-hanging fruit, but there are ways to help already: writing a set of tests that encapsulate all the known problems in test form would be a huge help. Pickling failures, this profile thing, etc. It would make the job of hunting the actual problem far far easier, right now part of the reason nobody has even tried is precisely that lacking any tests to know what 'fixing' means, we don't even start.

dwf commented

Good point, I will have a look at adding tests for this and the pickle bug (marked as KnownFail, I guess?). I should probably get back to studying for part of today, so probably tomorrow.


Fixed in master:

In [1]: import cProfile

In [2]: import math

In [3]: 

In [3]: x = 25

In [4]:"y = math.sqrt(x)")
         3 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {math.sqrt}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


@fperez fperez closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.