前言


最近有个需求,需要在 ubnutu22 上使用 pyinstaller 打包一个python2 的文件。

中间遇到了一些问题:

  1. pip2 install pyinstaller 报错

    解决方案:
pip2 install pyinstaller == 3.6
  1. python2 和 python3 的 pyinstaller 如何同时存在,我想把 python2 的 pyinstaller 命名为 pyinstaller2,

把 python3 的 pyinstaller 不重名。

# 如果安装了 python3 的 pyinstaller,需要先卸载
pip3 uninstall pyinstaller # 1. 安装python2 的 pyinstaller
pip2 install pyinstaller == 3.6
# 2. 找到 pyinstaller 位置 (我的环境是在 /usr/local/bin/pyinstaller)
whereis pyinstaller
# 3. 重命名 python2 的 pyinstaller 为 pyinstaller2
cd /usr/local/bin/
mv pyinstaller pyinstaller2
# 4. 检查
pyinstaller2 --version # 5. 重新安装 python3 的 pyinstaller
pip3 install pyinstaller # 6. 检查python3 的 pyinstaller
pyinstaller --version

然后,使用命令,打包出现报错

pyinstaller2 -F tst.py -p /usr/lib/python2.7/dist-packages/
1463 INFO: Python library not in binary dependencies. Doing additional searching...
Traceback (most recent call last):
File "/usr/local/bin/pyinstaller2", line 8, in <module>
sys.exit(run())
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/__main__.py", line 114, in run
run_build(pyi_config, spec_file, **vars(args))
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/__main__.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/building/build_main.py", line 734, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/building/build_main.py", line 681, in build
exec(code, spec_namespace)
File "/mnt/wallE_code_u22/pytest/pythonApiTst/code_u22/christie3_ctl.spec", line 17, in <module>
noarchive=False)
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/building/build_main.py", line 244, in __init__
self.__postinit__()
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/building/datastruct.py", line 160, in __postinit__
self.assemble()
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/building/build_main.py", line 478, in assemble
self._check_python_library(self.binaries)
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/building/build_main.py", line 568, in _check_python_library
python_lib = bindepend.get_python_library_path()
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/depend/bindepend.py", line 915, in get_python_library_path
python_libname = findLibrary(name)
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/depend/bindepend.py", line 775, in findLibrary
utils.load_ldconfig_cache()
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/depend/utils.py", line 400, in load_ldconfig_cache
path = m.groups()[-1]
AttributeError: 'NoneType' object has no attribute 'groups'

下面,将描述我的解决方案

正文


chatgpt 建议使用 sudo ldconfig 刷新共享库缓存,我试了但是还是报上面的错误。

后来 view /usr/local/lib/python2.7/dist-packages/PyInstaller/depend/utils.py 400 行,找到了报错的地方

try:
text = compat.exec_command(ldconfig, ldconfig_arg)
except ExecCommandFailed:
logger.warning("Failed to execute ldconfig. Disabling LD cache.")
LDCONFIG_CACHE = {}
return text = text.strip().splitlines()[splitlines_count:] LDCONFIG_CACHE = {}
for line in text:
# :fixme: this assumes libary names do not contain whitespace
m = pattern.match(line)
path = m.groups()[-1]
if is_freebsd or is_openbsd:
# Insert `.so` at the end of the lib's basename. soname
# and filename may have (different) trailing versions. We
# assume the `.so` in the filename to mark the end of the
# lib's basename.
bname = os.path.basename(path).split('.so', 1)[0]
name = 'lib' + m.group(1)
assert name.startswith(bname)
name = bname + '.so' + name[len(bname):]
else:
name = m.group(1)
# ldconfig may know about several versions of the same lib,
# e.g. differents arch, different libc, etc. Use the first
# entry.
if not name in LDCONFIG_CACHE:
LDCONFIG_CACHE[name] = path

这里path = m.groups()[-1] 的 m 是个 None 类型,然后 m的来源是 m = pattern.match(line), line 是获取共享库后逐行读取的字段。那就是 m = pattern.match(line) 出现了异常。

打印 m 报错 None 时 line 的值,发现此时 line: 缓存生成方: ldconfig (Ubuntu GLIBC 2.35-0ubuntu3.6) stable release version 2.35, 程序就是在解析这句时报错,原因是格式不匹配。

使用系统指令

sudo ldconfig -p

发现打印的信息最后一行就是:缓存生成方: ldconfig (Ubuntu GLIBC 2.35-0ubuntu3.6) stable release version 2.35

解决方案,修改 utils.py 中的处理过程,略过 m 为 None 的部分


LDCONFIG_CACHE = {}
for line in text:
# :fixme: this assumes libary names do not contain whitespace
m = pattern.match(line) # brian add 2024-05-09
if m is None:
print(line)
continue
# brian add end path = m.groups()[-1]
if is_freebsd or is_openbsd:
# Insert `.so` at the end of the lib's basename. soname
# and filename may have (different) trailing versions. We
# assume the `.so` in the filename to mark the end of the
# lib's basename.
bname = os.path.basename(path).split('.so', 1)[0]
name = 'lib' + m.group(1)
assert name.startswith(bname)
name = bname + '.so' + name[len(bname):]
else:
name = m.group(1)
# ldconfig may know about several versions of the same lib,
# e.g. differents arch, different libc, etc. Use the first
# entry.
if not name in LDCONFIG_CACHE:
LDCONFIG_CACHE[name] = path

