Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing local modules that are dot referenced with PyInstaller

I have a python script I'm working on that I am packaging into a one file executable with pyinstaller.

Within the script, when it is uncompiled, I am referencing a set of tools that live in a folder next to the main script, so something like this:

\parent
    ----->\tools\
       ------>db.py
       ------>file_utils.py
main.py

I've omitted the init, but it's there as well. Within my script, I'm importing these files like

import tools.db
import tools.file_utils

and all of this works fine. When I package it as an executable with PyInstaller, I am getting a module not found error for tools. I've tried adding the absolute path to tools, adding tools and the relevant files to hidden imports, and all of this, but I have a feeling it's the way I'm calling them in the script.

I suppose all those helper scripts could just be in the parent directory next to the script, or I could add tools to the sys.path and just import db and fileutils directly, but that seems a bit janky and/or cluttered. Anything obvious I am missing?

like image 572
Michael McIntyre Avatar asked Nov 07 '22 19:11

Michael McIntyre


1 Answers

So, after flattening the entirety of my directory structure in a tmp folder to write the executable, and changing my spec file to not compile to a different directory (as well as adding the parent directory to the -p switch) and remembering to delete a reference in a different py file to another module that no longer exists, I got this all working.

I think I'll piece back the parts so I can make this work much easier in the future and try and identify which part of the process was giving my packaging ImportErrors, but it was a useful exercise in picking apart the pieces of PyInstaller a bit and learning more about hooks and imports and module/package level imports.

like image 62
Michael McIntyre Avatar answered Nov 14 '22 20:11

Michael McIntyre