PyInstaller, spec file, ImportError: No module named 'blah'

This error can ocurre when you have dynamic imports in your code. In that case, pyinstaller don't include those packages in exe file. In that case you can:

  1. Add unused import of those packages in your code
  2. Tell pyinstaller to include it

One file option does not change anything in running your code. If you create --onefile exe all files created by pyinstaller are packed to exe file, and unpacked to local temp every time you run exe.


just gonna add my 2 cents because I encountered the same problem today - 6 years later :D

For Windows:

1) cmd => rightclick => with admin rights
2) Enter in cmd: "pip install pyinstaller"
3) navigate in cmd to the folder of "yourMain.py"
4) Enter in cmd: "pyinstaller --onefile --windowed yourMain.py"

5) If you import other scripts / data in "yourMain.py": 
Manually enter the folder "dist" (gets created - where "yourMain.exe" should be by now), 
and copy your scripts or folder structure there

(e.g. /assets/sounds; /assets/graphics; /scripts; anotherscript.py )

Then I was able to run the exe by double clicking.

Turned out to be pretty easy. What did the trick for me was the "--onefile" and adding my other files to the "dist" folder.

The "--windowed" is just so the python command window won't pop up when you start the exe.


The problem is that 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 add the following in a = Analysis(...):

hiddenimports=["mysql"],

This should be the result:

a = Analysis([os.path.join(HOMEPATH,'support/_mountzlib.py'), os.path.join(HOMEPATH,'support/useUnicode.py'), 'icinga.py'],
         pathex=['/home/user/projects/icinga_python/releases/v2.1'], hiddenimports=["mysql"],)

After that run pyinstaller with the spec file as an argument.