33

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!

gurnben
  • 331
  • 1
  • 3
  • 5
  • Update: I have repackaged and it now actually begins to execute, then throws an import error! Progress at least! Error is as follows: ImportError: The 'packaging' package is required – gurnben Jun 14 '16 at 15:49
  • Ok, i believe the issue at hand has been solved, now I am having an issue with file references... – gurnben Jun 14 '16 at 15:57
  • I met this too, how did you solve this? – Readon Shaw Jul 01 '16 at 08:21
  • It was a while back but I believe it was some data file dependencies. I had to modify the .spec file to include those files and folders – gurnben Jul 02 '16 at 14:00

9 Answers9

40

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
crifan
  • 12,947
  • 1
  • 71
  • 56
33

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

pyinstaller --hidden-import=pkg_resources.py2_warn example.py
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Anirudh
  • 441
  • 4
  • 3
10

Adding the line:

import pkg_resources.py2_warn

to my code helped.

101
  • 8,514
  • 6
  • 43
  • 69
Vikash Kumar
  • 111
  • 1
  • 2
  • 1
    How will this resolve the problem stated in the question? There's no such thing as a 'hidden module' in Python, so perhaps you could elaborate on what you mean by *import this ... as hidden Module*? – Martijn Pieters Jan 21 '20 at 12:44
9
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
Ankur
  • 457
  • 6
  • 21
7

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.

G. Juwot
  • 191
  • 1
  • 7
6

I had the same problem. Was solved by reinstalling the pyinstaller with the developer's branch version, following the directions in: https://github.com/pyinstaller/pyinstaller/issues/2137

The steps are:

  • Remove PyInstaller pip uninstall pyinstaller.
  • Download the zip from github.
  • Unzip file.
  • Make sure you are in the directory with "setup.py" and run: python setup.py install
sas
  • 103
  • 1
  • 11
4

I extend @vikash-kumar answer.
pyinstaller tool has command line parameter --hidden-import.

It resolves the topic problem for me.

More information:
https://web.archive.org/web/20200601130821/https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html#listing-hidden-imports.

3

this is because he did not copy a dependency.I solved it like this.

  • pyinstaller my_program.py

this creates a my_program.spec. it is a base configuration file.

open it with any text editor. search for

hiddenimports=[]

edit to.

hiddenimports=["pkg_resources.py2_warn"]

now let's call pyinstaller passing our configured file instead of our program

  • pyinstaller my_program.spec
0

Giving correct path to site_packages solved the issues for me. Initially I was giving site-packages path from my local user environment. Even though with multiple --hidden-import the exe was kept on failing with error "Failed to execute script 'pyi_rth_pkgres' due to unhandled exception!" and was looking for jaraco package. Then changed to global site-packages path, build the package again and it worked. Following is the command for reference

pyinstaller --exclude-module PyQt5 -p dataLoader_cy.py --hidden-import dataLoader_cy -p C:\ProgramData\Anaconda3\Lib\site-packages --hidden-import=torchvision --hidden-import=numpy --hidden-import=pkg_resources.py2_warn --hidden-import=packaging --hidden-import=pkg_resources --hidden-import=packaging.version --hidden-import=packaging.specifiers --hidden-import=packaging.requirements --hidden-import=pkg_resources._vendor.jaraco --onefile main.py 
TheTechRobo the Nerd
  • 1,249
  • 15
  • 28
Viji
  • 1
  • 1