简体   繁体   English

pyinstaller 一个文件 --no-console 不起作用“致命错误”

[英]pyinstaller one file --no-console does not work “Fatal Error”

I try 2 options with pyinstaller one with console and one without.我尝试使用 pyinstaller 的 2 个选项,一个带控制台,一个不带。

I am using Windows 10, Python 3.5.4, Windows Chrome Driver 2.33 and Selnium 3.6.0 and Pyinstaller 3.3.我使用的是 Windows 10、Python 3.5.4、Windows Chrome 驱动程序 2.33 和 Selnium 3.6.0 以及 Pyinstaller 3.3。

The one without console fails:没有控制台的那个失败了:

Here is the code for Test.py这是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")

Here are the pyinstaller commands with the exact same code:以下是具有完全相同代码的 pyinstaller 命令:

- With console window aka works: - 使用控制台窗口也可以工作:

pyinstaller -F -i favicon.ico Test.py

- Without the console Window fails - 没有控制台窗口失败

pyinstaller -F --noconsole -i favicon.ico Test.py

I get this error:我收到此错误:

*"Failed to execute script error"*

I am not sure why.我不知道为什么。

Thanks谢谢


Thanks for your reply I looked in:谢谢你的回复我看了:

C:\Users\testuser\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\chrome\service.py

There was nothing for subprocess here.这里没有子流程。

I also checked:我还检查了:

C:\Users\testuer\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\common\service.py

On line 70 I added in the entry for stdin=self.log_file as it was missing在第 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)

Recreated with pyinstaller:使用 pyinstaller 重新创建:

pyinstaller -F --noconsole  -i favicon.ico Test.py

This created the exe however now the console window appeared....这创建了 exe 但是现在控制台窗口出现了....

Python program with selenium(webdriver) Don't Work as single and noconsole exe file (pyinstaller) 带有 selenium 的 Python 程序(webdriver)不能作为单个和 noconsole exe 文件(pyinstaller)工作

after some search I found the full solution : Firstly go to -经过一番搜索,我找到了完整的解决方案:首先转到-

C:\Python35\Lib\site-packages\selenium\webdriver\common\service.py

Change :改变:

self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self.log_file, stderr=self.log_file)

To:至:

self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False, creationflags=0x08000000)

now simply build your app.py like this:现在只需像这样构建你的 app.py:

pyinstaller --noconsole --onefile  --noupx   Yourfile.py

I hope this helps someone:我希望这有助于某人:

C:\\Users\\%user_name%\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\selenium\\webdriver\\common C:\\Users\\%user_name%\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\selenium\\webdriver\\common

Add import and change in "def start":在“def start”中添加导入和更改:

from win32process import CREATE_NO_WINDOW从 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

if error, check pywin32: pip install pywin32如果错误,请检查 pywin32:pip install pywin32

Firstly go to- C:\\Python35\\Lib\\site-packages\\selenium\\webdriver\\common\\service.py首先转到- C:\\Python35\\Lib\\site-packages\\selenium\\webdriver\\common\\service.py

then in def start(self):然后在def start(self):

def start(self): """ Starts the Service. 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)``

Just add creationflags=0x08000000 , I've already added there in the code.只需添加creationflags=0x08000000 ,我已经在代码中添加了。 creationflags is a constant. creationflags 是一个常数。 It will work fine.它会正常工作。

or, you might also replace it with creationflags=CREATE_NO_WINDOW and add this line code from win32process import CREATE_NO_WINDOW .或者,您也可以将它替换为creationflags=CREATE_NO_WINDOWfrom win32process import CREATE_NO_WINDOW添加此行代码。

In my case, first method was working.就我而言,第一种方法有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM