如何在pyinstaller中包含selenium chromedriver?

2024-05-03 04:28:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用pyinstaller打包我的应用程序,这样其他人就可以使用它而不必下载所有额外的文件等。目前,我的脚本使用以下代码启动selenium chromedriver:

import os, inspect

current_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe() ))[0]))

chromedriver = os.path.join(current_folder, "chromedriver")

browser = webdriver.Chrome(chromedriver, options=chrome_options )

我正在用以下代码创建一个spec文件:

^{pr2}$

然后编辑spec文件中的二进制文件:

binaries=[('/Users/Vetle/Python/Scripts/Wiredelta_Timer/Final/chromedriver', '.')],

然后我用以下方法创建文件:

pyinstaller check_in_final.spec

当尝试启动我新创建的打包应用程序时,会出现错误:

Traceback (most recent call last):
  File "site-packages/selenium/webdriver/common/service.py", line 76, in start
  File "subprocess.py", line 769, in __init__
  File "subprocess.py", line 1516, in _execute_child
FileNotFoundError: [Errno 2] No such file or directory: '/Users/Vetle/chromedriver': '/Users/Vetle/chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "check_in_final.py", line 248, in <module>
  File "check_in_final.py", line 223, in login_timer
  File "site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
  File "site-packages/selenium/webdriver/common/service.py", line 83, in start
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

[21745] Failed to execute script check_in_final

我试着把chromedriver文件放在我的主文件夹里,这很管用。但是,我不希望文件存储在那里,同样的情况也发生在我的应用程序中的文本文件中-任何方式我都可以将它们存储在应用程序文件中或在其中创建一个新的目录文件夹?在


Tags: 文件pathinpy应用程序oscheckselenium