PyInstaller and Pandas
我有一个非常简单的Python模块,正在尝试将其编译为Windows .exe文件。 在我的脚本中,我正在使用wxPython和Pandas库。 仅当从我的模块中排除了Pandas库时,才生成/运行的PyInstaller .exe文件。
无论在PyInstaller中使用
1 2 3 | PyInstaller: version 2.1 pandas: version 0.15.2 Python: version 2.7 |
我遇到了同样的问题。我将其简化为类似于Hello.py这样的简单脚本:
1 2 | import pandas print"hello world, pandas was imported successfully!" |
为了使熊猫在运行时正确导入,我必须将Hello.spec修改为以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # -*- 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') |
然后我跑了:
1 | $pyinstaller Hello.spec --onefile |
从命令提示符下,得到了我所期望的" hello world"消息。我仍然不完全理解为什么这样做是必要的。我有一个自定义的熊猫构建-已挂接到MKL库中-但是我不清楚这是否导致运行失败。
这类似于这里的答案:Pyinstaller不正确导入pycripto ...有时
pyinstaller 3.3版存在类似问题。解决的办法是缺少一个隐藏的导入钩,如此处所述
我在Pyinstaller / hooks /下创建了一个名为hook-pandas.py的新文件,并将此提交中所述的内容放在此处,然后通过python setup.py install在Pyinstaller目录中手动重新安装了pyinstaller。
当我使用--onefile选项使用pyinstaller从pandas脚本创建exe时,此问题不再发生。
就像另一种解决方案一样,向
如果您不想接触pyinstaller的来源,这可能会有所帮助。
使用python version = 3.8和pyinstaller = 3.6时,无需自定义pyinstaller或添加熊猫钩子,钩子pandas.py已存在于Lib \ site-packages \ PyInstaller \ hooks中,并且一切正常。
我通过在项目目录(每个pyinstaller文档)中使用钩子文件hook-pandas.py解决了相同的问题
1 2 3 4 5 6 | hiddenimports = [ 'pandas._libs.tslibs.timedeltas', 'pandas._libs.tslibs.nattype', 'pandas._libs.tslibs.np_datetime', 'pandas._libs.skiplist', ] |
然后在spec文件中添加一行:
1 2 3 4 5 | ... a = Analysis([... hookspath=['.'], ...], ... |
我试图在规格文件中包含