在我问这里之前,我不知道如何创建可执行的python程序。幸运的是,我收到了快速答复,并且能够将我的脚本转换为可执行程序。可执行文件可以完美运行,但只能在我的计算机上运行。 这是我收到的两个错误,我觉得我需要修改脚本才能找到chrome驱动程序,我不确定Pyinstaller将所有内容保存在哪里。
Exception in Tkinter callback
Traceback (most recent call last):
File "site-packages\selenium\webdriver\common\service.py", line 76, in start
File "subprocess.py", line 775, in __init__
File "subprocess.py", line 1178, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tkinter\__init__.py", line 1705, in __call__
File "MarijuanaDoctors.py", line 25, in search
File "site-packages\selenium\webdriver\chrome\webdriver.py", line 68, 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
答案 0 :(得分:1)
您可以使用Pyinstaller将“ chromedriver.exe”与脚本捆绑在一起,如下所示:
pyinstaller --add-binary="localpathtochromedriver;." myscript.py
这会将“ chromedriver.exe”文件复制到与主.exe相同的文件夹中(或者,如果使用pyinstaller的“单文件”选项,则使用exe程序时,此填充将提取到temp文件夹中。)
在脚本中,您可以检查您是在正常运行脚本还是从捆绑(exe文件)模式运行脚本,并相应地选择chromedriver.exe的路径。( pyinstaller)
import sys
if getattr(sys, 'frozen', False ):
#Running from exe, so the path to exe is saved in sys._MEIPASS
chrome_driver = os.path.join(sys._MEIPASS, "chromedriver.exe")
else:
chrome_driver = 'localpathtochromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)
您可以在文档here中阅读有关此内容的信息。
限制: 您的.exe用户应在系统上安装Chrome,Chrome版本应与捆绑的chromedriver兼容。