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

前端 未结 4 460
北恋
北恋 2020-12-05 12: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.

相关标签:
4条回答
  • 2020-12-05 12:31

    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
    
    0 讨论(0)
  • 2020-12-05 12:32

    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')
    
    0 讨论(0)
  • 2020-12-05 12:37

    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!

    0 讨论(0)
  • 2020-12-05 12:52

    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']
    
    0 讨论(0)
提交回复
热议问题