📜 ⬆️ ⬇️

Making a standalone exe on IronPython

Sometimes you need to write a small program that will be distributed as an executable file, and you don’t want to have a sea of ​​files with the program. One exe-Schnick and everything, but at the same time I want to write it does not take a lot of time (some easy language).
CPython bundled with py2exe or cx_Freeze does not give the desired result: a lot of files and a large program size, although it works very quickly. A good solution was able to get in IronPython using the built-in pyc compiler. Even IDE is not required. Details under the cut.

It turned out just


Suppose you already have IronPython installed (note: the trick works in version 2.7.2 and higher, thanks to Juralis ) . And you have the following source code:
import clr clr.AddReference("System.Windows.Forms") from System.Windows.Forms import Application, Form class HabrForm(Form): def __init__(self): self.Text = 'Habr!' self.Name = 'Habr!' form = HabrForm() Application.Run(form) 

Save it to the mini.py file. Go to the program folder from the console and type the following command:
 ipy.exe C:\IronPython\Tools\Scripts\pyc.py /target:winexe /main:mini.py /standalone 
If you set target in exe, get a console program, which is convenient for debugging. You can also make a dll.

The compiler will give something like this:
 Input Files: Output: mini Target: WindowApplication Platform: ILOnly Machine: I386 Threading: STA Compiling... Generating stand alone executable Embedding Microsoft.Dynamic 1.1.0.20 Embedding Microsoft.Scripting 1.1.0.20 Embedding IronPython 2.7.0.40 Embedding IronPython.Modules 2.7.0.40 Embedding IronPython.SQLite 2.7.0.40 Embedding IronPython.Wpf 2.7.0.40 Saved to mini 

Exe-Schnick ready! Just keep in mind that there is no standard library in this build. And this is good. You can build a program using only the clr module on fast .NET libraries.

Connect Standard Library


If you need a standard library, then there are 2 ways to connect. The first is to compile the entire lib folder into one dll and make an AddReference. There is a ready-made solution , but since the program is too slow!
')
I went the other way. Given that IronPython has a built-in sys module, and the module loader can read modules from zip, the solution became obvious: pack the contents of the Lib folder (from the IronPython folder) and place it next to the exe file, while adding the following code:
 import sys sys.path.append(r'.\Lib.zip') 

Now you can import other modules, like json or urllib.
That's it, see how great it looks:

Source: https://habr.com/ru/post/149621/


All Articles