当前位置:网站首页>Pyinstaller打包pikepdf失败的问题排查
Pyinstaller打包pikepdf失败的问题排查
2022-06-29 02:07:00 【虚幻私塾】
优质资源分享
| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
问题
最近在项目里用到了pikepdf这个库,用于实现pdf水印插入的一个小功能,源码调试阶段运行一切OK但是在出包时报了如下异常。
Traceback (most recent call last):
File "pikepdf\\_\_init\_\_.py", line 19, in
File "PyInstaller\loader\pyimod03\_importers.py", line 495, in exec\_module
File "pikepdf\\_version.py", line 13, in
File "importlib\metadata.py", line 530, in version
File "importlib\metadata.py", line 503, in distribution
File "importlib\metadata.py", line 177, in from\_name
importlib.metadata.PackageNotFoundError: pikepdf
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 1, in
File "PyInstaller\loader\pyimod03\_importers.py", line 495, in exec\_module
File "pikepdf\\_\_init\_\_.py", line 21, in
ImportError: Failed to determine version
[29708] Failed to execute script 'main' due to unhandled exception!
异常定位
打印了两份堆栈的信息,翻翻炸出来点 init.py 的21行,代码如下
try:
from ._version import __version__
except ImportError as _e: # pragma: no cover
raise ImportError("Failed to determine version") from _e
从.version.py文件导入__version__失败?看看_version.py
try:
from importlib_metadata import version as _package_version # type: ignore
except ImportError:
from importlib.metadata import version as _package_version
__version__ = _package_version('pikepdf')
__all__ = ['\_\_version\_\_']
再看看上面的异常,也就再_package_version这个函数了。这边可以先写个简单的demo.py验证下,使用pyinstgaller编译后运行。
# demo.py
import pikepdf
if __name__ == '\_\_main\_\_':
print("Hello World")
# 输出
Traceback (most recent call last):
File "pikepdf\\_\_init\_\_.py", line 19, in
File "PyInstaller\loader\pyimod03\_importers.py", line 495, in exec\_module
File "pikepdf\\_version.py", line 13, in
File "importlib\metadata.py", line 530, in version
File "importlib\metadata.py", line 503, in distribution
File "importlib\metadata.py", line 177, in from\_name
importlib.metadata.PackageNotFoundError: pikepdf
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 1, in
File "PyInstaller\loader\pyimod03\_importers.py", line 495, in exec\_module
File "pikepdf\\_\_init\_\_.py", line 21, in
ImportError: Failed to determine version
[29708] Failed to execute script 'main' due to unhandled exception!
符合预期。所以说importlib.metadata.version 无法在pyinstaller打包后运行?
问题原因
对于pkgs_to_check_at_runtime中列出的每个包,需要通过在spec文件中使用copy_metadata(name)来收集元数据。说白了就是pyinstaller打包后缺少对应的metadata信息。
修复方案
1. 降低版本pikepdf的版本
这个库以前用过,没有出这个幺蛾子。看了下旧版本的源代码,一下是5.1.3版本之前的get_version实现,没有使用importlib库,自然也不会有问题。
from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution as _get_distribution
try:
__version__ = _get_distribution(__package__).version
except DistributionNotFound: # pragma: no cover
__version__ = "Not installed"
__all__ = ['\_\_version\_\_']
2. 在pyinstaller打包时指定copy-metadata
像这样
pyinstaller -F --copy-metadata pikepdf main.py
碎碎念
我觉得完全可以像版本5.1.3之前一样,获取不到时赋值为"Not installed"一样就行。然后提了个pr[https://github.com/pikepdf/pikepdf/pull/358]给作者,可惜作者认为这是importlib的锅(不改),倒也时说得过去。
边栏推荐
- 想请教股票开户要认识谁?现在网上开户安全么?
- Server antivirus
- [apprendre la programmation FPGA - 49 à partir de zéro]: vision - Comment la puce a - t - elle été conçue?
- [redis] set type
- 【Redis】数据介绍 & 通用命令 & String类型
- SAP ui5 beginner tutorial 24 - how to use OData data model
- 网上联系客户经理办理炒股开户安全吗?
- 微信运动自动点赞
- Three tips from small video app source code developers on small video platforms
- How to become a senior digital IC Design Engineer (5-1) theory: clock technology, reset Technology
猜你喜欢

【Redis】数据介绍 & 通用命令 & String类型

Analysis of sending principle of OData metadata request for SAP ui5 application

How to choose source code encryption software

Business system anti-virus
Fundamentals of scala (3): operators and process control

基于 RISC-V SoC 的可配置 FFT 系统设计(1)引言
![[redis] key hierarchy](/img/ab/a5d3bb61b4571966d0f47037af4f41.png)
[redis] key hierarchy

Interviewer: with the for loop, why do you need foreach??

Finally got the byte offer. The 25-year-old inexperienced experience in software testing is written to you who are still confused

PHP hospital network reservation management system source code, hospital consultation reservation registration OA system (commercial or graduation design)
随机推荐
Near's storage stacking
What is stock online account opening? Is it safe to open an account online?
Convert flat structure to tree structure
直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!
Tiflash compiler oriented automatic vectorization acceleration
Large scale visual relationship understanding
Com3529 test analysis
Using autogluon to forecast house price
Utiliser kubernets Resource Lock pour compléter son application ha
Blog publishing test 1
大三下期末考试
[redis] set type
Scala 基础 (三):运算符和流程控制
Necessary technologies for chip manufacturers (1) Introduction
Blog publishing test 2
Is the ETF fund reliable and safe
如何成为一名高级数字 IC 设计工程师(1-1)Verilog 编码语法篇:引言
C language course design - food warehouse management system
How to use PN junction to measure temperature?
Why is it recommended that you choose the self research company as much as possible