67

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.

warship
  • 2,924
  • 6
  • 39
  • 65
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • I'd say http://stackoverflow.com/questions/2709925/how-to-make-an-executable-file-in-python answers your question for windows. – Martijn Pieters May 04 '11 at 19:34
  • For Linux and Mac OS X, there is this question here on SO: http://stackoverflow.com/questions/4322250/python-executable – Martijn Pieters May 04 '11 at 19:36
  • Did you get it to work? I have a similar question here: http://stackoverflow.com/questions/14051403/how-to-save-a-form-from-qt-designer-as-a-standalone-app – Duh Compewtuhr Dec 27 '12 at 09:16
  • @DuhCompewtuhr Unfortunately I have not made additional attempts or projects, and still use PyGtk whenever I need some simple GUI stuff. – heltonbiker Dec 31 '12 at 18:00
  • Possible duplicate of [How to make an executable file in Python?](https://stackoverflow.com/questions/2709925/how-to-make-an-executable-file-in-python) – Jean-François Corbett Nov 07 '17 at 11:47

5 Answers5

69

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
Fingerbit
  • 38
  • 1
  • 5
Yuda Prawira
  • 12,075
  • 10
  • 46
  • 54
  • 8
    Hey..., Good news! It is now support python 3.3 – 3.5. – Ari Dec 11 '15 at 18:11
  • 1
    I see that pyinstaller now supports Python 3.6 as well: Multi-version: supports Python 2.7 and Python 3.3—3.6. –  Oct 14 '17 at 06:17
  • @YudaPrawira: Is it possible to run the executable made by pyinstaller to run on machine, which doesn't have pyqt installed? I am trying to deploy a pyqt5 application and is facing issue... – A.J Dec 29 '17 at 10:41
  • @Abin Please check here https://pythonhosted.org/PyInstaller/when-things-go-wrong.html#listing-hidden-imports I thought you can use --hidden-import command option. worth to try. – Yuda Prawira Jan 03 '18 at 06:39
24

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.

Michael Herrmann
  • 4,832
  • 3
  • 38
  • 53
4

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.

Don O'Donnell
  • 4,538
  • 3
  • 26
  • 27
  • O'Donnel: "the moneyguru.exe file (...) worked fine on Python 3.2". Don, I didn't understand this. You ran a .exe file on Python? You used cx_freeze to compile the py sources to get .exe file? Or someting else? Thanks! – heltonbiker May 05 '11 at 14:25
  • @heltonbiker: Sorry about the confusion. The download file is `moneyGuru_win_2_3_7.exe`. When you execute that it creates a directory in `C:\Program Files` called `Hardcoded Software`, this contains (among others) the file `moneyGuru.exe` which starts the Python app. – Don O'Donnell May 06 '11 at 04:29
  • @heltonbiker: I haven't tried to install the Linus or Mac versions but I expect they operate in a similar manner. – Don O'Donnell May 06 '11 at 05:10
  • 1
    All the links in this answer appear to no longer work reducing its effectiveness as an answer to the question. – moken Jul 14 '23 at 11:04
0

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)

Quazer
  • 353
  • 1
  • 8
-4

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.
Cameron Lee
  • 973
  • 9
  • 11