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

3

6

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?

Polestar answered 18/11, 2011 at 14:48 Comment(0)
M
3

Pyc.py is the way to go as Python Tools for Visual Studio does not support direct compilation. SharpDevelop has custom build actions for Ironpython. I used #Develop until PTVS came out.

See this question IronPython: EXE compiled using pyc.py cannot import module "os" for some detailed steps to follow to make your EXE. Remember to compile the standard lib modules you are using. If you have to make your users download and install IronPython then you wouldn't need to compile your script.

Mesquite answered 20/11, 2011 at 3:44 Comment(0)
G
6

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.

Glyptics answered 19/11, 2011 at 11:58 Comment(0)
M
6

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"
Macronucleus answered 30/8, 2018 at 3:26 Comment(0)
M
3

Pyc.py is the way to go as Python Tools for Visual Studio does not support direct compilation. SharpDevelop has custom build actions for Ironpython. I used #Develop until PTVS came out.

See this question IronPython: EXE compiled using pyc.py cannot import module "os" for some detailed steps to follow to make your EXE. Remember to compile the standard lib modules you are using. If you have to make your users download and install IronPython then you wouldn't need to compile your script.

Mesquite answered 20/11, 2011 at 3:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.