Python PyInstaller 和 Pandas

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29109324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:06:14  来源:igfitidea点击:

PyInstaller and Pandas

pythonpandaspyinstaller

提问by bsheehy

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 onlyworks/opens when the Pandas library is excluded from my module.

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

I am getting the same issue whether I use --onefileor --onedirin 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?

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

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

回答by physicistintheory

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

我遇到了同样的问题。我把它归结为一个简单的脚本,像这样的 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:

为了让 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')

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.

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

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

这类似于这里的答案:Pyinstaller not correclty importing pycripto...有时

回答by John

I had a similar issue with pyinstaller version 3.3. The solution was that there was a missing hiddenimport hook as described here

我在 pyinstaller 3.3 版中遇到了类似的问题。解决方案是缺少 hiddenimport 钩子,如here所述

I created a new file under Pyinstaller/hooks/ called hook-pandas.py and put the content as described in this commit hereand reinstalled pyinstaller manually via python setup.py install in the Pyinstaller directory.

我在 Pyinstaller/hooks/ 下创建了一个名为 hook-pandas.py 的新文件,并将本次提交中描述的内容放在这里,并通过 Pyinstaller 目录中的 python setup.py install 手动重新安装了 pyinstaller。

The issue did not recur when I created exe from my pandas script with pyinstaller using the --onefile option.

当我使用 --onefile 选项使用 pyinstaller 从我的 Pandas 脚本创建 exe 时,问题没有再次出现。

回答by Alto.Clef

Just as another solution, adding --hidden-import=pandas._libs.tslibs.timedeltaor whatever the module is missing to the pyinstallercommand also works.

就像另一种解决方案一样,--hidden-import=pandas._libs.tslibs.timedeltapyinstaller命令中添加或缺少模块的任何内容也都有效。

This may be helpful if you don't want to touch the source of pyinstaller.

如果您不想接触 pyinstaller 的源代码,这可能会有所帮助。

回答by mikey

I solved the same problem by using a hook file in the project directory (per pyinstaller document), hook-pandas.py

我通过在项目目录中使用钩子文件(每个 pyinstaller 文档),hook-pandas.py 解决了同样的问题

hiddenimports = [
    'pandas._libs.tslibs.timedeltas',
    'pandas._libs.tslibs.nattype',
    'pandas._libs.tslibs.np_datetime',
    'pandas._libs.skiplist',
]

then adding one line in the spec file:

然后在规范文件中添加一行:

...
a = Analysis([...
hookspath=['.'],
...],
...

I tried to include hiddenimports=[..., 'pandas', ...]in the spec file, somehow that didn't work out as expected.

我试图包含hiddenimports=[..., 'pandas', ...]在规范文件中,不知何故没有按预期工作。

回答by vpa

With python version=3.8 and pyinstaller=3.6, no need to custom the pyinstaller or add pandas hook, the hook-pandas.py already exists in Lib\site-packages\PyInstaller\hooks and everything works fine.

使用python version=3.8和pyinstaller=3.6,不需要自定义pyinstaller或添加pandas hook,hook-pandas.py已经存在于Lib\site-packages\PyInstaller\hooks中,一切正常。