Exe for my python script does not run because it does not recognize the module PIL

I have completed a python project and want to make an exe for easy acces. After creating said exe, I tried to run it but an error states “no module named ‘PIL'”. Everything works perfect when executing script from the interpreter.

I have tried:

  • Updating PIL and Image

  • Changing PIL to pil as suggested in another question (does not recognize module)

  • Changing import PIL to import Image (also not recognized)

If more details are needed I shall provide them.

Answer

You will need to include the path of the libraries in your setup. Example (from docs):

block_cipher = None
a = Analysis(['minimal.py'],
     pathex=['/Developer/PItests/minimal'],
     binaries=None,
     datas=None,
     hiddenimports=[],
     hookspath=None,
     runtime_hooks=None,
     excludes=None,
     cipher=block_cipher)

You need to include the pathes to the modules in the pathex parameter. If this still does not solve your issue, then the pyinstaller is not recognizing your import. You can set it explicitly in the hiddenimports parameter.