pyinstaller 将.py生成.exe ----报错 “IndexError: tuple index out of range”

2023-06-07,,

pyinstaller将py打包为exe文件,用pysintaller居然报错

  File "c:\anaconda3\lib\site-packages\PyInstaller\depend\utils.py", line 226, in __scan_code_instruction_for_ctypes
name = co.co_names[oparg]
IndexError: tuple index out of range

1
2
3

搜索了一番,用群众神奇的方式

找到D:\python\Python36-32\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py
路径根据安装目录找,上面是我的安装目录
打开modulegraph.py将大约2731附近将

return module_code_object.co_names[co_names_index]
更改为
return co_names_index
#coding by rekfan on 2017/03/21 10:40
#return module_code_object.co_names[co_names_index]

1
2
3
4
5

重新运行后发现……前一个地方不报错了,又有其他地方报错了,最后追查一圈发现了原因……使用pyinstaller的开发者新版,官网目前的版本是3.2.1 只支持到python3.5 ,恰好我用的Anaconda3装的python是3.6版本的。pyinstaller3.3 还没有发行,但是官网源码里有 https://github.com/pyinstaller/pyinstaller 替换 D:\python\Python36-32\Lib\site-packages\PyInstaller 即可 这样就支持python3.6了 不过是开发版,可能还不完善。

附带pyinstaller的使用方法

pip 安装,国内的源速度飞快

pip install -i https://pypi.douban.com/simple pyinstaller

1

然后正常输入pyinstaller -h 可以看到全面的帮助,最常见的把一个py脚本打包成exe就是
确保命令行已经在该文件目录下后输入

pyinstaller -F -c target.py 

1

-F代表打包成一个exe文件,-c是命令行脚本,如果有GUI则改为-w
具体的参数太多了,直接pyinstaller -h看吧。

===========================================================================================================

1. 错误类型:

ImportError: C extension: No module named ‘pandas._libs.tslibs.timedeltas’ n

2. 错误分析:

因为我的代码中引用了pandas库,然后使用pyinstaller打包的时候显示打包成功,但实际运行过程中显示模块缺失

3. 尝试解决方案:

3.1 删除pandas库重新安装

走外网的pip速度慢的时候,可以使用国内镜像,豆瓣镜像下载

pip install -i https://pypi.douban.com/simple/ packages-name)

1

重新安装后,再重新打包依然无法解决问题

3.2 再次百度

CSDN论坛的回答
经测试解决了我的问题。

4. 正式解决方案

修改原有的打包代码为:

pyinstaller -F -w -i xxx.ico yyy.py --hidden-import=pandas._libs.tslibs.timedeltas

1
-F 指只生成一个exe文件,不生成其他dll文件
-w 不弹出命令行窗口
-i 设定程序图标 ,其后面的ico文件就是程序图标
yyy.py 就是要打包的程序
–hidden-import=pandas._libs.tslibs.timedeltas 隐藏相关模块的引用

======================================================================================================

如果遇到错误:ImportError: DLL load failed: 找不到指定的模块
出现错误原因:安装包的来源问题,也可以理解为包版本兼容问题,有的包使用官方出版,有的包使用whl文件安装

解决方案:将所有包都统一来源,要么全部使用官方出版的包,要么全部使用whl里面的包,问题就解决了

numpy+scipy+scikit-learn组件,使用whl文件安装下载:

http://pan.baidu.com/s/1hstDOo8

第二步:去到Python安装Scripts目录下,再使用pip install xxx.whl安装,先装Numpy\Scipy包,再安装Scikit-Learn。

pyinstaller 将.py生成.exe ----报错 “IndexError: tuple index out of range”的相关教程结束。

《pyinstaller 将.py生成.exe ----报错 “IndexError: tuple index out of range”.doc》

下载本文的Word格式文档,以方便收藏与打印。