Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile an IronPython app into an EXE without Visual Studios (and possibly with py2exe)?

So I tried searching for this answer before coming straight here to ask it. It definitely seems doable to turn an IronPython app/set of scripts into an EXE. The one issue is that they all seem to talk about using IronPython with IronPython Studios, a Python plugin for Visual Studios. Last I checked, I thought that IronPython Studios was basically replaced with the Python Tools for Visual Studios (or PTVS) plugin tool. The problem with this being that due to the way my code is structured, it actually managed to crash the plugin, and therefore Visual Studios, upon loading my code into the IDE. It will do so until the next scheduled update coming out at the end of the month or early next month. So that is not going to be an option as I've been having to develop my project in Eclipse instead with the PyDev plugin.

I have used py2exe to convert some basic Python applications into executables before, but that was when I was using the CPython interpreter. I don't know how I would set up py2exe to work with IronPython. Would it be possible to use py2exe to make an IronPython executable? And if my program uses extra C# assemblies to do its work, how would I set those up too so py2exe can use them and my executable can see them for referencing?

like image 492
grg-n-sox Avatar asked Nov 18 '11 14:11

grg-n-sox


People also ask

Does IronPython compile?

IronPython is a Python compiler. It compiles Python code to in memory bytecode before execution (which can be saved to disk, making binary only distributions possible).


2 Answers

IronPython ships with a command line compiler pyc.py (in the Tools\Scripts folder) which you could use. Another alternative is to use SharpDevelop which will compile your IronPython code to an executable.

Your compiled IronPython application can use C# assemblies if they are in the GAC or if they are deployed alongside your executable in the same folder.

like image 175
Matt Ward Avatar answered Sep 20 '22 22:09

Matt Ward


I use IronPyton 2.7.8 and ipyc.exe rather than Tools\Scripts\pyc.py, and it introduces some differences, so i am leaving some information here.

1. use /embed and /standalone option

If you split your script into more than one py files, then use /embed option with /main option, they will generate a single exe file including all py script files. For example you have "MyMain.py" script and "OtherFuncs.py" then as follows.

C:\Program Files\IronPython 2.7\ipyc.exe /main:MyMain.py OtherFuncs.py /embed /platform:x86 /standalone /target:winexe

/standalone option is used for including basic dll files for IronPython under the C:\Program Files\IronPython 2.7\ folder like IronPython.dll, IronPython.Modules.dll, Microsoft.Dynamic.dll, and Microsoft.Scripting.dll

But be careful that DLLs/IronPyton.SQLite.dll and DLLs/IronPtyhon.WPF.dll are not included, so if you use WPF or SQLite functionality in your script, you should copy and distribute these dll files additionally with your exe file, and you should add the following line in your script

clr.AddReference("IronPython.Wpf")
clr.AddReference("IronPython.SQLite")

2. libraries script files under the C:\Program Files\IronPython 2.7\Lib folder

your script use many standard functions supported by the library script files under the C:\Program Files\IronPython 2.7\Lib folder, so you should distribute these script files with your exe.

there are two options, one is just copying the Lib folder to the folder having your exe file, and another is making a StdLib.dll having all standard library script files as the following command.

C:\Program Files\IronPython 2.7\ipyc.exe /main:StdLib.py io.py sys.py ...all necessary script files... /embed /platform:x86 /target:dll

StdLib.py is just empty file with 0 size, it is a hack for generating a single dll file. and you should add the following lines at the very first line in your script.

import clr
clr.AddReference("StdLib")

I use a python script making StdLib.dll something like the following.

#...
#Build StdLib.DLL
ipath = 'C:\Program Files\IronPython 2.7'
ipyc = ipath + '\ipyc.exe'
# any library files you need
gb += glob.glob(r".\Lib\*.py")
gb += glob.glob(r".\Lib\encodings\*.py")
# ...
gb = [ipyc,"/main:StdLib.py","/embed","/platform:x86","/target:dll"] + gb
subprocess.call(gb)
print "Made StdLib"
like image 35
Jun Avatar answered Sep 18 '22 22:09

Jun