Two questions

John Machin sjmachin at lexicon.net
Thu Jun 2 16:26:10 EDT 2005


Peter Hansen wrote:
> qscomputing at gmail.com wrote:
> 
>> Thanks to you all for the quick response.
>>
>> I've noticed that when I do
>> $ python myprog.py
>> the file myprog.pyc file is not created, but the .pyc files for files I
>> import *are* created. Is this intentional and, if so, how do I get the
>> myprog.pyc file?
> 
> 
> I thought the docs covered this, so I left it out.  The
> "main" .py file is not converted to a .pyc file for reasons I can't 
> remember (and don't care... after it, that's just the way it is).  If 
> you really need a .pyc for it, the simplest thing to do is "import 
> myprog" from the interactive prompt.

*Provided* the "main" .py file has been set up properly, in the sense 
that the scripty bits are guarded by "if __name__ == '__main__':"

Otherwise side effects (possibly horrid) may occur upon import.

Another way around this is for myprog.py to be a stub which merely does 
something like this:

if __name__ == "__main__":
     import myrealprog
     myrealprog.main()



>  The compileall module I mentioned 
> would also be able to do this.
> 
> -Peter



More information about the Python-list mailing list