添加了这个字段

        # brian add 2024-05-09
if m is None:
print(line)
continue
# brian add end

果然python2 停止维护后,还需要手动修改bug,很难忘的过程,在此记录,以做参考。

ubuntu22 python2 pyinstaller 打包报错:'NoneType' object has no attribute 'groups'的更多相关文章

  1. 在Python中使用moviepy进行视频剪辑时输出文件报错 ‘NoneType‘ object has no attribute ‘stdout‘问题

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 movipy输出文件时报错 'NoneType' ...

  2. Python PyInstaller 打包报错:AttributeError: 'str' object has no attribute 'items'

    pyinstaller打包时报错:AttributeError: 'str' object has no attribute 'items' 网上查询,可能是setuptools比较老: 更新一下 p ...

  3. python打包--pyinstaller打包报错

    先来一段报错信息  目前打包主要涉及socket模块出现的报错 missing module named resource - imported by posix, C:\Users\Administ ...

  4. 完美解决pyinstaller 打包报错找不到依赖pypiwin32 或pywin32-ctypes的错误

    报错信息 最近闲来无事,用python的tkinter库开发了一款带日程提醒的万年历桌面程序.在程序开发结束开始打包时,却发现一直报错 PyInstaller cannot check for ass ...

  5. python报错Nonetype object is not iterable

    https://www.cnblogs.com/zhaijiahui/p/8391701.html 参考链接:http://blog.csdn.net/dataspark/article/detail ...

  6. 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7

    最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...

  7. pyinstaller打包报错:AttributeError: 'str' object has no attribute 'items'

    导致原因和python多数奇奇怪怪的问题一样,依赖包的版本问题. 解决办法: 对setuptools这个包进行升级,链接在这里 https://pypi.org/project/setuptools/ ...

  8. pyinstaller打包报错: RecursionError: maximum recursion depth exceeded 已经解决

    看上去似乎是某个库自己递归遍历超过了python的栈高度限制 搜索了一番,很快找到了解决办法: https://stackoverflow.com/questions/38977929/pyinsta ...

  9. 报错 'dict' object has no attribute 'has_key'

    has_key方法在python2中是可以使用的,在python3中删除了. 比如: if dict.has_key(word): 改为: if word in dict:

  10. 'NoneType' object has no attribute '__getitem__'

    报错 'NoneType' object has no attribute '__getitem__' 你 result 返回的是 None ,所以 result [0] 取不了值

随机推荐

  1. SRAM、DRAM、Flash、DDR有什么区别

    SRAM SRAM的全称是Static Rnadom Access Memory,翻译过来即静态随机存储器.这里的静态是指这种存储器只需要保持通电,里面的数据就可以永远保持.但是当断点之后,里面的数据 ...

  2. 使用 Splashtop 启用员工远程访问

    使员工进行远程工作似乎是一项耗时.不安全且昂贵的任务.但是,借助 Splashtop,您可以快速.轻松.安全地使您的员工从任何位置以最高 价值远程访问其工作站. ​ 如何使用 Splashtop 启用 ...

  3. Advanced .Net Debugging 8:线程同步

    一.介绍 这是我的<Advanced .Net Debugging>这个系列的第八篇文章.这篇文章的内容是原书的第二部分的[调试实战]的第六章[同步].我们经常写一些多线程的应用程序,写的 ...

  4. 微信小程序校园跑腿系统怎么做,如何做,要做多久

    ​ 在这个互联网快速发展.信息爆炸的时代,人人都离不开手机,每个人都忙于各种各样的事情,大学生也一样,有忙于学习,忙于考研,忙着赚学分,忙于参加社团,当然也有忙于打游戏的(还很多),但生活中的一些琐事 ...

  5. .NET 6+Semantic Kernel快速接入OpenAI接口

    大家好,我是Edison. 今天我们快速地使用Semantic Kernel来集成OpenAI,使用20来行代码快速实现一个简单的AIGC应用. 这里,我就不多介绍Semantic Kernel了,包 ...

  6. LlamaFS自组织文件管理器

    LlamaFS是一个自组织文件管理器.它可以基于文件内容和修改时间等属性自动重命名和组织您的文件.它能让你不把时间花在对文件的复制.粘贴.重命名.拷贝.排序等简单操作上.有幸在Github上看到Lla ...

  7. 《iOS面试之道》-“串行队列的代码实战” 勘误

    一.原书第一版154页开始讲解串行队列.并发队列.以及在Dispatch_Async.Dispatch_Sync下面的作用 最后一段代码: if(_q == NULL) { _q = dispatch ...

  8. CH57x/CH58x/CH59x获取从机广播信息

    有时需要通过主机设备(MCU非手机)获取从设备的广播信息例如广播包,MAC地址,扫描应答包等 以下的程序片段及功能实现是在WCH的CH59X的observer例程上实现的: 1.获取广播包 所有的函数 ...

  9. Qt--点击按钮弹出一个对话框

    本文简要说明,如何实现点击按钮弹出一个文本框. 1)首先创建工程,我们就创建一个QMainWindow,不选择UI,就好了. 2)然后再单独创建一个C++类文件,最后得到的工程代码如下: 由于在创建m ...

  10. Vue3等比例缩放图片组件

    本文由 ChatMoney团队出品 有些情况我们需要在各种刁钻的情况下都要保持图片比例不变,比如用户缩放窗口等改变布局的情况.实现原理就是通过容器的宽度和内边距在保持你想要的比例. 以下是基础功能的组 ...