'Modules not found after running pyinstaller
I apologize now for the long post.
I am working on converting a Hello World program from a Python file to a stand-alone, distributable executable file, using pyinstaller. I am using pyinstaller 3.6, on Windows 10 Enterprise. Python code was written using Spyder 4.0.1 (I am currently unable to update to 4.1.3).
I have created the HelloWorld.py file:
#! python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 12:27:20 2020
@author: DNICHOL3
"""
def main():
print("Hello World!")
if __name__ == "__main__":
main()
As well as a cli.py file in the same folder:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 09:07 2020
@author: DNICHOL3
"""
# import HelloWorld
from HelloWorld.__main__ import main
if __name__ == '__main__':
main()
I am running pyinstaller with the –onefile –debug imports –clean flags:
pyinstaller cli.py –onefile –debug imports --clean
I then run cli.exe from the dist folder, and get several screens of output (import xxx, # clear yyy, # restore zzz, # cleanup [2] aaa). Embedded in this output are the following lines:
HelloWorld not found in PYZ
Traceback (most recent call last):
File “cli.py”, line 10, in <module>
File “<frozen importlib.bootstrap>”, line 991, in _find_and_load
File “<frozen importlib.bootstrap>”, line 961, in _find_and_load_unlocked
File “<frozen importlib.bootstrap>”, line 219, in _call_with_frames_removed
File “<frozen importlib.bootstrap>”, line 991, in _find_and_load
File “<frozen importlib.bootstrap>”, line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named ‘HelloWorld’
[21600] Failed to execute script cli
Looking at warn-cli.txt shows that HelloWorld is indeed missing:
Missing module named ‘HelloWorld.__main__’ imported by C:\Users\dnichol3\python\Utilities\cli.py (top-level)
and cli.spec seems to show that HelloWorld is missing from PYZ:
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
Executing the generated cli.exe encounters the error message:
File “cli.py”, line 10, in <module>
ModuleNotFoundError: No module named ‘HelloWorld’
I have gone through the pyinstaller documentation (When Things Go Wrong). I have attempted to include HelloWorld.py into my pyinstaller command by using the –hidden_import flag, but that consistently ends with pyinstaller: error: unrecognized arguments: --hidden_import. And it doesn’t matter whether or not I use an equal sign between hidden_import and HelloWorld, nor does it matter whether or not I use the .py file extension.
I tried some other Stack Overflow suggestions (https://stackoverflow.com/a/46894037/12344177, https://stackoverflow.com/a/41869771/12344177, and https://stackoverflow.com/a/47337389/12344177 ) to no avail. Any assistance would be appreciated.
Solution 1:[1]
I think this is not a pyinstaller issue but belongs to your imports. Try this cli.py without the .__main__
This means you import main() from the HelloWorld.py.
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 30 09:07 2020
@author: DNICHOL3
"""
# import HelloWorld
from HelloWorld import main
if __name__ == '__main__':
main()
Solution 2:[2]
Hi I hope this is not too late. I have struggled for days on this topic as well. Skimmed through the documentation and found this section.
My problem is slightly different, my folder structure looks like this:
my_package/
??? setup.py
??? my_package/
? ??? file_1.py
? ??? file_2.py
Also, I've did a pip install .
where setup.py
in an environment where pyinstaller
was installed.
In file_1.py
I have the following equivalent:
from my_package.file_2 import some_function
def foo():
print('hello world')
some_function()
foo()
I kept getting into the ModuleNotFoundError
problem when I run my .exe file. The solution from the above mentioned section was, in the folder where file_1.py
is:
pyinstaller file_1.py -F --collect-all my_package
and this worked. Can't say I truly understand everything in the docs, but well... it worked, and hope it does for you too... if this is not too late :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Boon |