importing and nose

Peter Otten __peter__ at web.de
Thu Jan 19 13:09:52 EST 2012


Andrea Crotti wrote:

> On 01/19/2012 05:36 PM, Peter Otten wrote:
>>
>> I don't believe you.
> 
> Quite sure it does:
> 
> [andrea at precision test]$ cat simple.py
> import profile
> from os import path
> import sys
> 
> prof_path = path.join(path.dirname(__file__), 'profiling')
> sys.path.append(prof_path)
> import x
> 
> 
> profile.run('x.f1()')
> [andrea at precision test]$ cat simple.py
> import profile
> from os import path
> import sys
> 
> prof_path = path.join(path.dirname(__file__), 'profiling')
> sys.path.append(prof_path)
> import x
> 
> 
> profile.run('x.f1()')
> [andrea at precision test]$ python2 simple.py
>           2005 function calls in 0.057 seconds
> 
>     Ordered by: standard name
> 
>     ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>       1001    0.007    0.000    0.007    0.000 :0(range)
>          1    0.007    0.007    0.007    0.007 :0(setprofile)
>          1    0.000    0.000    0.050    0.050 <string>:1(<module>)
>       1000    0.037    0.000    0.043    0.000 b.py:1(f2)
>          0    0.000             0.000          profile:0(profiler)
>          1    0.000    0.000    0.057    0.057 profile:0(x.f1())
>          1    0.007    0.007    0.050    0.050 x.py:4(f1)
> 
> 
> 
> But in general you're perfectly right, I forgot about this profile
> "issue", calling
> profile.runctx('x.f1()', locals=locals(), globals=globals())
> works, even if maybe I don't even need to pass so much, but the x location
> would be enough..
> 
> Why does the simple example works then?

Because x is in __main__'s global namespace. And that is used as the 
default:

    def run(self, cmd):
        import __main__
        dict = __main__.__dict__
        return self.runctx(cmd, dict, dict)





More information about the Python-list mailing list