No module named 'matplotlib' when using PyInstaller

1 493 vues
Accéder directement au premier message non lu

Oliver ulfik

non lue,
13 mai 2022, 19:36:0313/05/2022
à Kivy users support
I have a kivy app running in a venv with different libraries like matplotlib. While following the official docs for packaging an app I got to the point where I can build my exe but when I run it I get a 'no module found'. Matplotlib however is correctly installed in the virtual env. Here is the error messsage:

(speech-env) C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer>spyzer.exe
[INFO   ] [Logger      ] Record log in C:\Users\oliver\.kivy\logs\kivy_22-05-14_39.txt
[INFO   ] [Kivy        ] v2.1.0
[INFO   ] [Kivy        ] Installed at "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\kivy\__init__.pyc"
[INFO   ] [Python      ] v3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\spyzer.exe"
[INFO   ] [Logger      ] Purge log fired. Processing...
[INFO   ] [Logger      ] Purge finished!
[INFO   ] [Factory     ] 189 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [AudioGstplayer] Using Gstreamer 1.18.5.0
[INFO   ] [Audio       ] Providers: audio_gstplayer, audio_sdl2 (audio_ffpyplayer ignored)
 Traceback (most recent call last):
   File "kivy\lang\parser.py", line 553, in execute_directives
   File "<frozen importlib._bootstrap>", line 1109, in __import__
   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 790, in exec_module
   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
   File "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\audio_analysis.py", line 12, in <module>
     import matplotlib.pyplot as plt
 ModuleNotFoundError: No module named 'matplotlib'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "main.py", line 13, in <module>
     Builder.load_file('layouts/spyzer.kv')
   File "kivy\lang\builder.py", line 305, in load_file
   File "kivy\lang\builder.py", line 372, in load_string
   File "kivy\lang\parser.py", line 483, in __init__
   File "kivy\lang\parser.py", line 590, in parse
   File "kivy\lang\parser.py", line 556, in execute_directives
   File "<frozen importlib._bootstrap>", line 1109, in __import__
   File "<frozen importlib._bootstrap>", line 1027, in _gcd_import
   File "<frozen importlib._bootstrap>", line 961, in _sanity_check
 ValueError: Empty module name

[5064] Failed to execute script 'main' due to unhandled exception!

And here is my .spec file:

from kivy_deps import sdl2, glew
# -*- mode: python ; coding: utf-8 -*-


block_cipher = None

added_files = [( 'C:\\Users\\oliver\\Desktop\\app\\kivy\\kivy-speech\\layouts', 'layouts' )]

a = Analysis(
    ['C:\\Users\\oliver\\Desktop\\app\\kivy\\kivy-speech\\main.py'],
    pathex=[],
    binaries=[],
    datas = added_files,
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='spyzer',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    Tree('C:\\Users\\oliver\\Desktop\\app\\kivy\\kivy-speech\\'),
    a.binaries,
    a.zipfiles,
    a.datas,
    *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
    strip=False,
    upx=True,
    upx_exclude=[],
    name='spyzer',
)

Any help would be appreciated!

berk berk

non lue,
13 mai 2022, 21:04:5013/05/2022
à Kivy users support
https://www.youtube.com/watch?v=NEko7jWYKiE&t=517s I suggestion Codemy about it.
Also when you show this error , are you tried again pip install matplotlib?
I used matplotlib to convert .exe and did work  but i can't remember now. passed long months.

14 Mayıs 2022 Cumartesi tarihinde saat 02:36:03 UTC+3 itibarıyla use...@gmail.com şunları yazdı:

Elliot Garbus

non lue,
13 mai 2022, 22:38:5013/05/2022
à kivy-...@googlegroups.com

Try adding the missing library to the hiddenimports list in the spec file; or make sure it is imported in your python code.  The error suggests that for some reason, pyinstaller is not seeing matplotlib, so it is not being pulled into the bundle.

 

Below is my “typical” windows spec file.  The Collect section has a change – so the source files are not pulled into the bundle.  Also notice the datas section is used to add the data directories to the bundle.

 

# -*- mode: python -*-

from kivy_deps import sdl2, glew

block_cipher =
None
app_name = 'App Name Here'
win_icon = '../Images/my_icon.ico'

a = Analysis(['../main.py'],
             pathex=[],
             binaries=[],
             datas=[(
'../*.kv', '.'),
                    (
'../Images/*.png', './Images')],
             hiddenimports=[
'win32timezone'],
             hookspath=[],

             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=
False,
             win_private_assemblies=
False,
             cipher=block_cipher,
             noarchive=
False)

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=
True,
          name=app_name,
          debug=
