Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 835
Chris Avatar asked Aug 02 '15 18:08

Chris


People also ask

How do I fix the ImportError in Python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

Why is the .exe file created by PyInstaller not working?

The most common reason a PyInstaller package fails is that PyInstaller failed to bundle a required file. Such missing files fall into a few categories: Hidden or missing imports: Sometimes PyInstaller can't detect the import of a package or library, typically because it is imported dynamically.

What does ImportError mean?

ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised. If a module does not exist.

How do I disable UPX in PyInstaller?

PyInstaller looks for the UPX in the standard executable path(s) (defined by PATH environment variable), or in the path specified via the --upx-dir command-line option. If found, it is used automatically. The use of UPX can be completely disabled using the --noupx command-line option.


2 Answers

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']
like image 125
Raissel Ramirez Orozco Avatar answered Sep 24 '22 22:09

Raissel Ramirez Orozco


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')
like image 40
Ahad aghapour Avatar answered Sep 26 '22 22:09

Ahad aghapour