国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

pyinstaller打包pyqt文件

2019-11-14 17:25:45
字體:
供稿:網(wǎng)友

打包pyqt文件

如何將pyqt生成exe的二進(jìn)制文件呢,pyinstaller就是這樣的工具

可以將腳本文件.py 文件轉(zhuǎn)換為編輯后的二進(jìn)制文件,在進(jìn)行發(fā)布

下面說下,如果打包

一. 安裝:

    下載地址:https://github.com/pyinstaller/pyinstaller

    最新版本pyinstaller2.1.1 (2015-01)

    

python setup.py install

  pyinstaller 是有依賴包的,安裝之前必須要安裝pywin32, 在網(wǎng)上找到對應(yīng)版本的pywin32.msi

二. 寫一個(gè)要打包的py文件

    

# -*- coding: utf-8 -*-import sysfrom PyQt4 import QtGui, QtCoreclass buttonRedrect(QtGui.QWidget):    def __init__(self):        super(buttonRedrect, self).__init__()        self.setWindowTitle('Mouse Event')        self.setWindowIcon(QtGui.QIcon('QQ.png'))        self.labels = QtGui.QLabel(u'點(diǎn)我試試!', self)        self.labels.setGeometry(50, 50, 150, 50)        self.labels.mouseReleaseEvent = self.events    def events(self, event):        ev=event.button()        if ev== QtCore.Qt.LeftButton:            OK = QtGui.QMessageBox.information(self, (u'提示'),(u'左鍵'),QtGui.QMessageBox.Yes , QtGui.QMessageBox.No)            if OK==QtGui.QMessageBox.Yes:                QtGui.QMessageBox.information(self, (u'提示'),(u'YES'),QtGui.QMessageBox.Yes)            else:                QtGui.QMessageBox.information(self, (u'提示'),(u'NO'),QtGui.QMessageBox.Yes)        elif ev == QtCore.Qt.RightButton:            OK = QtGui.QMessageBox.warning(self, (u'提示'),(u'右鍵'),QtGui.QMessageBox.Yes , QtGui.QMessageBox.No)        elif ev == QtCore.Qt.MiddleButton:            OK = QtGui.QMessageBox.question(self, (u'提示'),(u'滾動軸'),QtGui.QMessageBox.Yes , QtGui.QMessageBox.No)app=QtGui.Qapplication(sys.argv)buttonr=buttonRedrect()buttonr.show()sys.exit(app.exec_())

三. 文件打包

   在要打包的同級目錄下,新建一個(gè)pyinstaller.py, 內(nèi)容如下

   

#! /usr/bin/env python#-----------------------------------------------------------------------------# Copyright (c) 2013, PyInstaller Development Team.## Distributed under the terms of the GNU General Public License with exception# for distributing bootloader.## The full license is in the file COPYING.txt, distributed with this software.#-----------------------------------------------------------------------------"""Main command-line interface to PyInstaller."""# from  PyInstaller import  *import  osif __name__ == '__main__':    from PyInstaller.main import run    opts=['abc.py','-F','-w','--icon=favicon.ico']    run(opts)

 其中opts是參數(shù),將參數(shù)修改成你要的需求

-- 參數(shù)說明:

    

-F, --onefile Py代碼只有一個(gè)文件

-D, --onedir Py代碼放在一個(gè)目錄中(默認(rèn)是這個(gè))

-K, --tk 包含TCL/TK

-d, --debug 生成debug模式的exe文件

-w, --windowed, --noconsole 窗體exe文件(Windows Only)

-c, --nowindowed, --console 控制臺exe文件(Windows Only)

-o DIR, --out=DIR 設(shè)置spec文件輸出的目錄,默認(rèn)在PyInstaller同目錄

--icon=<FILE.ICO> 加入圖標(biāo)(Windows Only)

-v FILE, --version=FILE 加入版本信息文件

--upx-dir, 壓縮可執(zhí)行程序

 

將命令行CMD切換切換到當(dāng)前要打包的目錄

   F:/PRoject/pyqt/TEST>

輸入命令:

    python pyinstaller.py

 

等5,6秒pyinstaller的INFO編譯完成之后,目錄里面就多出兩個(gè)文件夾 build   和  dist, 其中 dist 里面就是二進(jìn)制的打包文件

 

