No module named when using PyInstaller
Had a similar problem with no module named FileDialog
. Discovered that with version 3.2, I could use
pyinstaller --hidden-import FileDialog ...
instead of modifying my main script.
Pyinstaller won't see second level imports. So if you import module A, pyinstaller sees this. But any additional module that is imported in A will not be seen.
There is no need to change anything in your python scripts. You can directly add the missing imports to the spec file. Just change the following line:
hiddenimports=[],
to
hiddenimports=["Tkinter", "FileDialog"],
The problem were some runtime dependencies of matplotlib. So the compiling was fine while running the program threw some errors. Because the terminal closed itself immediately I didn't realize that. After redirecting stdout
and stderr
to a file I could see that I missed the libraries Tkinter
and FileDialog
. Adding two import
s at the top of the main solved this problem.
If you are getting ModuleNotFoundError: No module named ...
errors and you:
- call PyInstaller from a directory other than your main script
- use relative imports in your script
then your executable can have trouble finding the relative imports.
This can be fixed by:
-
calling PyInstaller from the same directory as your main script
-
OR removing any
__init__.py
files (empty__init__.py
files are not required in Python 3.3+) -
OR using PyInstaller's
paths
flag to specify a path to search for imports. E.g. if you are calling PyInstaller from a parent folder to your main script, and your script lives insubfolder
, then call PyInstaller as such:pyinstaller --paths=subfolder subfolder/script.py
.