English 中文(简体)
Building executables for Python 3 and PyQt
原标题:

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed.

I almost exclusively care about Windows platforms, so my goal is to have a single executable file and maybe some resource files and .dlls in the end.

Having searched around, I came to the conclusion that

  • py2exe only supports Python up to version 2.7
  • pyinstaller only supports Python up to version 2.6
  • cx_Freeze does not work for me because I keep on getting the following error when trying to execute my successfully build binary:

Y:Userslulzuildexe.win32-3.1>system_shutdown.exe
Traceback (most recent call last):
File "Y:Program Files (x86)Pythonlibsite-packagescx_FreezeinitscriptsConsole3.py", line 27, in exec(code, m.__dict__)
File "Y:/Users/lulz/Documents/Coding/Python3/projects/System Shutdown/system_shutdown.pyw", line 5, in from PyQt4 import QtCore
File "ExtensionLoader_PyQt4_QtCore.py", line 16, in AttributeError: NoneType object has no attribute modules

So my problem is basically two problems here:

  1. Is there another way but cx_Freeze to build binaries with my configuration?
  2. If not, what might the cx_Freeze problem be?

I can provide more information on the second problem if necessary, like my call of cx_Freeze, my distutils setup script etc.

Thank you already for your help and comments.

最佳回答

You can fix this by appending one line of code to freeze.py in your cx_Freeze package.

It is described here: http://www.mail-archive.com/cx-freeze-users@lists.sourceforge.net/msg00212.html

It worked for me at least :)

Cheers, Almar

问题回答

For Python 3.3 and later, there s a good resolution here: py2exe - generate single executable file

Install py2exe:

pip install py2exe

Then add besides your_script.py file, the following Make_exe.py file:

from distutils.core import setup
import py2exe, sys

class Make_exe():
    def __init__(self, python_script):
        sys.argv.append( py2exe )

        setup(
            console=[{ script : python_script}],
            zipfile = None,
            options={
                 py2exe : 
                {
                     bundle_files : 1, 
                     compressed : True,
                    # Add includes if necessary, e.g. 
                     includes : [ lxml.etree ,  lxml._elementpath ,  gzip ],
                }
            }
        )

if __name__ ==  __main__ :
    Make_exe( your_script.py )

And if you want to make your_script.py rebuild itself as your_script.exe each time you run it in python, you can add to its main:

import subprocess
import sys

if __name__ ==  __main__ :
    currentFile = sys.argv[0]
    if currentFile.lower().endswith(".py"):
        exitCode = subprocess.call("python Make_exe.py")
        if exitCode==0 :
            dirName = os.path.dirname(currentFile)
            exeName = os.path.splitext(os.path.basename(currentFile))[0] +  .exe 
            exePath = dirName + "/dist/" + exeName
            cmd = [exePath] + sys.argv[1:]
            print ("Executing command:
 %s" % cmd)
            exitCode = subprocess.call(cmd)
        sys.exit(exitCode)
    else:
        print ("This will be executed only within the new generated EXE File...")




相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签