Building executables for Python 3 and PyQt

杀马特。学长 韩版系。学妹 提交于 2019-11-30 04:44:57

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

Noam Manos

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:\n %s" % cmd)
            exitCode = subprocess.call(cmd)
        sys.exit(exitCode)
    else:
        print ("This will be executed only within the new generated EXE File...")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!