遇到了几个问题,记录一下
ubuntu16.04系统 python3.6 conda (注意ubuntu的可执行文件就直接是文件名,windows的可执行文件是exe)
pip install pyinstaller
然后直接
pyinstaller -F 文件名.py
在当前目录下面有一个dist文件夹 直接运行./文件名即可
结果在运行的时候出现问题
Traceback (most recent call last):
File "site-packages/PyInstaller/loader/rthooks/pyi_rth_pkgres.py", line 13, in <module>
File "/home/xx/anaconda3/envs/keras/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/pkg_resources/__init__.py", line 86, in <module>
ModuleNotFoundError: No module named 'pkg_resources.py2_warn'
[132693] Failed to execute script pyi_rth_pkgres
有文章要降setuptools版本,或者升级
都对我无效
我是卸载了pip上的pyinstaller,改在conda环境里安装pyinstaller,结果出现了
Traceback (most recent call last):
File "xx.py", line 6, in <module>
File "/home/xx/anaconda3/envs/keras/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/numpy/__init__.py", line 151, in <module>
File "/home/xx/anaconda3/envs/keras/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/numpy/ctypeslib.py", line 369, in <module>
File "site-packages/numpy/ctypeslib.py", line 358, in _get_typecodes
File "site-packages/numpy/ctypeslib.py", line 358, in <dictcomp>
ModuleNotFoundError: No module named 'numpy.core._dtype_ctypes'
解决办法1
解决办法参考链接
先将.py文件转换为.spec
pyi-makespec --noupx -F --hidden-import=numpy.core._dtype_ctypes 文件名.py
再生成可执行文件
pyinstaller 文件名.spec
搞定!
解决办法2 (最终采用)
学习链接
分析问题出现原因:pyinstaller 打包时有其他的包(未在你文件内的import中)没有调用成功,所以出现
ModuleNotFoundError: No module named 'xxx'
其实可以跳过spec的过程,最后显示缺哪个包就在打包时强行加上这个包。
我在后续打包的时候出现缺少 ‘six’ 包,可以输入:
pyinstaller -F 文件名.py --hidden-import six
进行编译得到可执行文件(根目录dist下)
顺利解决。