[Tutor] Re: [Pythonmac-SIG] Re: Weird import problem with PythonIDE on Mac (was 'import problem')

Jack Jansen Jack.Jansen at cwi.nl
Fri Apr 22 10:31:11 CEST 2005


On 22 Apr 2005, at 03:06, Chris Smith wrote:

> ###
> def y1():
>     pass
> def foo():
>     from __main__ import y1
>     pass
> foo()
> ###
>
> Here is a version of the code, stripped of the timeit code.  The above 
> segment exhibits the same symptoms as the previously submitted one.
>
> Even though I am running this as "__main__" it behaves as though it is 
> not __main__. i.e. if I run this with pyOxide without the 'run as 
> __main__' option, it generates the same ImportError ('Can't import 
> y1') as it does when run in the PythonIDE.
>
> In the pythonIDE it generates the error whether the 'run as __main__' 
> option is on or off. As a test of that option, I verified that the 
> following code only runs when the __main__ option is on and it worked 
> as expected:
>
> ###
> if __name__=='__main__':
> 	print 'running as main'
> ###

As always, reading the source provides the answer.

If you look in PyEdit.py, method Editor.execstring(), you'll see that 
the only thing "run as __main__" does is set the module name to 
"__main__". It does *not* change the globals dictionary to __main__.

I'm not sure about the reasoning behind this, I think Just wanted to 
make sure that if you had two edit windows open both with "run as 
__main__" selected they didn't influence each other. On the other hand 
I can imageine that if you do that, open two windows in __main__ mode, 
the behaviour you want is exactly that.

Just?
--
Jack Jansen, <Jack.Jansen at cwi.nl>, http://www.cwi.nl/~jack
If I can't dance I don't want to be part of your revolution -- Emma 
Goldman



More information about the Tutor mailing list