如何将Qt4 Python2.7应用转为Windows EXE?已试多工具未成功求帮助
Hey there! Let's work through this Qt4 app packaging problem together. Since Qt4 is a bit of a legacy framework now, most issues boil down to mismatched Python versions, missing Qt dependencies, or outdated tool configurations. Here's how to troubleshoot each packaging tool you tried:
1. Fixing PyInstaller Issues for Qt4
- First, confirm you're using a compatible Python version—Qt4 plays nicest with Python 2.7 or Python 3.4-3.6 (newer Python versions dropped official Qt4 support).
- Install an older, Qt4-friendly version of PyInstaller:
pip install pyinstaller==3.6 - Generate a spec file to explicitly include Qt4's required components:
pyi-makespec --onefile --windowed your_app.py - Open the generated
.specfile, and add thisdatasentry under theAnalysissection (replaceC:/PythonXXwith your actual Python installation path):datas = [ ('C:/PythonXX/Lib/site-packages/PyQt4/plugins/platforms/qwindows.dll', 'platforms'), ] - Build your app using the spec file instead of the direct command:
pyinstaller your_app.spec
2. Fixing py2exe Issues for Qt4
- py2exe is pretty outdated, so stick with Python 2.7 for the most reliable results with Qt4.
- Install a compatible py2exe version:
pip install py2exe==0.9.2.2 - Create a
setup.pyfile with the following content (replaceyour_app.pywith your actual script name):from distutils.core import setup import py2exe import sys from PyQt4 import QtCore sys.setrecursionlimit(5000) setup( windows=[{'script': 'your_app.py'}], options={ 'py2exe': { 'includes': ['sip', 'PyQt4.QtCore', 'PyQt4.QtGui'], 'dll_excludes': ['MSVCP90.dll'], 'bundle_files': 1, 'compressed': True } } ) - Run the build command:
python setup.py py2exe - After building, copy the
platformsfolder from your PyQt4 installation (usually inLib/site-packages/PyQt4/plugins) into thedistfolder generated by py2exe—this fixes most "missing platform plugin" errors.
3. Fixing cx_Freeze Issues for Qt4
- Again, use Python 2.7 or 3.4-3.6 and install an older cx_Freeze version that supports Qt4:
pip install cx-Freeze==5.1.1 - Create a
setup.pyfile like this:import sys from cx_Freeze import setup, Executable from PyQt4 import QtCore, QtGui base = None if sys.platform == 'win32': base = 'Win32GUI' # Hides the console window for GUI apps executables = [Executable('your_app.py', base=base)] setup( name='YourQt4App', version='0.1', description='Your basic Qt4 application', options={ 'build_exe': { 'includes': ['sip', 'PyQt4.QtCore', 'PyQt4.QtGui'], 'include_files': [ (QtCore.QCoreApplication.applicationDirPath() + '/plugins/platforms/qwindows.dll', 'platforms/qwindows.dll') ] } }, executables=executables ) - Build your app with:
python setup.py build
General Troubleshooting Tips
- Always test your app runs correctly in your Python environment first—if it crashes there, the problem isn't with the packaging tool.
- Check the error logs each tool generates (PyInstaller logs in the
buildfolder, py2exe/cx_Freeze log to the console) for specific missing modules or DLLs—this will tell you exactly what's missing. - If you get a "could not find or load the Qt platform plugin windows" error, manually copy the entire
platformsfolder from your PyQt4 installation into the same directory as your final EXE.
内容的提问来源于stack exchange,提问作者Sumit Sharma
相关产品推荐
相关产品推荐

