PyInstaller 和 Pandas

新手上路,请多包涵

我有一个相当简单的 Python 模块,我正试图将其编译成 Windows .exe 文件。在我的脚本中,我使用了 wxPython 和 Pandas 库。生成的 PyInstaller .exe 文件仅在 Pandas 库从我的模块中排除 时才 有效/打开。

无论我在 PyInstaller 中使用 --onefile 还是 --onedir 我都会遇到同样的问题。我在网上发现 PyInstaller (2.1) 的“新”版本应该已经解决了这个错误。有没有人知道该怎么做?

 PyInstaller: version 2.1
pandas: version 0.15.2
Python: version 2.7

原文由 Gohawks 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 502
1 个回答

我遇到了同样的问题。我把它归结为一个像 Hello.py 这样的简单脚本:

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

为了让 pandas 在运行时正确导入,我必须将 Hello.spec 修改为以下内容:

 # -*- 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')

然后我跑了:

 $pyinstaller Hello.spec --onefile

从命令提示符并得到我预期的“hello world”消息。我仍然不完全理解为什么这是必要的。我有一个自定义构建的 pandas - 它已连接到 MKL 库 - 但我不清楚这是导致运行失败的原因。

这类似于此处的答案: Pyinstaller not correclty importing pycripto… sometimes

原文由 physicistintheory 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题