pyinstaller总结

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

12345678902、针对之前完全打包成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时,就会报错。
解决办法

  1. 执行 pyinstaller,会生成 filename.spec文件:
  2. 在 filename.spec 文件头添加下面语句
import sys
sys.setrecursionlimit(5000)
  1. 再次执行 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

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyInstaller是一个用于将Python程序打成独立可执行文件的工具。关于在虚拟环境下使用PyInstaller的问题,有以下几点要注意: 首先,建议在虚拟环境中安装PyInstaller,并使用虚拟环境中的PyInstaller来进行打操作。这样可以避免系统环境和虚拟环境之间的冲突。 其次,可以使用pipenv来创建和管理虚拟环境。pipenv是一个流行的Python虚拟环境工具,可以方便地在项目中管理依赖和环境。通过安装pipenv并使用它来创建虚拟环境,可以确保最新版本的PyInstaller被安装在虚拟环境中。 另外,如果你的项目有较多的依赖项,建议先使用pyinstaller来打,而不是直接使用pyinstaller -F -w xxx.py的方式。这是因为,直接使用pyinstaller -F -w xxx.py生成exe文件可能会隐藏一些潜在的依赖问题。通过先使用pyinstaller -F xxx.py命令,可以查看打过程中是否有缺失的依赖项。 总结起来,在使用PyInstaller时,建议在虚拟环境中安装并使用PyInstaller,可以使用工具如pipenv来创建和管理虚拟环境,并在打前先使用pyinstaller -F xxx.py命令来检查依赖项是否完整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [pyinstaller指南,No module named xxx,is only available if OpenCV is installed.虚拟环境打](https://blog.csdn.net/qq_40094716/article/details/121967379)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [使用虚拟环境打python为exe 文件的方法](https://download.csdn.net/download/weixin_38750999/13998093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值