pyinstaller打包之后会遇到运行中库报错的问题
直接使用 pyinstaller -F xxxx.py 是有问题的 需要导入指定的库的目录
opts.append('--add-data='+r"D:anacondeenvszzhLibsite-packagescpcaresources;cpcaresources")
opts.append('--add-data='+r"D:anacondeenvszzhLibsite-packagespystrichfonts;pystrichfonts")
完整的一键打包py文件 自行拿
EXTRA_OPT_SWITCH = 'ext'
Versions = ""
ROBOT_NAME = ""
EXE_NAME = ROBOT_NAME + Versions #编译好后的exe的名字。
FILE_NAME = EXE_NAME + ".exe"
EXE_PARENT_DIR_NAME = 'yd_syst'
''
'em' #程序父目录的名字。
EXE_CONSOLE = True #有无控制台黑框,True/False。
EXE_ONEFILE = True #是否打包成单个文件(One File)。单个文件启动速度会变慢。
ICO_PATH_FILE = 'yd.ico' #exe文件图标的路径,默认为None时,会使用Pyinstaller自带的图标。
def copyDir(srcDir, dstDir, printMsg):
"""
将srcDir目录拷贝到dstDir目录中。
如目标存在,则会删除。
"""
print(printMsg)
if os.path.exists(srcDir):
if os.path.exists(dstDir):
shutil.rmtree(dstDir)
shutil.copytree(srcDir, dstDir)
else:
print(srcDir, "此目录不存在, 故忽略.")
def un_zip(file_name, path):
"""unzip zip file"""
zip_file = zipfile.ZipFile(file_name)
for names in zip_file.namelist():
zip_file.extract(names, path)
zip_file.close()
def extraOPt():
"""
一些额外的操作。
"""
print("[INFO(自动化)]:重命名dist目录以避免中文目录可能导致程序异常.")
sDir,dDir = r'dist/'+EXE_NAME, r'dist/'+EXE_PARENT_DIR_NAME
if os.path.exists(dDir):
shutil.rmtree(dDir)
os.rename(sDir, dDir)
newExeDir = r"dist/"+EXE_PARENT_DIR_NAME
newResDir = newExeDir+r'/ui/res'
newLibsDir = newExeDir+r'/libs'
copyDir(r'ui/res', newResDir, "[INFO(自动化)]:拷贝ui/res目录到dist中.")
copyDir(r'libs', newLibsDir, "[INFO(自动化)]:拷贝libs目录到dist中...")
un_zip(r'libs/cefpython3.zip', newExeDir)
os.remove(newExeDir+r'/libs/cefpython3.zip')
print("[INFO(自动化)]:压缩整个%s目录..."%EXE_PARENT_DIR_NAME)
try:
import zlib
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
#进入dist目录
os.chdir('dist')
zFileName = EXE_PARENT_DIR_NAME+'.zip'
zFileObj=zipfile.ZipFile(zFileName, 'w',compression)
try:
for dirpath,dirs,files in os.walk(EXE_PARENT_DIR_NAME):
for file in files:
if file == zFileName:
print("skip myself--------------")
continue
z_path = os.path.join(dirpath,file)
print('[%s]compressing:%s'%(dirpath, z_path))
zFileObj.write(z_path)#,z_path[start:]
finally:
if zFileObj:
zFileObj.close()
print("[INFO(自动化)]:完毕。弹出压缩文件目录...")
#os.system("explorer.exe .")
import platform, subprocess
if platform.system() == 'Windows':
subprocess.Popen("explorer.exe .")
def _standardized_pictures(path):
"""
标准化png图片,避免使用时终端显示的异常("libpng warning: iccp:known incorrect sRGB profile").
"""
qm = QImage()
try:
for dir_path, dir_names, file_paths in os.walk(path):
for image in file_paths:
# 获取所有png图片地址并处理
if os.path.splitext(image)[1] == '.png':
qm.load(os.path.join(dir_path, image))
qm.save(os.path.join(dir_path, image))
print("标准化.png成功!")
except FileNotFoundError as ax:
print(ax)
print("标准化出错!")
finally:
return
if __name__ == '__main__':
# 定位项目地址
path_project = os.getcwd()
# 处理项目中的所有png图片
_standardized_pictures(path_project)
from PyInstaller.__main__ import run
opts = [
'main.py',
'-c' if EXE_CONSOLE else '-w',
'--name='+EXE_NAME
]
if EXE_ONEFILE:
opts.append('-F')
if ICO_PATH_FILE:
opts.append('--icon='+ICO_PATH_FILE)
opts.append('--add-data='+r"D:anacondeenvszzhLibsite-packagescpcaresources;cpcaresources")
opts.append('--add-data='+r"D:anacondeenvszzhLibsite-packagespystrichfonts;pystrichfonts")
run(opts)
if EXTRA_OPT_SWITCH in sys.argv:
extraOPt()



