Pyinstaller 可执行文件找不到包含的“flask-compress”发行版

ala*_*ras 3 python pyinstaller

这是我的系统信息:

123 INFO: PyInstaller: 4.0
123 INFO: Python: 3.5.4
124 INFO: Platform: Windows-10-10.0.18362-SP0
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用 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
Run Code Online (Sandbox Code Playgroud)

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

pyinstaller --paths C:\Users\alan9\PycharmProjects\PracticumProject\venv\Lib\site-packages --hidden-import=flask_compress main.py
Run Code Online (Sandbox Code Playgroud)

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

小智 5

我用猴子补丁解决了这个问题。只需将此代码粘贴到您在破折号之前导入的模块中,您就可以开始使用了。在我的情况下,我有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
Run Code Online (Sandbox Code Playgroud)