How do I compile a PyQt script (.py) to a single standalone executable file for windows (.exe) and/or linux?

PythonQt4CompilationExecutablePyqt4

Python Problem Overview


I started to fiddle with PyQt, and made a "beautiful" script from the pyqt whitepaper example app (pastebin)

It works perfectly in Windows and Linux (with qt environment already installed on both).

Now my question is: Since I am trying to use Qt because it is compiled (at least pure old C++ based Qt), how can I compile some .exe file to run it on Windows, or a standalone executable for Linux.

The point is that I want the program to be compiled, because of speed and portability, instead of interpreted from source, which would require a previous setup on any machine. One of the goals, for example, is sending small gui scripts via email to coworkers who are not programmers at all.

Python Solutions


Solution 1 - Python

if you want completelly create one stand alone executable, you can try PyInstaller . i feel it's better to create one stand alone executable than cx_freeze or py2exe (in my experience). and easy to use (full documentation available in the site).

It supports Python 3.6 or newer.

Pass the --onefile argument if you want to create completely standalone .exe. in example :

pyinstaller.exe --onefile --windowed app.py

Solution 2 - Python

After spending many weeks on this and trying all the alternatives - PyInstaller, py2exe, cx_freeze,... - I created my own library: https://build-system.fman.io/. It is based on PyInstaller but solves many of its common pain points. It also lets you create native installers on Windows, Mac and Linux.

Solution 3 - Python

You may want to check out cx_freeze. It claims to create executables which are "cross platform and should work on any platform that Python itself works on."

I came across it in exploring the moneyGuru package which uses PyQt. I downloaded the moneyguru.exe file to my Windows XP system, executed it, and it worked fine on Python 3.2.

You can clone the hg repo from here to see how it.s done.

Solution 4 - Python

There is a module named Py2EXE, which will do exactly what you want to do. It will convert the script into a .exe file to run on windows. I'm not sure about linux, but I bet there is a module out there somewhere. py2exe.com

Solution 5 - Python

I am using pyinstaller

pip install pyinstaller

I don't know, but pyinstaller does't append sip.pyd. So, your need a PyQt5\sip.pyd. I recommend nice windows style qwindowvistastyle.dll.

Make build.cmd file as:

pyinstaller --onefile --clean ^
    --add-binary="C:\Users\Quazer\.virtualenv\pyqt5-36\Lib\site-packages\PyQt5\sip.pyd;PyQt5" ^
    --add-binary="C:\Users\Quazer\.virtualenv\pyqt5-36\Lib\site-packages\PyQt5\Qt\plugins\styles\qwindowsvistastyle.dll;PyQt5\Qt\plugins\styles" ^
    .\main.py

^ - new line in command file (.cmd, .bat)

Solution 6 - Python

> Since I am trying to use Qt because it is compiled

You're defeating this benefit by using Python. Although the other answers give an introduction to the options for distributing Python code without requiring users to install Python themselves, Python is intended to be an interpreted language so there will be downsides to each of these options (ex. speed, program size, compatibility, etc...). They may or may not be deal-breakers to you.

Your two other options are:

  1. Embrace the interpreted nature of Python: have people you're sharing your program with install Python and the dependencies. You can simplify this process significantly though. Ex. on Linux, use a package manager.
  2. Write your program in C++. Doing so would allow you to truly compile a single, native executable. This unfortunately means dropping Python, but there's reasons people still write code in less beautiful languages like C++ and it sounds like you might be running into some of them.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionheltonbikerView Question on Stackoverflow
Solution 1 - PythonYuda PrawiraView Answer on Stackoverflow
Solution 2 - PythonMichael HerrmannView Answer on Stackoverflow
Solution 3 - PythonDon O'DonnellView Answer on Stackoverflow
Solution 4 - PythonMatt HabelView Answer on Stackoverflow
Solution 5 - PythonQuazerView Answer on Stackoverflow
Solution 6 - PythonCameron LeeView Answer on Stackoverflow