如何使用 pyinstaller 将 chromedriver.exe 与在 selenium webdriver 上运行的 python 脚本合并?
How to Merge chromedriver.exe with a python script that runs on selenium webdriver using pyinstaller?
据我所知,可以合并或添加@Phillip 询问的chromedriver.exe with a python script that runs on selenium webdriver using Pyinstaller with reference to this 。
这是我的代码(python):
# importing packages / modules
import os
import service # This module cannot be installed as it asks for Microsoft Visual C++ 14 to be isntalled on pc
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Defining required functions that needs to be used again and again
def init_driver():
chrome_path = os.path.join(os.path.dirname(__file__), 'selenium','webdriver','chromedriver.exe')
service = service.Service(chrome_path) # So, this cannot be used to merge it.
path = webdriver.Chrome(service, options=Options())
driver = init_driver()
以及在 cmd 中给出的命令:
pyinstaller --noconfirm --onefile --console --icon "path/to/icon.ico" --add-binary "path/to/chrome/driver;./driver" "path/to/the/python/pythonscript.py"
我们需要牢记以下两点:
- Pyinstaller 选项
- 源代码中的驱动程序路径
Chromedriver.exe 是二进制文件,因此,我们可以使用 --add binary
将其添加到 Pyinstaller,正如我在问题中提到的那样。
因此,我们在 cmd 中的 pyinstaller 命令将是这样的:
pyinstaller --noconfirm --onefile --console --icon "path/to/icon.ico" --add-binary "path/to/chrome/driver;./driver" "path/to/the/python/pythonscript.py"
其次,我们需要以这样一种方式修改源代码,它不会损害我们的代码,无论是在 python 控制台还是在转换后的可执行文件中。
因此,我们需要将 driver_path
代码从
更改为
driver = webdriver.Chrome('path/to/chromedriver.exe', options=Options())
像这样
import os
import sys
def resource_path(another_way):
try:
usual_way = sys._MEIPASS # When in .exe, this code is executed, that enters temporary directory that is created automatically during runtime.
except Exception:
usual_way = os.path.dirname(__file__) # When the code in run from python console, it runs through this exception.
return os.path.join(usual_way, another_way)
driver = webdriver.Chrome(resource_path('./driver/chromedriver.exe'), options=Options())
我们编写 Exception
部分是为了使其与 python 控制台一起工作。如果 运行 仅以 .exe 格式为主要目标,那么我们也可以忽略异常部分。
当我们试图从 class 访问 protected 成员 时,此代码在 python 控制台中抛出指向 _MEIPASS
的警告.除此之外,突出显示的是 sys.pyi
找不到对 _MEIPASS
的引用。
据我所知,可以合并或添加@Phillip 询问的chromedriver.exe with a python script that runs on selenium webdriver using Pyinstaller with reference to this
这是我的代码(python):
# importing packages / modules
import os
import service # This module cannot be installed as it asks for Microsoft Visual C++ 14 to be isntalled on pc
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Defining required functions that needs to be used again and again
def init_driver():
chrome_path = os.path.join(os.path.dirname(__file__), 'selenium','webdriver','chromedriver.exe')
service = service.Service(chrome_path) # So, this cannot be used to merge it.
path = webdriver.Chrome(service, options=Options())
driver = init_driver()
以及在 cmd 中给出的命令:
pyinstaller --noconfirm --onefile --console --icon "path/to/icon.ico" --add-binary "path/to/chrome/driver;./driver" "path/to/the/python/pythonscript.py"
我们需要牢记以下两点:
- Pyinstaller 选项
- 源代码中的驱动程序路径
Chromedriver.exe 是二进制文件,因此,我们可以使用 --add binary
将其添加到 Pyinstaller,正如我在问题中提到的那样。
因此,我们在 cmd 中的 pyinstaller 命令将是这样的:
pyinstaller --noconfirm --onefile --console --icon "path/to/icon.ico" --add-binary "path/to/chrome/driver;./driver" "path/to/the/python/pythonscript.py"
其次,我们需要以这样一种方式修改源代码,它不会损害我们的代码,无论是在 python 控制台还是在转换后的可执行文件中。
因此,我们需要将 driver_path
代码从
driver = webdriver.Chrome('path/to/chromedriver.exe', options=Options())
像这样
import os
import sys
def resource_path(another_way):
try:
usual_way = sys._MEIPASS # When in .exe, this code is executed, that enters temporary directory that is created automatically during runtime.
except Exception:
usual_way = os.path.dirname(__file__) # When the code in run from python console, it runs through this exception.
return os.path.join(usual_way, another_way)
driver = webdriver.Chrome(resource_path('./driver/chromedriver.exe'), options=Options())
我们编写 Exception
部分是为了使其与 python 控制台一起工作。如果 运行 仅以 .exe 格式为主要目标,那么我们也可以忽略异常部分。
当我们试图从 class 访问 protected 成员 时,此代码在 python 控制台中抛出指向 _MEIPASS
的警告.除此之外,突出显示的是 sys.pyi
找不到对 _MEIPASS
的引用。