Gray
Gray

Reputation: 29

openpyxl not working as EXE

Error message from running my exe:

ModuleNotFoundError: No module named 'openpyxl'

testHi.py

#simple test to see if openpyxl module works
import openpyxl

print ("hi")
input()

hook-openpyxl.py

# taken from pyinstaller dev team, store in same dir as testHi.py 
from PyInstaller.utils.hooks import collect_data_files

datas = collect_data_files('openpyxl')

cmd line input:

pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. hiTest.py

I run the the hiTest and get the error above.

I have looked everywhere for this solution. Can anyone tell me what I am doing wrong.

Upvotes: 2

Views: 6291

Answers (5)

Vi5iON
Vi5iON

Reputation: 21

@Kallisto Your answer worked. And command line input of @Gary also worked. Place the file "hook-openpyxl.py" in the folder where your spec file will be created and then use the command line input.

enter image description here

This occurs whenever the hook files for the module we are trying to integrate is missing in "\Lib\site-packages\PyInstaller\hooks".

enter image description here

You can see that there is hook-openpyxl.py between hook-numpy and hook-packaging.

After running the command line input procced with general steps of pyinstaller.

enter image description here

Upvotes: 0

Kallisto
Kallisto

Reputation: 11

You was quite close. :-)

I fixed the problem by modifying the "hook-openpyxl.py" file. The command collect_data_files('openpyxl') actually returns an empty list. But there is another command collect_submodules which seems to do what we want. So my "hook-openpyxl.py" file looks like this.

from PyInstaller.utils.hooks import collect_submodules

hiddenimports = collect_submodules('openpyxl')

I placed the "hook-openpyxl.py" file in the same directory like my spec file. In the spec file I set to location of the new hook file

a = Analysis(
    ...
    ...
    hookspath=['.'],
    ...
    ...
...

I guess, you will have the same result with your command line parameter

pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. hiTest.py

My environment

  • Python=3.8
  • openpyxl=3.0.10
  • PyInstaller=4.8

Upvotes: 0

ahmed taiko
ahmed taiko

Reputation: 31

use --hiddenimport openpyxl

long with the previous solutions, it worked for me, I was able to enforce the import of openpyxl.

Upvotes: 0

Matt Binford
Matt Binford

Reputation: 720

I was able to get this working using auto-py-to-exe (which uses pyinstaller) by including the following folders/files from my python library into the same folder that I run pyinstaller from:

  • jdcal.py
  • openpyxl (folder)
  • et_xmlfile (folder)

pyinstaller command:

    pyinstaller -y -F "[directory]/myscript.py"



Notes on Library Location:

Windows library location for me was: C:\users[username]\AppData\Local\Programs\Python\Python37-32\Lib

The packages were in the "site_packages" folder

Upvotes: 0

Takehomasak
Takehomasak

Reputation: 31

I fixed my issue by installing it through Pip, rather than install the package through Pycharm, and Pyinstaller was able to find the package.

I got the idea from looking through the text in the command prompt and saw it was loading modules that I had installed via Pip and not through Pycharm.

Upvotes: 3

Related Questions