Compiling main script into .pyc

Terry Reedy tjreedy at udel.edu
Thu Jan 16 23:45:41 EST 2014


On 1/16/2014 10:19 PM, MRAB wrote:
> On 2014-01-17 02:56, bob gailer wrote:
>> On 1/16/2014 8:01 PM, Sam wrote:
>>> One thing I observe about python byte-code compiling is that the main
>>> script does not gets compiled into .pyc. Only imported modules are
>>> compiled into .pyc.
>>>
>>> May I know how can I compile the main script into .pyc?
>> Duh? Just import it!
>>
> What if you want to just compile it? Importing will run it!

Write the main script as

def main(): ...
if __name__ == '__main__':  main()

The difference between merely compiling 'def main' and executing it is 
trivial.

Or don't bother compiling and write main.py as one line:
   from realmain import start; start()

-- 
Terry Jan Reedy




More information about the Python-list mailing list