PyInstaller可执行文件找不到包含的flASK-COMPRESS

2022-03-23 00:00:00 python pyinstaller

问题描述

这是我的系统信息:

123 INFO: PyInstaller: 4.0
123 INFO: Python: 3.5.4
124 INFO: Platform: Windows-10-10.0.18362-SP0

我一直在尝试使用Pyinstaller生成要在应用程序中使用的Python(PyQt)可执行文件。但是,当我将可执行文件打包并运行时,它将抛出以下内容:

pkg_resources.DistributionNotFound: The 'flask-compress' distribution was not found and is required 
by the application
[14684] Failed to execute script main

此依赖项已存在于我的虚拟环境中,我已尝试指定站点软件包目录和flask_compress导入的路径,如下所示:

pyinstaller --paths C:Usersalan9PycharmProjectsPracticumProjectvenvLibsite-packages --hidden-import=flask_compress main.py

注意:我尝试使用不同的python版本、使用不同的pyinstaller标志(onefile、windowed、onedir)、在安装了Windows 7/10的不同计算机上、在Windows 10 VM的干净副本上以及使用FBS来创建此应用程序的可执行文件,但我总是收到相同的错误消息:(


解决方案

我用猴子补丁解决了这个问题。只需将此代码粘贴到您在DASH之前导入的模块中,就应该可以开始了。在我的例子中,我有flask-compress==1.5.0,所以我只是硬编码了版本,但是您可能可以做一些更聪明的事情。

"""
Simple module that monkey patches pkg_resources.get_distribution used by dash
to determine the version of Flask-Compress which is not available with a
flask_compress.__version__ attribute. Known to work with dash==1.16.3 and
PyInstaller==3.6.
"""

import sys
from collections import namedtuple

import pkg_resources

IS_FROZEN = hasattr(sys, '_MEIPASS')

# backup true function
_true_get_distribution = pkg_resources.get_distribution
# create small placeholder for the dash call
# _flask_compress_version = parse_version(get_distribution("flask-compress").version)
_Dist = namedtuple('_Dist', ['version'])

def _get_distribution(dist):
    if IS_FROZEN and dist == 'flask-compress':
        return _Dist('1.5.0')
    else:
        return _true_get_distribution(dist)

# monkey patch the function so it can work once frozen and pkg_resources is of
# no help
pkg_resources.get_distribution = _get_distribution

相关文章