How to cross-compile a Python script into a Windows executable on Linux
Using Wine and Pyinstaller.
Pyinstaller is a program that packages Python programs into stand-alone executables, under the most used OSs (Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX).
Initially Pyinstaller had a beta feature that allows the cross-compilation of a windows executable under Linux.
However, due the instability of the feature, it has been removed since version 1.5.
Fortunately, is still possible to package a Windows executable using PyInstaller under Wine.
1. Install wine and Python
$ sudo apt-get install wine $ wget https://www.python.org/ftp/python/2.7.9/python-2.7.9.amd64.msi $ wine msiexec /i python-2.7.9.amd64.msi /qb
If you need to compile a 32bit executable, you need to install wine32:
sudo dpkg --add-architecture i386 && sudo apt-get update && sudo apt-get install wine32
2. Install PyInstaller on wine
$ cd ~/.wine/drive_c/Python27 $ wine python.exe Scripts/pip.exe install pyinstaller
3. Package a python scripts
Package Python scripts (e.g., helloworld.py) with pyinstaller.
$ cat helloworld.py #!/usr/bin/env python print('Hello World!') $ wine ~/.wine/drive_c/Python27/Scripts/pyinstaller.exe --onefile helloworld.py
The Windows executable file is located in dist/
.
$ wine dist/helloworld.exe Hello World!
Finally, transfer the executable on a windows box and test it: