Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

python - PyInstaller and Pandas

I have a fairly simple Python module that I am trying to compile into a Windows .exe file. In my script I am using the wxPython and Pandas libraries. The PyInstaller .exe file that is generated only works/opens when the Pandas library is excluded from my module.

I am getting the same issue whether I use --onefile or --onedir in PyInstaller. I found online that the "new" version of PyInstaller (2.1) should have taken care of this bug. Does anyone have any ideas on what to do?

PyInstaller: version 2.1
pandas: version 0.15.2
Python: version 2.7
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I ran into the same issue. I boiled it down to a simple script like this Hello.py:

import pandas
print "hello world, pandas was imported successfully!"

To get pandas to import at run-time correctly I had to modify the Hello.spec to the following:

# -*- mode: python -*-

block_cipher = None

def get_pandas_path():
    import pandas
    pandas_path = pandas.__path__[0]
    return pandas_path

a = Analysis(['Hello.py'],
         pathex=['C:\ScriptsThatRequirePandas'],
         binaries=None,
         datas=None,
         hiddenimports=[],
         hookspath=None,
         runtime_hooks=None,
         excludes=None,
         win_no_prefer_redirects=None,
         win_private_assemblies=None,
         cipher=block_cipher)

dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      exclude_binaries=True,
      name='Hello',
      debug=False,
      strip=None,
      upx=True,
      console=True )
scoll = COLLECT(exe,
           a.binaries,
           a.zipfiles,
           a.datas,
           strip=None,
           upx=True,
           name='Hello')

I then ran:

$pyinstaller Hello.spec --onefile

from the command prompt and got the 'hello world' message I expected. I still don't completely understand why this is necessary. I have a custom build of pandas - which is hooked into the MKL libraries - but it isn't clear to me that this is causing the run failure.

This is similar to the answer here: Pyinstaller not correclty importing pycripto... sometimes


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...