Why Do I get an ImportError when building a .exe with pyinstaller?
Asked Answered
W

4

11

I just created a small GUI program that compiles and works fine in IPython, but when I try to export it to a .exe using pyinstaller it gives me an import error. I'm sure it's sklearn because when I comment out the sklearn imports my file open fine when I build it.

C:\Users\Chris\Anaconda>C:/Users/Chris/Anaconda/dist/Room_Test.exe
WARNING: file already exists but should not:                            C:\Users\Chris\AppData\Local\Temp\_MEI100402\Include\pyconfig.h
Traceback (most recent call last):
File "<string>", line 9, in <module>
File "C:\Users\Chris\Anaconda\Lib\site-    packages\PyInstaller\loader\pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
  File "C:\Users\Chris\Anaconda\build\Room_Test\out00-    PYZ.pyz\sklearn.neighbors", line 6, in <module>
  File "C:\Users\Chris\Anaconda\Lib\site-    packages\PyInstaller\loader\pyi_importers.py", line 409, in load_module
    module = imp.load_module(fullname, fp, filename, self._c_ext_tuple)
  File "dist_metrics.pxd", line 48, in init sklearn.neighbors.ball_tree     (sklearn\neighbors\ball_tree.c:35726)
  File "C:\Users\Chris\Anaconda\Lib\site-    packages\PyInstaller\loader\pyi_importers.py", line 409, in load_module
    module = imp.load_module(fullname, fp, filename, self._c_ext_tuple)
  File "dist_metrics.pyx", line 52, in init sklearn.neighbors.dist_metrics     (sklearn\neighbors\dist_metrics.c:25494)
ImportError: No module named typedefs
Welt answered 2/8, 2015 at 18:1 Comment(1)
This is not python, rather Pyrex C extension. I suppose pyinstaller can't deal with that automatically.Dowie
D
14

You can still use pyinstaller by adding the following to your command:

--hidden-import sklearn.neighbors.typedefs

or by adding the following to your .spec file:

hiddenimports=['cython', 'sklearn', 'sklearn.neighbors.typedefs']
Donothingism answered 25/8, 2016 at 12:35 Comment(2)
I ran into the same problem and when I included --hidden-import sklearn.neighbors.typedefs I got the following error: RecursionError: maximum recursion depth exceededFact
@PankajJoshi - you can try setting the recursion limit in the spec file: #38978429Alderete
G
9

its better to use spec file to import another hidden libraries maybe cause the problem. I list all Sklearn libraries and add to spec file as a hiddenimports like this:

  # -*- mode: python -*-

block_cipher = None


a = Analysis(['MyPythonApplication.py'],
             pathex=['..\\ApplicationFolder'],
             binaries=[],
             datas=[],
             hiddenimports=['cython', 'sklearn', 'sklearn.ensemble', 'sklearn.neighbors.typedefs', 'sklearn.neighbors.quad_tree', 'sklearn.tree._utils'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='ExeFileName',             
          debug=False,
          strip=False,
          upx=False,
          console=False )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='ApplicationName')
Gantry answered 15/1, 2018 at 13:44 Comment(1)
This worked great for me! Just had to add 'sklearn.pipeline' in my case also.Genovevagenre
W
3

Solved it myself! Ended up using py2exe. Much easier to import modules even though bundling to one .exe is not yet supported on x64 systems. But worked for my purposes. Just added the following line to the "includes":

sklearn.neighbors.typedefs
Welt answered 3/8, 2015 at 20:10 Comment(0)
C
1

Find this dir "E:\Anaconda3\Lib\site-packages\PyInstaller\hooks". Add a file "hook-pandas.py",wrie this content to this file:

"""
Hook for pandas. 
Suport for pyinstaller error : No module named ‘pandas._libs.tslibs.timedeltas
"""
hiddenimports=[
    #all your previous hidden imports
    'pandas', 'pandas._libs.tslibs.timedeltas',
    'sklearn.neighbors.typedefs'
]

Then use this command:

pyinstaller -F myfile.py --hidden-import sklearn.neighbors.typedefs

Then it will be OK!

Childbed answered 23/3, 2018 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.