PyInstaller with Pandas creates over 500 MB exe

后端 未结 6 1203
栀梦
栀梦 2020-12-08 05:39

I try to create an exe file using PyInstaller 3.2.1, for test purpose I tried to make an exe for following code:

import pandas as pd
print(\'hello world\')
<         


        
6条回答
  •  萌比男神i
    2020-12-08 06:23

    I have the Anaconda 3.5.5 build for Python on Windows 10 and was also getting excessively large executables using the Anaconda distribution.

    I was able to correct this by doing the following:

    1. First create a virtual environment (forums suggest virtualenv, but this gave me problems so instead I used venv)

      python -m venv C:/Python/NewEnv
      

    This creates a virtual environment inside C:/Python/NewEnv with base python, pip and setuptools

    1. Next switch to the newly created environment

      C:/Python/NewEnv/Scripts/activate
      

    You'll know that the environment is different as your command prompt will be prefaced with your new environment name (NewEnv)

    1. Install numpy first, then scipy, then pandas

      pip install numpy==1.13.3
      pip install scipy==1.1.0
      pip install pandas==0.18.1
      pip install pypiwin32==223
      pip install pyinstaller==3.2
      

    I had to use these versions as I've tried different ones, but any later version of pandas were giving me further issues.

    1. Once these have been installed you can compile your program

      C:/Python/NewEnv/Scripts/pyinstaller --onefile program.py
      
    2. This will create a .spec file, which you'll need to modify with this version of pandas and pyinstaller to add hidden imports otherwise loading pandas from the executable will fail (Not sure if there's a pyinstaller command to just create the spec file, but if there is then rather do that - see ammendment#1)

    There will be a hidden imports line inside the newly created .spec file:

        hiddenimports=[],
    

    Change this to add pandas._libs.tslibs.timedeltas

        hiddenimports=['pandas._libs.tslibs.timedeltas'],
    
    1. Then you can compile your program again against the .spec file

      C:/Python/NewEnv/Scripts/pyinstaller --onefile program.spec
      

    Note that this will install the program in whichever directory you are in so change directories before executing pyinstaller.

    Ammendmend#1: I see that it's possible to add the hook-pandas.py to the Pyinstaller hooks. So after you install pyinstaller in the new environment, run

        echo hiddenimports = ['pandas._libs.tslibs.timedeltas'] > C:\Python\NewEnv\Lib\site-packages\PyInstaller\hooks\hook-pandas.py
    

提交回复
热议问题