python-3.x - 无法使用 PyInstaller 错误创建 .exe 文件

标签 python-3.x pandas pyinstaller

我正在使用 Python 3.7.3 和 PyInstaller 4.0 以及 Windows 10。我的脚本看起来像

import pandas as pd
print('hello')

但是当我尝试执行 .exe 文件时出现错误。

到目前为止,我已经尝试了以下方法:

pyinstaller --hidden-import=pandas --onefile myscript.py

但它不起作用。我也在这里更新到当前的开发版本: https://pyinstaller.readthedocs.io/en/stable/installation.html

此外,我编辑 .spec 文件并编写

# -*- mode: python -*-block_cipher = Nonedef get_pandas_path():
    import pandas
    pandas_path = pandas.__path__[0]
    return pandas_patha = Analysis(['FIFA.py'],
             pathex=['C:\\Users\\NBenton\\PycharmProjects\\RES3D_BETA'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             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,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='FIFA',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

然后运行

pyinstaller myscript.spec --onefile

我知道这是一个常见问题,但其他问题的答案对我不起作用。

有什么帮助吗? 谢谢

在很多行之后,错误是:

File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\hooks\hook-numpy.core.py", line 29, in <module>
    pkg_base, pkg_dir = get_package_paths('numpy.core')
  File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 528, in get_package_paths
    file_attr = get_module_file_attribute(package)
  File "c:\programdata\anaconda3\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 330, in get_module_file_attribute
    raise ImportError
ImportError

最佳答案

Hook -pandas.py

datas = collect_data_files('pandas') 添加到 hook-pandas.py 文件对我有用

from PyInstaller.utils.hooks import collect_submodules
from PyInstaller.utils.hooks import collect_data_files

# Pandas keeps Python extensions loaded with dynamic imports here.
hiddenimports = collect_submodules('pandas._libs')
datas = collect_data_files('pandas')

Anaconda 提示符

我还添加了隐藏导入 pkg_resources.py2_warn 因为这是我收到的错误

(base) C:\Users\...\test_folder>pyinstaller test.py -F --hidden-import pkg_resources.py2_warn
102 INFO: PyInstaller: 3.6
102 INFO: Python: 3.7.6 (conda)
106 INFO: Platform: Windows-10-10.0.18362-SP0
116 INFO: wrote C:\Users...\test_folder\test.spec
120 INFO: UPX is not available.
125 INFO: Extending PYTHONPATH with paths

注意:-Fpyinstaller test.py -F --hidden-import pkg_resources.py2_warn中的--onefile相同

测试.py

import pandas as pd
print('hello world')
input()

关于python-3.x - 无法使用 PyInstaller 错误创建 .exe 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61396071/

相关文章:

python - 在Python中读取具有不同长度列的.csv文件作为字典

python - 对齐两个数据框列并保留顺序(没有字典顺序重新排序

python - 导出单个 .exe 时,PyInstaller 卡在 "Building PKG ..."上

python - Pandas ,使用 apply 方法吗?

python - 无法将 Jinja2 模板包含到 Pyinstaller 分发中

python - NSUserNotificationCenter.defaultUserNotificationCenter() 使用 PyInstaller 返回 None

python - 将Python库添加到远程集群机器?

python - 如何在 PyQt5 InputDialog 中获得更多输入文本?

python - 尽管设置了边界,但角色在 Python/PyGame 中移出屏幕

python-3.x - 如何将逗号分隔值提取到 Pandas 中的各个行?