--遇到的問題:

在給打包文件添加icon的時(shí)候,報(bào)錯(cuò)

  

  File "C:/Python27/lib/site-packages/pyinstaller-2.1.1dev_-py2.7.egg/PyInstaller/build.py", line 320, in __postinit__    self.assemble()  File "C:/Python27/lib/site-packages/pyinstaller-2.1.1dev_-py2.7.egg/PyInstaller/build.py", line 1245, in assemble    icon.CopyIcons(tmpnm, self.icon)  File "C:/Python27/lib/site-packages/pyinstaller-2.1.1dev_-py2.7.egg/PyInstaller/utils/icon.py", line 170, in CopyIcons    hsrc = win32api.LoadLibraryEx(srcpath, 0, LOAD_LIBRARY_AS_DATAFILE)pywintypes.error: (193, 'LoadLibraryEx', '%1 /xb2/xbb/xca/xc7/xd3/xd0/xd0/xa7/xb5/xc4 Win32 /xd3/xa6/xd3/xc3/xb3/xcc/xd0/xf2/xa1/xa3')

當(dāng)時(shí)我選擇的icon參數(shù)是: --icon=favicon.png   

后來把icon格式改為favicon.ico,才可以, 且 icon的參數(shù)不能帶  " ' "  or  '  "   '

 

 


pyinstaller Using UPX

 

UPX的作用是給生成的exe加殼,減小體積,  我測試打包一個(gè)pyqt14Mb文件UPX壓縮后只有9Mb

下載upx

地址:http://upx.sourceforge.net/    (最新upx391w.zip,2013-05月)

官網(wǎng)說明

http://pythonhosted.org/PyInstaller/

Using UPX

UPX is a free utility available for most Operating systems. UPX compresses executable files and libraries, making them smaller, sometimes much smaller. UPX is available for most operating systems and can compress a large number of executable file formats. See the UPX home page for downloads, and for the list of supported executable formats. As of May 2013, the only major absence is 64-bit binaries for Windows and Mac OS X. UPX has no effect on these.

A compressed executable program is wrapped in UPX startup code that dynamically decompresses the program when the program is launched. After it has been decompressed, the program runs normally. In the case of a PyInstaller one-file executable that has been UPX-compressed, the full execution sequence is:

  • The compressed program start up in the UPX decompressor code.
  • After decompression, the program executes the PyInstaller bootloader, which creates a temporary environment for Python.
  • The Python interpreter executes your script.

PyInstaller looks for UPX on the execution path or the path specified with the --upx-dir option. If UPX exists, PyInstaller applies it to the final executable, unless the --noupx option was given. UPX has been used with PyInstaller output often, usually with no problems.

 

使用

將下載的UPX包解壓,將upx.exe 復(fù)制到pyinstaller的根目錄 或項(xiàng)目目錄,在pyinstaller參數(shù)中添加--upx-dir=FILE

注意:

  • 網(wǎng)上教程常見的-X選項(xiàng)啟用upx已經(jīng)失效
  • 如果upx.exe已經(jīng)復(fù)制到PyInstaller文件夾下,會默認(rèn)使用upx,如果不在文件夾下,可以使用--upx-dir選項(xiàng),如--upx-dir=upx_dir,如--upx-dir=/usr/local/share/   or --upx-dir=./
  • 如果upx.exe復(fù)制到了PyInstaller文件夾下,如果不想使用upx,需要添加參數(shù) --noupx

 

upx打包報(bào)錯(cuò):

upx: C:/Users/Administrator/AppData/Roaming/pyinstaller/bincache01_py27/mfcm90u.dll: CantPackException: .NET files (win32/.net) are not yet supported34447 INFO: Executing - ./upx --lzma -q C:/Users/Administrator/AppData/Roaming/pyinstaller/bincache01_py27/qtgui4.dll39400 INFO: Executing - ./upx --lzma -q C:/Users/Administrator/AppData/Roaming/pyinstaller/bincache01_py27/qtsvg4.dll39618 INFO: Executing - ./upx --lzma -q C:/Users/Administrator/AppData/Roaming/pyinstaller/bincache01_py27/qtxml4.dll39877 INFO: Executing - ./upx --lzma -q C:/Users/Administrator/AppData/Roaming/pyinstaller/bincache01_py27/qtopengl4.dllupx: C:/Users/Administrator/AppData/Roaming/pyinstaller/bincache01_py27/qtopengl4.dll: IOException: rename error: File existsCannot find ('qtopengl4.dll', 'C:/Users/Administrator/AppData/Roaming/pyinstaller/bincache01_py27/qtopengl4.dll', 1, 'b')Traceback (most recent call last):