False,
          bootloader_ignore_signals=
False,
          strip=
False,
          upx=
False,
          console=
False,
          icon=win_icon)
coll = COLLECT(exe,

               a.binaries,
              a.zipfiles,
               a.datas,
               *[Tree(p)
for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=
False,
               upx=
False,
               name=app_name)

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/7e40dce0-70b9-42ca-9f28-1ff99217108fn%40googlegroups.com.

 

Oliver ulfik

non lue,
14 mai 2022, 06:00:3414/05/2022
à Kivy users support
Thanks for the reply! So I tried the hiddenimports thing with matplotlib and now pyInstaller is complaining about a different library called librosa. Does not make sense because the PyInstaller doc states that you use --hidden-import  for imports not visible in the code of the script(s) but the matplotlib import is visible in one of my .py files.

Oliver ulfik

non lue,
14 mai 2022, 07:03:5414/05/2022
à Kivy users support
I put numpy, librosa and matplotlib into hidden_imports. Now I get a different error message. Seems to be a problem with the path. But the registry file is in the correct folder:

(speech-env) C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer>spyzer.exe
[INFO   ] [Logger      ] Record log in C:\Users\oliver\.kivy\logs\kivy_22-05-14_60.txt

[INFO   ] [Kivy        ] v2.1.0
[INFO   ] [Kivy        ] Installed at "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\kivy\__init__.pyc"
[INFO   ] [Python      ] v3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\spyzer.exe"
[INFO   ] [Logger      ] Purge log fired. Processing...
[INFO   ] [Logger      ] Purge finished!
[INFO   ] [Factory     ] 189 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [AudioGstplayer] Using Gstreamer 1.18.5.0
[INFO   ] [Audio       ] Providers: audio_gstplayer, audio_sdl2 (audio_ffpyplayer ignored)
 Traceback (most recent call last):
   File "main.py", line 10, in <module>

     Builder.load_file('layouts/spyzer.kv')
   File "kivy\lang\builder.py", line 305, in load_file
   File "kivy\lang\builder.py", line 372, in load_string
   File "kivy\lang\parser.py", line 483, in __init__
   File "kivy\lang\parser.py", line 590, in parse
   File "kivy\lang\parser.py", line 553, in execute_directives
   File "<frozen importlib._bootstrap>", line 1109, in __import__
   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 790, in exec_module
   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
   File "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\audio_analysis.py", line 13, in <module>
     import librosa

   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
   File "librosa\__init__.py", line 209, in <module>
     self.wins.append(win)

   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
   File "librosa\core\__init__.py", line 5, in <module>
     Modules are classes that can be loaded when a Kivy application is starting. The

   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
   File "librosa\core\convert.py", line 7, in <module>

   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
   File "librosa\core\notation.py", line 8, in <module>

   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
   File "librosa\util\__init__.py", line 78, in <module>


   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
   File "librosa\util\files.py", line 32, in <module>
   File "pooch\core.py", line 648, in load_registry
 FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\oliver\\Desktop\\app\\kivy\\spyzer-exe\\dist\\spyzer\\librosa\\util\\example_data\\registry.txt'
[20332] Failed to execute script 'main' due to unhandled exception!

Elliot Garbus

non lue,
14 mai 2022, 09:20:4414/05/2022
à kivy-...@googlegroups.com

If you look inside the dist directory that pyinstaller created, is the ‘missing’ file in the correct place?

This video will not help with your specific issue – but it helped me understand how pyinstaller works.

https://youtu.be/tOTLqUQC-k0

Oliver ulfik

non lue,
14 mai 2022, 16:16:2014/05/2022
à Kivy users support
Actually the file is not in the correct path. It should be "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\speech-env\Lib\site-packages\librosa\util\example_data\registry.txt " and not "C:\\Users\\oliver\\Desktop\\app\\kivy\\spyzer-exe\\dist\\spyzer\\librosa\\util\\example_data\\registry.txt". I will watch the video, maybe it can help me to grasp some topics.

Elliot Garbus

non lue,
14 mai 2022, 16:32:0714/05/2022
à kivy-...@googlegroups.com

You could add the file, registry.txt, to the datas section.  Remember to specify the full source file and the dst directory.  See: https://pyinstaller.org/en/stable/spec-files.html#adding-data-files

Oliver ulfik

non lue,
15 mai 2022, 06:53:0015/05/2022
à Kivy users support
I added the file to the C:\\Users\\oliver\\Desktop\\app\\kivy\\spyzer-exe\\dist\\spyzer\\librosa\\util\\example_data\\  directory. Now I get an ModuleNotFoundError, despite putting the name of the library in hidden_imports. Can you maybe have a look and try to build an exe yourself? I made a public repository (https://github.com/oulfik/spyzer/tree/develop).

C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer>spyzer.exe
[INFO   ] [Logger      ] Record log in C:\Users\oliver\.kivy\logs\kivy_22-05-15_2.txt

[INFO   ] [Kivy        ] v2.1.0
[INFO   ] [Kivy        ] Installed at "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\kivy\__init__.pyc"
[INFO   ] [Python      ] v3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\spyzer.exe"
[INFO   ] [Logger      ] Purge log fired. Processing...
[INFO   ] [Logger      ] Purge finished!
[INFO   ] [Factory     ] 189 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [AudioGstplayer] Using Gstreamer 1.18.5.0
[INFO   ] [Audio       ] Providers: audio_gstplayer, audio_sdl2 (audio_ffpyplayer ignored)
 Traceback (most recent call last):
   File "kivy\lang\parser.py", line 553, in execute_directives
   File "<frozen importlib._bootstrap>", line 1109, in __import__
   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 790, in exec_module
   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
   File "C:\Users\oliver\Desktop\app\kivy\spyzer-exe\dist\spyzer\audio_analysis.py", line 14, in <module>
     import librosa.display
 ModuleNotFoundError: No module named 'librosa.display'


 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "main.py", line 10, in <module>
     Builder.load_file('layouts/spyzer.kv')
   File "kivy\lang\builder.py", line 305, in load_file
   File "kivy\lang\builder.py", line 372, in load_string
   File "kivy\lang\parser.py", line 483, in __init__
   File "kivy\lang\parser.py", line 590, in parse
   File "kivy\lang\parser.py", line 556, in execute_directives
   File "<frozen importlib._bootstrap>", line 1109, in __import__
   File "<frozen importlib._bootstrap>", line 1027, in _gcd_import
   File "<frozen importlib._bootstrap>", line 961, in _sanity_check
 ValueError: Empty module name
[11932] Failed to execute script 'main' due to unhandled exception!

Elliot Garbus

non lue,
15 mai 2022, 20:30:2515/05/2022
à kivy-...@googlegroups.com

You have a set of imports in kv.  Especially at the top of spyzer.kv

Pyinstaller does not know about imports in kv.

Add the python imports that are uniquely in  kv to main.py, or add them to hidden imports.

 

If I have time later tonight I’ll try a build.

Elliot Garbus

non lue,
15 mai 2022, 20:43:2815/05/2022
à kivy-...@googlegroups.com

Did a little more work…  It looks like librosa does not provide pyinstaller hooks.

It might be possible to just add some of the files to datas list in the spec file or you may need to create a hook file.

See: https://github.com/librosa/librosa/issues/538

And: https://pyinstaller.org/en/stable/hooks.html

Elliot Garbus

non lue,
16 mai 2022, 00:13:4916/05/2022
à kivy-...@googlegroups.com

Doing a little more searching it looks like:

You should do the following:

 

To your spec file Add:

from PyInstaller.utils.hooks import collect_data_files

 

then extend the datas=[] to include

collect_data_files('librosa')

 

https://pyinstaller.org/en/stable/hooks.html#PyInstaller.utils.hooks.collect_data_files

Oliver ulfik

non lue,
16 mai 2022, 07:52:5716/05/2022
à Kivy users support
Thank you for your research. As you stated I had to include some hooks. I also needed to include missing dll files for gstreamer and add hidden imports. Now it works! My new spec:

from kivy_deps import sdl2, glew
from PyInstaller.utils.hooks import collect_data_files

# -*- mode: python ; coding: utf-8 -*-


block_cipher = None

datas = collect_data_files('librosa')
datas += collect_data_files('vosk')
datas += collect_data_files('pyAudioAnalysis')


a = Analysis(
    ['C:\\Users\\oliver\\Desktop\\app\\kivy\\kivy-speech\\main.py'],
    pathex=[],
    binaries=[('C:\\Users\\oliver\\Desktop\\app\\kivy\\kivy-speech\\speech-env\\share\\gstreamer\\bin', '.')],
    datas=datas,
    hiddenimports=['matplotlib', 'librosa.display', 'vosk', 'pyAudioAnalysis.audioSegmentation', 'win32timezone'],

Elliot Garbus

non lue,
16 mai 2022, 09:28:2216/05/2022
à kivy-...@googlegroups.com
Glad to hear you got it working!

Sent from my iPhone

On May 16, 2022, at 4:53 AM, Oliver ulfik <use...@gmail.com> wrote:


Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message