A problem about ipython

Robert Kern robert.kern at gmail.com
Fri Apr 1 11:16:38 EDT 2011


On 3/31/11 8:48 PM, Vincent Ren wrote:
> Hey, everyone, I'm trying to use ipython recently. It's very nice,
> however, when I run this(from Programming Python 3rd) in ipython, I'll
> get a NameError:
>
>
> In [1]: import settime, timer, set
>
> In [2]: import profile
>
> In [3]: profile.run('timer.test(100, settime.setops, set.Set)')
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call
> last)
>
> /home/vincent/hacking/python/<ipython console>  in<module>()
>
> /usr/lib/python2.6/profile.pyc in run(statement, filename, sort)
>       68     prof = Profile()
>       69     try:
> --->  70         prof = prof.run(statement)
>       71     except SystemExit:
>       72         pass
>
> /usr/lib/python2.6/profile.pyc in run(self, cmd)
>      454         import __main__
>      455         dict = __main__.__dict__
> -->  456         return self.runctx(cmd, dict, dict)
>      457
>      458     def runctx(self, cmd, globals, locals):
>
> /usr/lib/python2.6/profile.pyc in runctx(self, cmd, globals, locals)
>      460         sys.setprofile(self.dispatcher)
>      461         try:
> -->  462             exec cmd in globals, locals
>      463         finally:
>      464             sys.setprofile(None)
>
> /usr/lib/pymodules/python2.6/IPython/FakeModule.pyc in<module>()
>
> NameError: name 'timer' is not defined

In order to support pickling and its %run feature, IPython makes a fake __main__ 
module. It looks like profile.run() explicitly imports __main__ to try to run 
the statement there. Honestly, it's been a thorn in our side for a long time, 
but it's a confusing bit of the code. Most interactive shells actually written 
in Python are going to have a similar need to do a workaround, since they 
already have a __main__. The regular shell is not written in Python, so it has 
no problem.

You will want to ask on the IPython list for future IPython questions.

   http://mail.scipy.org/mailman/listinfo/ipython-user

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list