【问题标题】:pyinstaller one file --no-console does not work "Fatal Error"pyinstaller 一个文件 --no-console 不起作用“致命错误”
【发布时间】:2018-03-20 05:26:13
【问题描述】:

我尝试了 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,但是现在控制台窗口出现了......

Python program with selenium(webdriver) Don't Work as single and noconsole exe file (pyinstaller)

【问题讨论】:

标签: python selenium pyinstaller


【解决方案1】:

经过一番搜索,我找到了完整的解决方案: 首先转到 -

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

【讨论】:

  • 你的答案是对的,但你仍然在任务管理器上打开运行的 chromedrivers,有什么解决方案吗?
【解决方案2】:

我希望这对某人有所帮助:

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: 点安装 pywin32

【讨论】:

    【解决方案3】:

    首先去- 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。

    就我而言,第一种方法有效。

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 1970-01-01
      • 2016-09-27
      • 2019-11-29
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      相关资源
      最近更新 更多