【问题标题】:Error while executing my .exe made with pyinstaller执行我用 pyinstaller 制作的 .exe 时出错
【发布时间】:2021-10-14 02:06:17
【问题描述】:

我做了一个在 python 中使用 selenium web 驱动程序的小程序。我的程序在 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
  • 我应该用这个命令做什么?在哪里 ?它在cmd中不起作用
  • 对不起,您可以将其添加到 pyinstaller 中,例如pyinstaller ./main.py --collect-data selenium。它是一个 pyinstaller 开关,用于收集包数据文件,例如 selenium\webdriver\remote\getAttribute.js。您可以在此处查看文档:pyinstaller.readthedocs.io/en/stable/…
  • 好的,现在我的程序(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)
  • 我只需将./driver/chromedriver.exe" 更改为r"C:/ etc 就可以了

标签: python selenium webdriver selenium-chromedriver pyinstaller


【解决方案1】:

我使用了规范文件

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))

【讨论】:

    【解决方案2】:

    添加

    --collect-data selenium
    

    pyinstaller 命令行选项,

    应该是这样的:

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

    【讨论】:

      【解决方案3】:

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

      我遇到了pyinstaller 4.7这个问题

      但是pyinstaller==4.3 可以解决。

      试试:

      pip install pyinstaller==4.3
      

      【讨论】:

        猜你喜欢
        • 2016-12-26
        • 2019-11-17
        • 1970-01-01
        • 2021-04-22
        • 2019-05-07
        • 2021-06-14
        • 2018-10-07
        • 2012-06-16
        • 2020-12-08
        相关资源
        最近更新 更多