I have a main script that import the module from another script (sub_script.py) using importlib. I also pass the argument to the other script:
import importlib
parser = argparse.ArgumentParser(add_help=False)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-option1', action = "store_true")
args = parser.parse_known_args()
if args[0].option1:
function = importlib.import_module('sub_script')
function.main(namespace = args[1])
While this code runs fine by itself (using Python main_script.py), it returns the following error message after I complied it with Pyinstaller:
Traceback (most recent call last):
File "<string>", line 33, in <module>
ImportError: No module named sub_script
main_script returned -1
I tried to:
1) add a __init__.py
under my folder
or
2) move sub_script.py to a sub_folder with __init__.py
but either works.
I also tried to complied it under Ubuntu, but got the same message.
However, it complied and ran fine if I just using import:
import sub_script
Any ideas? Thanks!
pyinstaller can't automatically package a module that is imported dynamically. If you really need to use importlib to import the module, then you need to tell pyinstaller. You can use the --hidden-import option for this:
--hidden-import MODULENAME, --hiddenimport MODULENAME
Name an import not visible in the code of the script(s). This option can be used multiple times.
See PyInstaller Docs for more detail