Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packaging a Python script on Linux into a Windows executable

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.

like image 551
Chinmay Kanchi Avatar asked Jun 01 '10 15:06

Chinmay Kanchi


People also ask

Can you convert Python script to exe?

Using pyinstaller you can convert the python file to exe. Type pyinstaller <python-file-name>, this will convert the . py to .exe file with console. Add –no-console keyword after pyinstaller to convert python file to executable without command prompt.

How do I convert .py to .exe in Linux?

py file to .exe. Another way to convert . py files to .exe is by using cx_freeze. Cx_freeze is used to create executable files from python scripts and it is cross-platform.


1 Answers

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.

like image 199
SparkAndShine Avatar answered Sep 20 '22 03:09

SparkAndShine