简体   繁体   English

PyQT / Windows / Pyinstaller(exe)-NameError:未定义全局名称“ QtGui”

[英]PyQT / Windows / Pyinstaller (exe) - NameError: global name 'QtGui' is not defined

I have a problem trying to turn my python code into an executable using pyinstaller. 我在尝试使用pyinstaller将python代码转换为可执行文件时遇到问题。 I'm using PyQT 4.9.1 and Python 2.7. 我正在使用PyQT 4.9.1和Python 2.7。

I am getting the error when I try and build it (Build.py): 尝试构建(Build.py)时出现错误:

Traceback (most recent call last):
  File "Build.py", line 1494, in <module>
    main(args[0], configfilename=opts.configfile)
  File "Build.py", line 1472, in main
    build(specfile)
  File "Build.py", line 1429, in build
    execfile(spec)
  File "c:\projects\vibot\vibotUI_07.py", line 270, in <module>
    window = viUI()
  File "c:\projects\vibot\vibotUI_07.py", line 9, in __init__
    QtGui.QMainWindow.__init__(self)
NameError: global name 'QtGui' is not defined

I have searched google and all the solutions are based on correcting improper importing of modules, but I already did it properly to begin with. 我已经搜索过谷歌,所有解决方案都基于纠正模块的不正确导入,但是从一开始我就已经正确地做到了。

Here is a cropped version of my code: 这是我的代码的裁剪版本:

#!/usr/bin/env python

import sys
import os
from PyQt4 import QtCore, QtGui

class viUI(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('test')
        self.setObjectName('viMainWindow')
        self.resize(400, 600)

        self.show()

app = QtGui.QApplication(sys.argv)
window = viUI()
sys.exit(app.exec_())

This is the Makespec.py file: 这是Makespec.py文件:

# -*- mode: python -*-
a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'), os.path.join(HOMEPATH,'support\\useUnicode.py'), 'c:\\projects\\vibot\\vibotUI_07.py'],
             pathex=['c:\\Python\\pyinstaller-1.5.1\\pyinstaller-1.5.1'])
pyz = PYZ(a.pure)
exe = EXE( pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=os.path.join('dist', 'vibotUI_07.exe'),
          debug=False,
          strip=False,
          upx=True,
          console=True )

It looks to me like you're calling Build.py and passing your script as a paramter. 在我看来,您正在调用Build.py并将脚本作为参数传递。 I just tested this to see what would happen and got the same output that you posted. 我只是对此进行了测试,以查看会发生什么并获得与发布相同的输出。

With the current stable PyInstaller (1.5.1) you need to create a spec file first . 使用当前稳定的PyInstaller(1.5.1),您需要首先创建一个规范文件 Instead of Build.py run MakeSpec.py with your script as an argument. 代替Build.py,以脚本作为参数运行MakeSpec.py。 This will create a .spec file that you then send to Build.py. 这将创建一个.spec文件,然后将其发送到Build.py。

The documentation shows options you can pass to MakeSpec for things like setting an icon under Windows and setting deployment options. 该文档显示了可以传递给MakeSpec的选项,例如在Windows下设置图标和设置部署选项。 These options all go into the spec file so that you just need to call Build.py again when you need to rebuild your application. 这些选项都进入了规范文件,因此当您需要重新构建应用程序时,只需再次调用Build.py。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM