IronPython – how to compile exe

IronPython 2.6 ships with pyc.py (look in %IRONPYTONINSTALLDIR%\Tools\Scripts\pyc.py), script that can be used to compile python script(s) into binary (exe). For the purpose of this mini how-to I created a directory D:\IronTestCompile with the following contents:

D:\IronTestCompile>dir /b /oe
Microsoft.Scripting.ExtensionAttribute.dll
Microsoft.Scripting.dll
Microsoft.Scripting.Core.dll
IronPython.dll
IronPython.Modules.dll
Microsoft.Dynamic.dll
hello2.py
hello1.py
pyc.py
HelloWorld.py

From IronPython directory I copied all the necessary DLL’s (Microsoft*.dll, IronPython*.dll) that are needed for compilation and as run-time prerequisite to run compiled HelloWorld.exe. HelloWorld.py is my main script that imports two additional modules, hello1.py and hello2.py and pyc.py is compilation script shipped with IronPython 2.6.

Here is the contents of HelloWorld.py, hello1.py and hello2.py:

#
# HelloWorld.py
# IronPython - Compile test
#
import hello1
import hello2

print "HelloWorld from HelloWorld.py"
hello1.Hello()
hello2.Hello()
# End

# hello1.py
def Hello():
    print "Hello World from Hello1.py"

# hello2.py
def Hello():
    print "Hello World from Hello2.py"

Now let’s see what we can do with pyc.py script:

D:\IronTestCompile>ipy pyc.py

pyc: The Command-Line Python Compiler

Usage: ipy.exe pyc.py [options] file [file ...]

Options:
    /out:output_file                          Output file name (default is main_file.<extenstion>)
    /target:dll                               Compile only into dll.  Default
    /target:exe                               Generate console executable stub for startup in addition to dll.
    /target:winexe                            Generate windows executable stub for startup in addition to dll.
    /? /h                                     This message

EXE/WinEXE specific options:
    /main:main_file.py                        Main file of the project (module to be executed first)
    /platform:x86                             Compile for x86 only
    /platform:x64                             Compile for x64 only


Example:
    ipy.exe pyc.py /main:Program.py Form.py /target:winexe

And here is my command line that I used to compile console application HelloWorld.exe:

D:\IronTestCompile>ipy pyc.py hello1.py hello2.py /out:HelloWorld /main:HelloWorld.py /target:exe
Input Files:
        hello1.py
        hello2.py
Output:
        HelloWorld
Target:
        ConsoleApplication
Platform:
        ILOnly
Machine:
        I386
Compiling...
Saved to HelloWorld

End result of compilation are two files, HelloWorld.exe and HelloWorld.dll. Both files (plus above listes DLL’s) are needed for application to run. And here is sample output:

D:\IronTestCompile>HelloWorld
HelloWorld from HelloWorld.py
Hello World from Hello1.py
Hello World from Hello2.py
Advertisements

Posted on 21.12.2009, in Scripting and tagged , . Bookmark the permalink. Comments Off on IronPython – how to compile exe.

Comments are closed.