67

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114

6 Answers6

43

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.

SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
  • 2
    You will have to install all the dependencies in wine too (though you might have it in Ubuntu). __Make sure that your application run using command__ `wine python appli.py` – Jithin Pavithran Oct 14 '17 at 10:43
  • Useless now since there is no way to install python 3 on wine – Jacob Sánchez Sep 04 '20 at 00:17
  • There is also a way to install python in wine if you have no .msi file https://askubuntu.com/questions/678277/how-to-install-python3-in-wine – Eugene Mar 09 '22 at 13:40
21

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

Praveen
  • 1,791
  • 3
  • 20
  • 33
luc
  • 41,928
  • 25
  • 127
  • 172
  • Yes, that works: http://groups.google.com/group/pyinstaller/browse_thread/thread/2884993f9b070968 – stephan Jun 01 '10 at 15:45
  • Aargh! This _should_ work, but the bloody thing only supports python 2.6 on *NIXes and my script requires python 2.6 to work... Looks like more effort than it's worth... I guess I just need to plan my build cycles so I only reboot into Windows a minimum number of times... – Chinmay Kanchi Jun 01 '10 at 15:58
  • 1
    @Chinmay: there was a pywin26 branch of pyinstaller that has been merged into the trunk. So, if you are the daring kind of person, you can check out the trunk using `svn co http://svn.pyinstaller.org/trunk pyinstaller-trunk` and it *should* work with 2.6 on Windows. – stephan Jun 02 '10 at 05:31
  • 1
    I will give this a try. The immediate problem has been solved by rebooting into Windows, but this _is_ likely to crop up fairly often now that I've moved almost completely over to Linux. Thanks for the heads-up! – Chinmay Kanchi Jun 02 '10 at 23:57
  • 6
    this feature is being taken out: https://groups.google.com/forum/?fromgroups#!searchin/PyInstaller/linux$20windows/pyinstaller/veq3BlA_Bns – hoju Feb 23 '12 at 07:00
  • SO messes up the previous link. Anyway in that thread the author says "I'm the one who implemented this feature some time ago. And I'm going to remove it in the near future, since it does one work half." – hoju Feb 23 '12 at 07:02
  • 12
    For future visitors, here's the working link: https://groups.google.com/forum/?fromgroups#!topic/pyinstaller/veq3BlA_Bns – TheLQ Jul 19 '13 at 18:28
  • 1
    It seems some users were able to successfully compile a Windows exe on Linux using pyInstaller under Wine. You might give it a try. – gaborous Aug 08 '13 at 00:34
  • 4
    As is mentioned by @hoju, this feature is removed from PyInstaller since 1.5. See http://www.pyinstaller.org/wiki/FAQ#Features: "In version 1.4 we had build in some support for this, but it showed to work only half. It would require some Windows system on another partition and would only work for pure Python programs. As soon as you want a decent GUI (gtk, qt, wx), you would need to install Windows libraries anyhow. So it's much easier to just use Wine." – azalea Aug 25 '14 at 20:39
  • @ChinmayKanchi I make it, please check my answer. – SparkAndShine Feb 24 '16 at 14:55
  • PyInstaller doesn't support cross-compilation. Wine is for that. – Robert Moore Jun 05 '17 at 20:53
  • 1
    It has been said in FAQ "No, this is not supported. Please use Wine for this, PyInstaller runs fine in Wine" – user6039980 Oct 27 '18 at 21:51
  • Answer invalid in 2020 – Jacob Sánchez Sep 04 '20 at 00:18
3

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.

Adam Crossland
  • 14,198
  • 3
  • 44
  • 54
3

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

kaptaan
  • 31
  • 2
  • This is a very helpful answer to me because you have shown me how to find the wines pyinstaller.exe location. If anyone is still facing any issues while using this output location then please check if there is any file that contains spaces. Just enclose them with quotes and it will work nicely. – Shahriar Rahman Zahin May 10 '22 at 05:56
3

I wrote a blog post on how to do this with PyInstaller. Here's the summary:

  • How to create EXEs for Python on Linux, using PyInstaller and WINE
    • Download Python 3.8 Windows installer
    • wine python-3.8.9.exe, then see instructions below
    • wine C:/Python38/python.exe -m pip install --upgrade pip
    • wine C:/Python38/python.exe -m pip install -r requirements.txt, and requirements.txt should include PyInstaller itself
    • wine C:/Python38/Scripts/pyinstaller.exe ...
    • All done!

Installing Python 3.8 in Wine:

  1. Check "Add Python 3.8 to PATH"
  2. Click "Customize installation
  3. Click "Next"
  4. Click "Install for all users"
  5. Set the install location as C:\\Python38
  6. Click "Install"
  7. Close the window.

Feel free to read the post to get more details.

makeworld
  • 1,266
  • 12
  • 17
2

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.

Perkins
  • 2,409
  • 25
  • 23