61
votes

I have a Python script that I'd like to compile into a Windows executable. Now, py2exe works fine from Windows, but I'd like to be able to run this from Linux. I do have Windows on my development machine, but Linux is my primary dev platform and I'm getting kind of sick of rebooting into Windows just to create the .exe. Nor do I want to have to buy a second Windows license to run in a virtual machine such as VirtualBox. Any ideas?

PS: I am aware that py2exe doesn't exactly compile the python file as much as package your script with the Python interpreter. But either way, the result is that you don't need Python installed to run the script.

5
When I need something Windows-only (e.g. IE or testing NSIS install packages), I use a virtual machine with Windows; Virtualbox works great and no rebooting is necessary. Of course, this is just side-stepping the issue. - Piskvor left the building
Ah, but technically you require two licences of Windows for this... - Chinmay Kanchi
Do I? How so? The host OS is not Windows in my case, only the guest is. - Piskvor left the building
@Piskvor In order to not need two licenses, one for bare metal and one for VirtualBox, the asker would have to first uninstall Windows from bare metal and install it in VirtualBox. I'm not sure whether an OEM license even allows that. - Damian Yerrick
Where did an OEM license requirement come from? I see no mention in the question; do not assume too much. - Piskvor left the building

5 Answers

21
votes

Did you look at PyInstaller?

It seems that versions through 1.4 support cross-compilation (support was removed in 1.5+). See this answer for how to do it with PyInstaller 1.5+ under Wine.

Documentation says:

Add support for cross-compilation: PyInstaller is now able to build Windows executables when running under Linux. See documentation for more details.

I didn't try it myself.

I hope it helps

35
votes

As mentioned by other answerers, the cross-compilation feature is removed from PyInstaller since 1.5. Here, show how to package a Windows executable from Python scripts using PyInstaller under wine.

Step 1: Install wine and Python

sudo apt-get install wine
wine msiexec /i python-2.7.10.msi /L*v log.txt

PS:

  • Newer Python versions already include pip (is used to install pyinstaller). Download Python installation package from here (e.g., python-2.7.10.msi)

  • For macos users, use brew cask install xquartz wine-stable.

Step 2: Install PyInstaller on wine

$ cd ~/.wine/drive_c/Python27
$ wine python.exe Scripts/pip.exe install pyinstaller

Successfully installed pyinstaller-3.1.1 pypiwin32-219

Step 3: Package Python scripts

Package Python scripts (e.g., HelloWorld.py) with pyinstaller.

$ wine ~/.wine/drive_c/Python27/Scripts/pyinstaller.exe --onefile HelloWorld.py

# filename: HelloWorld.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print('Hello World!')

The Windows executable file is located in dist/.

$ wine dist/HelloWorld.exe 
Hello World!
fixme:msvcrt:__clean_type_info_names_internal (0x1e24e5b8) stub

Refer to here for the detailed description.

3
votes

You could run Windows in VirtualBox in order to run py2exe. VBox offers a powerful command-line client for automating tasks, so it something that you could likely integrate into your development process with ease.

2
votes

I have tested py2exe inside of wine, and it does function. You'll need to install python in wine for it to work, or if you only use the standard libarary, you can bundle py2exe with py2exe from the windows machine and then use it in wine. Just keep in mind you need the same version of the ms visual C libraries in wine as were used to compile python or things won't work properly.

0
votes

Tested on Platform: Kubuntu 20.04, wine 6.0, python38

Download wine and python

  1. Download windows version of python from https://www.python.org/downloads/release/python-3810/

  2. Install wine sudo apt install wine

  3. Open your terminal and run wine the-python-exe-you-downloaded

  4. Run find ~/.wine -name pip.exe this will give you the pip path:

/home/yourusername/.wine/drive_c/users/yourusername/Local Settings/Application Data/Programs/Python/Python38/Scripts/pip.exe

Install pyinstaller

Run wine /home/yourusername/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pip.exe install pyinstaller

Package your file

Find installation path

find ~/.wine -name pyinstaller.exe

wine /home/yourusernmae/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pyinstaller.exe --onefile yourpythonfile