Search code examples
pythonmodulepackagepyinstallerhook

Problem with PyInstaller and local packages


My project is structured as follows:

  • example3/
    • window/
      • __init__.py
      • ...
    • __init__.py
    • __main__.py
  • setup.py
  • example.py

in the main file:

import sys
from PySide2.QtWidgets import QApplication
import window

def main():
    app = QApplication(sys.argv)
    main_window = window.Main()
    main_window.show()
    app.exec_()

if __name__ == '__main__':
    main()

The build script example.py for PyInstaller:

from example3.__main__ import main

if __name__ == '__main__':
    main()

How do I get PyInstaller to recognize my project's packages or the modules in them? PyInstaller only sees the packages installed using pip. I had read something about hook files but didn't really understand how to do it.

I created the executable with the command:

> pyinstaller example.py --onefile

When I try to run the application I get the error message:

> ./dist/example
Traceback (most recent call last):
  File "example.py", line 3, in <module>
  File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
  File "example3/__main__.py", line 7, in <module>
  File "/tmp/embedded.xpuoquuw.zip/shibokensupport/__feature__.py", line 146, in _import
ModuleNotFoundError: No module named 'window'
[127129] Failed to execute script 'example' due to unhandled exception!

Solution

  • The problem could be solved by telling PyInstaller the search path for my project's modules and packages:

    pyinstaller --onefile example.py --paths ./example3/