Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyInstaller failing to include some modules from C:\Python27\Lib

I've been repeatedly making good PyInstaller executables of a Tkinter utility program, and suddenly this morning the resulting executable fails with a "can't import" error for modules in C:\Python27\Lib, such as "timeit" and "bisect".

The script runs fine on its own. Only the executable has problems.

Any ideas what could have changed to cause this behavior? Or how to force a fix?

[EDIT] Here's the specific error reported by the executable:

Traceback (most recent call last):
  File "<string>", line 35, in <module>
  File "../..\utils\InterpolatedArray.py", line 12, in <module>
    import bisect
ImportError: No module named bisect

When I comment-out the use of this module (to bypass the import of bisect), it next fails on an import of timeit. None of these errors occur when running the script itself.

[EDIT2] Pyinstaller creates the directories it needs (./build and ./dist), and has no permission problems. The pyinstaller build completes without error.

[EDIT3] Here's the build command I'm using:

pyinstaller -F MyMainModule.py
like image 263
BobC Avatar asked Feb 28 '14 19:02

BobC


People also ask

Does PyInstaller include modules?

To find out, PyInstaller finds all the import statements in your script. It finds the imported modules and looks in them for import statements, and so on recursively, until it has a complete list of modules your script may use.

How do I fix PyInstaller not working?

To Solve 'pyinstaller' is not recognized as an internal or external command operable program or batch file Error You just need to add python script in your path to solve this error.

Does PyInstaller work with packages?

PyInstaller is for installing a "Python application and all its dependencies into a single package", not for installing a Python package. You need to use something else, like the distutils package, to do that.

How do I add hidden imports to PyInstaller?

The simpler solution is to use --hidden-import=modulename along with the PyInstaller script. It will add modulename as import statement silently. Hooks are better if you want to specify which import needs what additional modules. --hidden-import is simpler as a one-shot or for debugging.


2 Answers

Found a fix, if not the cause. Here's my updated build line:

pyinstaller --hidden-import=timeit --hidden-import=bisect -F MyMainModule.py

Still not sure why PyInstaller suddenly forgot how to find these two modules (and only these two modules) among over 20 other modules correctly included in the build.

like image 136
BobC Avatar answered Oct 12 '22 21:10

BobC


I encounter similar issues while packaging a Python script imported openpyxl. Here is my solution.

Step 1: install the python module, openpyxl

$ wine python.exe Scripts/pip.exe install openpyxl

Step 2: add the openpyxl path

Append the openpyxl path (~/.wine/drive_c/Python27/Lib/site-packages) to pathex in the Analysis object in the application spec file (e.g.,ProcessSpreadsheet.spec).

a = Analysis(['ProcessSpreadsheet.py'],
             pathex=['C:\\Python27\\Scripts', '~/.wine/drive_c/Python27/Lib/site-packages'],
             binaries=None,
             datas=None,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

Step 3: rebuild

$ wine pyinstaller.exe ProcessSpreadsheet.spec

Refer to here for the detailed description.

like image 22
SparkAndShine Avatar answered Oct 12 '22 22:10

SparkAndShine