当前位置:网站首页>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的锅(不改),倒也时说得过去。
边栏推荐
- [learn FPGA programming from scratch -49]: Vision - how is the chip designed?
- Who do you want to know when opening a stock account? Is it safe to open an account online now?
- 指南针手机股票开户哪个券商更安全更方便?
- Wechat campaign auto like
- In MySQL database, the two data written when creating tables with foreign keys are the same. Do I copy them or fail to display them
- [apprendre la programmation FPGA - 49 à partir de zéro]: vision - Comment la puce a - t - elle été conçue?
- 数字 IC 设计、FPGA 设计秋招笔试题目、答案、解析(1)2022 紫光展锐(上)
- Test a CSDN free download software
- [从零开始学习FPGA编程-50]:视野篇 - 芯片是如何被制造出来的?芯片制造的十三大步骤。
- 7-29 delete substring in string (non KMP)
猜你喜欢
随机推荐
如何成为一名高级数字 IC 设计工程师(4-5)脚本篇:Shell 脚本实现的文件比较操作
Use code binding DataGridView control to display tables in program interface
Fundamentals of scala (3): operators and process control
How to become a senior digital IC Design Engineer (5-1) theory: clock technology, reset Technology
110. 简易聊天室13:聊天室服务端
[learn FPGA programming from scratch -50]: Vision - how are chips made? The thirteen steps of chip manufacturing.
Analysis of advantages and disadvantages of environment encryption and transparent encryption
Near consensus mechanism
在按钮禁用时消除hover效果
Configurable FFT system design based on risc-v SOC (1) Introduction
想请教一下,如何选择券商?现在网上开户安全么?
【Redis】初识 Redis
[learning notes] subsets and questions
How to become a senior digital IC Design Engineer (1-1) Verilog coding Grammar: Introduction
Utiliser kubernets Resource Lock pour compléter son application ha
What is stock online account opening? Is it safe to open an account online?
Using autogluon to forecast house price
QT basics tutorial: qstringlist
Junior final exam
Ambiguity between 0 and 1





![[redis] sortedset type](/img/7f/f5f1aa603c8994b669d52a435fed7e.png)

![[redis] set type](/img/97/0a83016e89316849c01072044f2124.png)
