Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyinstaller executable cannot find 'flask-compress' distribution that is included

Here is my system info:

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

I've been trying to generate a Python (PyQt) executable using Pyinstaller to be used in an app. However, when I package the executable and run it, it will throw this:

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

This dependency already exists in my virtual environment and I have tried specifying the path to the site packages directory and the flask_compress import like this:

pyinstaller --paths C:\Users\alan9\PycharmProjects\PracticumProject\venv\Lib\site-packages --hidden-import=flask_compress main.py

Note: I have tried to create the executable for this application using different python versions, with different pyinstaller flags (onefile, windowed, onedir), on different computers with Windows 7/10, on a clean copy of a Windows 10 VM, and with fbs but I always receive the same error message:(

like image 298
alan balderas Avatar asked Jan 19 '26 00:01

alan balderas


1 Answers

I solved this problem with monkey patching. Just paste this code in a module that you import before dash and you should be good to go. In my case I had flask-compress==1.5.0 so I just hardcoded the version but you could probably do something more clever.

"""
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
like image 50
gdm Avatar answered Jan 23 '26 21:01

gdm