Code bundled with PyInstaller cannot import a locally installed vtk module: ModuleNotFoundError

Viewed 1451

I tried to apply pyinstaller to the simple file vtest.py

import vtk

print('Hello World')

on windows. I can run the command

py vtest.py

without issue. But when I try to generate an exe with the command

pyinstaller vtest.py --onefile

I get the error when I try to run the executable

Traceback (most recent call last):
  File "vtest.py", line 1, in <module>
  File "c:\users\djame\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "vtk.py", line 30, in <module>
  File "importlib\__init__.py", line 127, in import_module
ModuleNotFoundError: No module named 'vtkmodules'
[11448] Failed to execute script vtest

I tried changing the hook file as outlined here but no luck.

1 Answers

Pyinstaller can't detect this import so it doesn't attach it to the bundle. This happens due to less straightforward imports in the libs.

There are more than one solution:

  • Anywhere in your code add import statement for every missing module. As adding new module might lead to new missing modules it imports, you might need several iterations.

  • Add the same list of modules to hiddenimports argument in spec file or console command. This process might be iterative as well.

Another reason for missing modules is that PyInstaller treats them as system ones, because they are stored in dirs like /usr/lib. You can check if it is your case by importing the module and printing its .__file__.

Related