简体   繁体   中英

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?

Try:

from tools import db
from tools import file_utils

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM