在一台PC上存在一个版本以上的Python版本,比如说存在Python3.5,而后面又因为要是使用dataclass,要Python3.7版本;
而IDE PyCharm,在完成要进行package,如pack2exe文件是一个bat文件时,具体如下所示:
::PyInstaller: 3.2.1
::Python: 3.5.4
::Platform: Windows-7-6.1.7601-SP1
rmdir /q /s ".\dist"
rmdir /q /s ".\build"
xcopy ".\ui\images\*.*" ".\ui\exe\ui\images\" /s /h /d /y
xcopy "logging.conf" ".\ui\exe\" /s /h /d /y
::--hidden-import json
pyinstalle --uac-admin --icon=./ui/images/logo.ico -F -w HCILog.py
rmdir /q /s ".\build"
xcopy "dist\*.*" ".\ui\exe\" /s /h /d /y
rmdir /q /s ".\dist"
rm "HCILog.spec"
cd ".\ui\exe\"
HCILog.exe
在运行pack2exe.bat进行打包后会出现如下所示错误:
一开始以为是HCILog所含包的路径import 引入路径问题造成的,所以使用-D 再次package去查看具体问题(示例如下所示):
rmdir /q /s ".\dist"
rmdir /q /s ".\build"
xcopy ".\ui\images\*.*" ".\ui\exe\ui\images\" /s /h /d /y
xcopy "logging.conf" ".\ui\exe\" /s /h /d /y
::--hidden-import json
pyinstaller --uac-admin --icon=./ui/images/logo.ico -D HCILog.py
rmdir /q /s ".\build"
xcopy "dist\*.*" ".\ui\exe\" /s /h /d /y
rmdir /q /s ".\dist"
rm "HCILog.spec"
cd ".\ui\exe\"
HCILog.exe
在cmd中执行hciLog.exe文件显示如下错误信息:
str(Exception):
str(e): No module named 'hciutils.btsnoop'
repr(e): ImportError("No module named 'hciutils.btsnoop'",)
Traceback (most recent call last):
File "pytools\HCILog.py", line 8, in
ImportError: No module named 'hciutils.btsnoop'
traceback.print_exc(): None
traceback.format_exc(): Traceback (most recent call last):
File "pytools\HCILog.py", line 8, in
ImportError: No module named 'hciutils.btsnoop'
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
Traceback (most recent call last):
File "pytools\HCILog.py", line 164, in
File "pytools\HCILog.py", line 29, in __init__
File "pytools\HCILog.py", line 55, in InitLog
File "pytools\HCILog.py", line 109, in updateCfg
NameError: name 'uart_transport_t' is not defined
[9368] Failed to execute script HCILog
然后再次查看import路径,更改package的主HCIlog.py文件,发现依然存在问题;
但是直接运行HCILog.py是没有任何问题的;
崩溃中... ...
一个偶然的机会发现__pycache__目录中存在一个类似HCILog.cpython-35.pyc文件;
我就茫然了,采用的Python3.7进行实现的。
此处应该是HCILog.cpython-37.pyc才对的,怎么会3.5呢?问题很关键呀,那肯定是PyInstaller引用错误了。
查资料Python3.4是支持package 3.7的呀,难不成在安装PyInstaller时,所安装的路径不对。
切换PyInstaller的安装路径到Python 3.7目录下,重新安装;
更改pack2exe.bat文件中路径:
"D:\\Program Files (x86)\\Python37-32\\Scripts\\pyinstaller" --uac-admin --icon=./ui/images/logo.ico -F -w HCILog.py
再次执行pack2exe.bat打包,完成后,OK。