Pyinstaller "Failed to execute script pyi_rth_pkgres" and missing packages

54,620

Solution 1

same problem here:

  • OS: Win10
  • Python: 3.7
    • pyinstaller installed by pip install pyinstaller

fix by (same solution with above, by no need download):

pip uninstall pyinstaller
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip

Solution 2

Extending Vikash Kumar's answer, build the application by adding the --hidden-import argument to the command.

For example, running the command given below worked for me.

"pyinstaller --hidden-import=pkg_resources.py2_warn example.py"

update: added missing "="

Solution 3

Adding the line:

import pkg_resources.py2_warn

to my code helped.

Solution 4

pyinstaller --hidden-import=pkg_resources.py2_warn --onefile example.py

you can use this really it works no need to install or uninstall anything just use this it will create one file only , below code will not create the black window also if you are creating a Tkinter application mainly

 pyinstaller --hidden-import=pkg_resources.py2_warn --onefile --noconsole example.py

Solution 5

To iterate further on the best hidden answer from elton fernando.

# -*- mode: python ; coding: utf-8 -*-
from kivy_deps import sdl2, glew
import pkg_resources.py2_warn # before you add it to hiddenimports, import it here.
import dependency_injector.errors
import six
block_cipher = None


a = Analysis(['...'],
             pathex=['..'],
             binaries=[],
             datas=[],
             hiddenimports=['pkg_resources.py2_warn', 'dependency_injector.errors', 'six'], # This is the line you need
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='...',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
                Tree('./'),
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=True,
               upx_exclude=[],
               name='...')

Whenever you encounter an import error, just import them at the top and add them as a string to hiddenimports in the array.

Share:
54,620
Admin
Author by

Admin

Updated on July 13, 2020

Comments

  • Admin
    Admin almost 4 years

    This is my first time posting a question here as most of my questions have already been answered by someone else! I am working on a GUI application in python and am attempting to use pyinstaller to package it into a single folder and .exe for easier portability. Currently, I am using windows 10 and anaconda3 to manage my python packages. The application relies on tkinter, pillow, numpy, scikit-learn, opencv, ffmpeg, and matplotlib. The application is formatted with a main GUI.py file that creates objects of a number of other files (many of which are stored in a subfolder as this GUI is replacing a command line utility that served the same purpose). The issue I am running into (as you can see in the title) is that the .exe is throwing the error block:

    Traceback (most recent call last): File "site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py", line 11, in File "c:\users\gurnben\anaconda3\envs\opencv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module exec(bytecode, module.dict) File "site-packages\setuptools-20.7.0-py3.5.egg\pkg_resources__init__.py", line 68, in File "site-packages\setuptools-20.7.0-py3.5.egg\pkg_resources\extern__init__.py", line 60, in load_module ImportError: The 'packaging' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution. Failed to execute script pyi_rth_pkgres

    When I look at the warn.txt it gives a massive list of missing packages including parts of some packages that are actually in the single folder package.
    I have, however, successfully gotten it to recognize the dll files from opencv and it is not listed among the missing (nor is ffmpeg however I did not see any ffmpeg files in the folder). I had to pass in a custom path to get it to include the opencv files as they are not in anaconda at this time.

    Any hints or ideas for next troubleshooting steps? I am overly greatful for all of the help you an offer and I can upload any code, files, etc. that would help you diagnose the issue. In the meantime I will continue searching for a solution myself!