意思就是沒有找到qtopeng14.dll, 

解決辦法: 復(fù)制Python安裝目錄C:/Python27/Lib/site-packages/PyQt4 下的QtOpenGL4.dll到C:/Users/Administrator/AppData/Roaming/pyinstaller/bincache01_py27

 


 

--version添加版權(quán)信息

 我們發(fā)現(xiàn)一般打包的二進(jìn)制exe文件都有版權(quán)信息,如QQ

那pyinstaller如何添加版權(quán)信息呢?

添加版權(quán)信息很簡單,添加參數(shù):

-v FILE, --version=FILE 加入版本信息文件

官網(wǎng)說明:

地址:  http://pythonhosted.org/PyInstaller/

Capturing Version Data

pyi-grab_version executable_with_version_resource

The pyi-grab_version command is invoked with the full path name of a Windows executable that has a Version resource. (A Version resource contains a group of data structures, some containing binary integers and some containing strings, that describe the properties of the executable. For details see the Version Information Structures page.)

The command writes text that represents a Version resource in readable form. The version text is written to standard output. You can copy it from the console window or redirect it to a file. Then you can edit the version information to adapt it to your program. This approach is used because version resources are complex. Some elements are optional, others required. When you view the version tab of a Properties dialog, there's no simple relationship between the data displayed and the structure of the resource. Using pyi-grab_version you can find an executable that displays the kind of information you want, copy its resource data, and modify it to suit your package.

The version text file is encoded UTF-8 and may contain non-ASCII characters. (Unicode characters are allowed in Version resource string fields.) Be sure to edit and save the text file in UTF-8 unless you are certain it contains only ASCII string values.

The edited version text file can be given with a --version-file= option to pyinstaller or pyi-makespec. The text data is converted to a Version resource and installed in the executable output.

In a Version resource there are two 64-bit binary values, FileVersion and ProductVersion. In the version text file these are given as four-element tuples, for example:

filevers=(2, 0, 4, 0),prodvers=(2, 0, 4, 0),

The elements of each tuple represent 16-bit values from most-significant to least-significant. For example the FileVersion value given resolves to 0002000000040000 in hex.

set_version version_text_file executable_file

The set_version utility reads a version text file as written by pyi-grab_version, converts it to a Version resource, and installs that resource in the executable_file specified.

For advanced uses, examine a version text file. You find it is Python code that creates a VSVersionInfo object. The class definition for VSVersionInfo is found in utils/versioninfo.py in the PyInstallerdistribution folder. You can write a program that imports that module. In that program you can eval the contents of a version info text file to produce a VSVersionInfo object. You can use the .toRaw()method of that object to produce a Version resource in binary form. Or you can apply the unicode() function to the object to reproduce the version text file.

使用pyinstaller內(nèi)置的grab_version.py工具獲得其他.exe程序的版本信息文件,版本信息文件里面包括公司名、程序名稱版本號、文件說明、語言等

然后再把這個(gè)信息里面的相關(guān)信息更改成你想要的信息,再使用上面的--version-file=version_text_file

使用

grab_version.py的位置:   Python安裝目錄/Lib/site-packages/PyInstaller-2.1.1dev_-py2.7.egg/PyInstaller/cliutils/grab_version.py

同時(shí)你也可以在Python安裝目錄找到C:/Python27/Scripts/pyi-grab_version.exe (前提已經(jīng)安裝pyinstaller) 直接使用pyi-grab_version.exe

python grab_version.py  C:/QQProtect.exe

pyi-grab_version.exe   C:/QQProtect.exe

產(chǎn)生的file_version_info.txt內(nèi)容如下:

