使用 PyInstaller 制作的 .exe 文件执行时出现错误。

4

我写了一个小程序,使用Python中的Selenium Web Driver。当我在Spyder上运行我的程序时,它能正常工作。 然后我使用Pyinstaller制作exe文件。

pyinstaller ./main.py --onefile --noconsole --add-binary "./driver/chromedriver.exe;./driver"

但是我无法启动它,我得到了这个错误

Traceback (most recent call last):
File "noto.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
File "selenium\webdriver\__init__.py", line 18, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
File "selenium\webdriver\firefox\webdriver.py", line 29, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
File "selenium\webdriver\remote\webdriver.py", line 26, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
File "selenium\webdriver\remote\webelement.py", line 43, in <module>
File "pkgutil.py", line 637, in get_data
File "PyInstaller\loader\pyimod03_importers.py", line 479, in get_data
FileNotFoundError: [Errno 2] No such file or 
directory:'C:\\Users\\Mon nom\\AppData\\Local\\Temp\\1\\_MEI50482\\
selenium\\webdriver\\remote\\getAttribute.js'
[28052] Failed to execute script 'noto' due to unhandled exception!

实际上,我没有这个目录,因为selenium安装在以下路径: C:\Users\Mon nom\Anaconda3\Lib\site-packages\selenium\webdriver\remote,而我有getAttribute.js文件。

是否有人有想法? :)


--collect-data selenium - gfdsweds
1
我该怎么处理这个命令?在哪里?它在cmd中不起作用。 - Quentin Moreau
1
抱歉,您可以将其添加到pyinstaller中,例如 pyinstaller ./main.py --collect-data selenium。这是一个pyinstaller开关,用于收集包数据文件,例如selenium\webdriver\remote\getAttribute.js。您可以在此处查看文档:https://pyinstaller.readthedocs.io/en/stable/usage.html#cmdoption-collect-data - gfdsweds
好的,现在我的程序(tk)已经启动了,但我仍然遇到一个错误: Message: 'chromedriver.exe' executable needs to be in PATH为了提供信息,我执行了: pyinstaller ./noto.py --onefile --add-binary "./driver/chromedriver.exe;./driver" --collect-data selenium还有,在我的.py文件中,我已经通知了: driver = webdriver.Chrome("./driver/chromedriver.exe",options=chrome_options) - Quentin Moreau
我只是将 ./driver/chromedriver.exe" 更改为 r"C:/ etc,然后它就可以工作了。 - Quentin Moreau
3个回答

2

只需添加

--collect-data selenium

在你的pyinstaller命令行中添加选项,应该像这样:

pyinstaller ./main.py  --collect-data selenium  --onefile --noconsole --add-binary "./driver/chromedriver.exe;./driver"

0

我使用了规范文件

pyinstaller --clean PolyTasks.spec

规格文件是

# -*- mode: python ; coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_data_files

datas = []
datas += collect_data_files('selenium')


block_cipher = None


a = Analysis(['PolyTasks.py'],
             pathex=[],
             binaries=[ ( './driver/94chromedriver.exe', './driver' ) ],
             datas=datas,
             hiddenimports=[],
             hookspath=[],
             hooksconfig={},
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts, 
          [],
          exclude_binaries=True,
          name='PolyTasks',
          debug=True,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas, 
               strip=False,
               upx=True,
               upx_exclude=[],
               name='PolyTasks')
import shutil
shutil.copyfile('poly_ep_ring.csv', '{0}/poly_ep_ring.csv'.format(DISTPATH))

0

尝试使用不同的pyinstaller版本进行构建。

我在使用pyinstaller 4.7时遇到了这个问题。

但是使用pyinstaller==4.3后问题得以解决。

尝试:

pip install pyinstaller==4.3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接