[英]pyinstaller one file --no-console does not work “Fatal Error”
我嘗試使用 pyinstaller 的 2 個選項,一個帶控制台,一個不帶。
我使用的是 Windows 10、Python 3.5.4、Windows Chrome 驅動程序 2.33 和 Selnium 3.6.0 以及 Pyinstaller 3.3。
沒有控制台的那個失敗了:
這是Test.py的代碼
#!python3
from selenium import webdriver
# Chrome Proxy options and disable the Yellow Bar about Chrome being controlled by Automated Software.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-logging")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-default-apps")
chrome_options.add_argument("--disable-extensions")
# load google.com with the proxy settings and a logging path to a file
driver = webdriver.Chrome(r"D:\Selenium\chromedriver_win32\chromedriver.exe", chrome_options=chrome_options)
# maximise window
driver.maximize_window()
driver.get("https://www.google.com/mail")
以下是具有完全相同代碼的 pyinstaller 命令:
- 使用控制台窗口也可以工作:
pyinstaller -F -i favicon.ico Test.py
- 沒有控制台窗口失敗
pyinstaller -F --noconsole -i favicon.ico Test.py
我收到此錯誤:
*"Failed to execute script error"*
我不知道為什么。
謝謝
謝謝你的回復我看了:
C:\Users\testuser\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\chrome\service.py
這里沒有子流程。
我還檢查了:
C:\Users\testuer\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\common\service.py
在第 70 行,我添加了 stdin=self.log_file 的條目,因為它丟失了
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdin=self.log_file, stdout=self.log_file, stderr=self.log_file)
使用 pyinstaller 重新創建:
pyinstaller -F --noconsole -i favicon.ico Test.py
這創建了 exe 但是現在控制台窗口出現了....
帶有 selenium 的 Python 程序(webdriver)不能作為單個和 noconsole exe 文件(pyinstaller)工作
經過一番搜索,我找到了完整的解決方案:首先轉到-
C:\Python35\Lib\site-packages\selenium\webdriver\common\service.py
改變:
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file, stderr=self.log_file)
至:
self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False, creationflags=0x08000000)
現在只需像這樣構建你的 app.py:
pyinstaller --noconsole --onefile --noupx Yourfile.py
我希望這有助於某人:
C:\\Users\\%user_name%\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\selenium\\webdriver\\common
在“def start”中添加導入和更改:
從 win32 進程導入 CREATE_NO_WINDOW
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdin=self.log_file, stdout=self.log_file, stderr=self.log_file, creationflags=CREATE_NO_WINDOW)
except TypeError:
raise
如果錯誤,請檢查 pywin32:pip install pywin32
首先轉到- C:\\Python35\\Lib\\site-packages\\selenium\\webdriver\\common\\service.py
然后在def start(self):
def start(self):
""" Starts the Service.
:Exceptions:
- WebDriverException : Raised either when it can't start the service
or when it can't connect to the service
"""
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,
creationflags=0x08000000)``
只需添加creationflags=0x08000000 ,我已經在代碼中添加了。 creationflags 是一個常數。 它會正常工作。
或者,您也可以將它替換為creationflags=CREATE_NO_WINDOW
並from win32process import CREATE_NO_WINDOW
添加此行代碼。
就我而言,第一種方法有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.