# UTF-8## For more details about fixed file info 'ffi' see:# http://msdn.microsoft.com/en-us/library/ms646997.aspxVSVersionInfo(  ffi=FixedFileInfo(    # filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)    # Set not needed items to zero 0.    filevers=(3, 9, 3, 7012),    prodvers=(3, 9, 3, 0),    # Contains a bitmask that specifies the valid bits 'flags'r    mask=0x3f,    # Contains a bitmask that specifies the Boolean attributes of the file.    flags=0x0,    # The operating system for which this file was designed.    # 0x4 - NT and there is no need to change it.    OS=0x4,    # The general type of file.    # 0x1 - the file is an application.    fileType=0x1,    # The function of the file.    # 0x0 - the function is not defined for this fileType    subtype=0x0,    # Creation date and time stamp.    date=(0, 0)    ),  kids=[    StringFileInfo(      [      StringTable(        u'040904e4',        [StringStruct(u'CompanyName', u'Tencent'),        StringStruct(u'FileDescription', u'QQ安全防護(hù)進(jìn)程'),        StringStruct(u'FileVersion', u'3.9.3.7012'),        StringStruct(u'InternalName', u'QQProtect '),        StringStruct(u'LegalCopyright', u'Copyright (C) 1999-2014 Tencent All Rights Reserved'),        StringStruct(u'OriginalFilename', u'QQProtect.exe'),        StringStruct(u'ProductName', u'騰訊QQ'),        StringStruct(u'ProductVersion', u'3.9.3.0')])      ]),     VarFileInfo([VarStruct(u'Translation', [1033, 1252])])  ])

我們修改上面txt文件內(nèi)容信息保存到項(xiàng)目中的根目錄

修改后:

# UTF-8## For more details about fixed file info 'ffi' see:# http://msdn.microsoft.com/en-us/library/ms646997.aspxVSVersionInfo(  ffi=FixedFileInfo(    # filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)    # Set not needed items to zero 0.    filevers=(3, 9, 3, 7012),    prodvers=(3, 9, 3, 0),    # Contains a bitmask that specifies the valid bits 'flags'r    mask=0x3f,    # Contains a bitmask that specifies the Boolean attributes of the file.    flags=0x0,    # The operating system for which this file was designed.    # 0x4 - NT and there is no need to change it.    OS=0x4,    # The general type of file.    # 0x1 - the file is an application.    fileType=0x1,    # The function of the file.    # 0x0 - the function is not defined for this fileType    subtype=0x0,    # Creation date and time stamp.    date=(0, 0)    ),  kids=[    StringFileInfo(      [      StringTable(        u'040904e4',        [StringStruct(u'CompanyName', u'Pyqt公司'),        StringStruct(u'FileDescription', u'Pyqt版本信息測試'),        StringStruct(u'FileVersion', u'1.0.0.12'),        StringStruct(u'InternalName', u'www.pyqt.com '),        StringStruct(u'LegalCopyright', u'Copyright (C) 1999-2015 pyqt All Rights Reserved'),        StringStruct(u'OriginalFilename', u'getversion.exe'),        StringStruct(u'ProductName', u'Pyqt版本信息顯示效果'),        StringStruct(u'ProductVersion', u'1.9.3.0')])      ]),     VarFileInfo([VarStruct(u'Translation', [1033, 1252])])  ])

pyinstaller命令:

if __name__ == '__main__':    from PyInstaller.main import run    params=['Getversion.py', '-F', '-w', '--icon=favicon.ico', '--version-file=file_version_info.txt']    run(params)

效果:


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高雄市| 柳州市| 株洲县| 榆林市| 深水埗区| 滦南县| 高密市| 永春县| 江油市| 鹿泉市| 沁源县| 迭部县| 新丰县| 东乡族自治县| 石家庄市| 镇康县| 临武县| 乐昌市| 永年县| 寻乌县| 玛沁县| 青海省| 巴南区| 辽宁省| 新丰县| 临高县| 前郭尔| 朔州市| 武安市| 屯门区| 沅陵县| 龙里县| 平果县| 佛山市| 延边| 晋州市| 漳浦县| 儋州市| 栾城县| 马公市| 通河县|