PyInstaller on 32-bit Linux - ImportError: The 

2019-03-31 03:09发布

问题:

I'm making a program using Python2.7 and Kivy1.9.2-dev, and trying to package it with PyInstaller-3.0 for different systems as a single executable.

The systems I'm trying to package it for are these:

  • 64-bit Linux Mint 17.3
  • 32-bit Linux Mint 17 (also tried while upgrading to 17.1 and 17.3)
  • 32-bit Windows XP SP3
  • Raspbian (Raspberry Pi)

On all these systems the program is working well when just run with Python, uncompiled. (so, all Kivy dependencies are fine, too).

However, out of the executables made with PyInstaller, only the one made on 64-bit Linux works as one file. The Windows and Raspbian executables mostly work (I'll write about it later), but the one made on 32-bit Linux still doesn't run. It gives the following error when run (I tried running it on both 32 and 64-bit Linux):

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "/media/Data/Programming/Python/installers/PyInstaller-3.0/PyInstaller/loader/pyimod03_importers.py", line 363, in load_module
    exec(bytecode, module.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 48, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/extern/__init__.py", line 60, in load_module
ImportError: The 'six' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.
pyi_rth_pkgres returned -1

Here's what I'm confused about:

  1. Inside my "/usr/local/lib/python2.7/dist-packages/" there is no "pkg_resources" folder, but the above indicates that it, apparently, is there... It even reads the files there successfully. Is it something that gets created just when the executable starts?
  2. I don't specifically use "six" for anything, before this error I didn't even know it existed.
  3. "Six" IS installed on my system, as confirmed by the Package Manager and by Pip. It's located in "/usr/local/lib/python2.7/dist-packages/". I guess PyInstaller can't find it for some reason (as, I believe, if everything is packaged correctly it doesn't have to be there on the system where the executable is run.)

So, my question is pretty typical, what may cause this problem for PyInstaller (just in case, I DID run the "setup.py install" for it), and how to get around/fix it?

Thanks!

PS: On a side note, I mentioned the problems with Windows and Raspbian executables. On Windows, the exe only runs when there is "zlib1.dll" present in the same folder (even if specifically packaged into the exe with PyInstaller, it doesn't work), and on Raspbian I only got the program working without using "--onefile" (with "--onefile", it seems PyInstaller doesn't package any Python binaries into the executable, like libpython.2.7.so, and maybe others too)

回答1:

Well, adding 'six' into hidden packages, as Clement suggested, didn't work, but started a sequence of trial-and-error that finally led to a solution. After the test with "hiddenimports" didn't work I tried just importing 'six' into my Python code. And the compiled executable no longer showed this error! However, it now said that the package named 'packaging' is required... Which I didn't have installed.

To put it short, starting from the initial problem, I did this:

  1. Installed 'packaging' using 'pip':

    sudo pip install packaging

  2. Added these imports into my main Python code:

    import six

    import packaging

    import packaging.version

    import packaging.specifiers

(all the imports added were trial-and-error, done until the PyInstaller-made executable finally worked).

Seems a bit hack-y, as making the executable for a 64-bit Linux didn't require any of these imports, but at least it now works, and the executable size is basically unaffected.



回答2:

For the following setup (anaconda):

PyInstaller: 3.2
Python: 3.5.2
Platform: Windows-10-10.0.10240-SP0
Numpy: 1.11.1

And the following mwe.py:

import numpy
print ("hello world")

I had to do the following to fix:

pip install packaging

Build with the following bat file (^ is the BAT line continuation):

pyinstaller --noconfirm ^
        --hidden-import six ^
        --hidden-import packaging ^
        --hidden-import packaging.version ^
        --hidden-import packaging.specifiers ^
        --hidden-import packaging.requirements ^
        mwe.py


回答3:

I had a similar problem. Try to add "six" as well as "kivy" to the hidden_packages in your spec file. If it doesn't work, make sure that setuptools is installed in its 19.2 version. It seemed to be the problem for me on Windows. Hope it helps.