pyinstaller总结
一、pyinstaller的安装
pip install pyinstaller
二、pyinstaller打包方法
linux下:
只能生成linux下的可执行文件
windows下:
生成exe可执行文件
pyinstaller xx.py
pyinstaller -F xx.py#-D
pyinstaller -F -w -i x.ico xx.py
以后打包基本上用如下代码:
pyinstaller xx.py -w -i xx.ico -p xx1.py,xx2.py,xx3.py
三、pyinstaller打包过程中出现的问题
1、RecursionError: maximum recursion depth exceeded
解决方法:
在pyinstaller xx.py 之后生成的xx.spec里面加上如下代码:
import sys
sys.setrecursionlimit(10000)
重新保存xx.spec之后,在cmd中再次运行如下代码:
pyinstaller xx.spec
2、针对之前完全打包成onefile 的exe之后,运行会出现如下问题:
1)pandas包无法调用
2)plot出现一些问题
3)https://blog.csdn.net/dby_bright/article/details/101076953
鉴于上述问题,建议直接打包,使用如下代码:
pyinstaller xx.py
四、pyinstaller打包会出现的问题
1.我的最终解决方案是在pipenv shell环境中打包成功的
Pyinstaller打包报错Failed to execute script pyi_rth_pkgres
Pyinstaller打包项目文件的时候
出现报错
fatal error detected
failed to execute script pyi_rth_pkgres
检测到致命错误
无法执行脚本pyi_rth_pkgres
思来想去,我写的项目并没有用到这个叫做pyi_rth_pkgres的脚本,由于我是使用conda创建的子环境下载的pyinstaller(我才重装过系统),莫非是anaconda中package出现了问题导致这个情况。
先是经过网上大量的说用的方法(实际上一点用都没有)
Usage: pipenv [OPTIONS] COMMAND [ARGS]...
Options:
--where Output project home information.
--venv Output virtualenv information.
--py Output Python interpreter information.
--envs Output Environment Variable options.
--rm Remove the virtualenv.
--bare Minimal output.
--completion Output completion (to be eval'd).
--man Display manpage.
--support Output diagnostic information for use in GitHub issues.
--site-packages Enable site-packages for the virtualenv. [env var:
PIPENV_SITE_PACKAGES]
--python TEXT Specify which version of Python virtualenv should use.
--three / --two Use Python 3/2 when creating virtualenv.
--clear Clears caches (pipenv, pip, and pip-tools). [env var:
PIPENV_CLEAR]
-v, --verbose Verbose mode.
--pypi-mirror TEXT Specify a PyPI mirror.
--version Show the version and exit.
-h, --help Show this message and exit.
Usage Examples:
Create a new project using Python 3.7, specifically:
$ pipenv --python 3.7
Remove project virtualenv (inferred from current directory):
$ pipenv --rm
Install all dependencies for a project (including dev):
$ pipenv install --dev
Create a lockfile containing pre-releases:
$ pipenv lock --pre
Show a graph of your installed dependencies:
$ pipenv graph
Check your installed dependencies for security vulnerabilities:
$ pipenv check
Install a local setup.py into your virtual environment/Pipfile:
$ pipenv install -e .
Use a lower-level pip command:
$ pipenv run pip freeze
Commands:
check Checks for security vulnerabilities and against PEP 508 markers
provided in Pipfile.
clean Uninstalls all packages not specified in Pipfile.lock.
graph Displays currently-installed dependency graph information.
install Installs provided packages and adds them to Pipfile, or (if no
packages are given), installs all packages from Pipfile.
lock Generates Pipfile.lock.
open View a given module in your editor.
run Spawns a command installed into the virtualenv.
shell Spawns a shell within the virtualenv.
sync Installs all packages specified in Pipfile.lock.
uninstall Un-installs a provided package and removes it from Pipfile.
update Runs lock, then sync.
原理大致就是忽略这个包
pyinstaller --hidden-import pyi_rth_pkgres -w -F C:/kivy_test.py
实际上毫无作用,依旧报错(另一个内容)
尝试重装pyinstaller
pip uninstall pyinstaller
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
依旧毫无作用,报错信息任然一样。
开始排查,发现曾经的打包参数依旧全部保留,有一些路径信息任然还在,于是我在项目文件夹中依次删除了,build/,dist/,及main.spec这三个用于存储打包配置的信息文件。
????再度打包,成功
说明pyinstaller创建的配置相关缓存文件并不是每次创建的,是第一次创建,之后直接读取,一旦发生一丁点的环境变化,打包可能就会遇到问题。
PS:
当时网上还找了一些资料
main return -1
这种错误基本都是自己的问题, 只能在输出exe时参数加个’-d’即debug模式, 然后再查下打印的错误信息Failed to execute script pyi_rth_pkgres
可以先换Pyinstaller的版本, 这个错误会消失, 但会弹出其他的错误信息, 然并卵这种错误基本都是自己的问题, 只能在输出exe时参数加个’-d’即debug模式, 然后再查下打印的错误信息
Failed to execute script xxxx
这种错误基本都是自己的问题, 只能在输出exe时参数加个’-d’即debug模式, 然后再查下打印的错误信息
This application failed to start … Qt platform plugin …
这种错误先配下PyQt5的plugins的环境变量, 如博主的是C:\Python34\Lib\site-packages\PyQt5\plugins不行再换Pyinstaller的版本 (貌似3.0.0这个版本有问题, 后来换3.2.1就没事了)
2.打包出现的迭代问题
cd
pyinstaller -F -w -i favicon love.py
-w 表示隐藏程序运行时的命令行窗口(不加-w会有黑色窗口)
括号内的为可选参数,-i icofile表示给程序加上图标,图标必须为.ico格式
icofile表示图标的位置,建议直接放在程序文件夹里面,这样子打包的时候直接写文件名就好
今天在用python写一个递归查询数据库的程序时,报了一个错误:
RecursionError: maximum recursion depth exceeded in comparison
原因
查询过相关文档和资料后才发现了问题原因,python的递归深度是有限制的,默认为1000。当递归深度超过1000时,就会报错。
解决办法
- 执行 pyinstaller,会生成 filename.spec文件:
- 在 filename.spec 文件头添加下面语句
import sys
sys.setrecursionlimit(5000)
- 再次执行 pyinstaller 和 .spec文件
pyinstaller filename.spec
可以将递归的深度修改的大一些,即可解决问题,但是还是建议在程序中不要使用太深的递归层数。
import sys
sys.setrecursionlimit(100000) #例如这里设置为十万
补充测试
由于对最大递归层数产生兴趣,于是我在自己电脑上用以下代码做了测试:
def recursion(depth):
depth += 1
print(depth)
recursion(depth)
recursion(0)
反复测试的结果显示,最大递归的层数到达997层时即会出现报错的提示。
补充测试2
在修改最大递归层数为100000后,再执行上面的代码,发现最多也只是可以递归到第3220层。程序没有报错,但是会自动退出。如果你知道为什么,请留言。
3.打包完成之后文件太大
对于这样的打包 建议用直接直接打包
pyinstller -i 1.ico xx.py