Getting rid of chromedirver console window with pyinstaller - python

I'm using chromedriver in headless mode. I compile the script using pyinstaller as one exe file. Everything works fine, except that I get the following console window whenever I open a chrome page:
I've tried the options --windowed alone, --noconsole alone, --windowed and --noconsole together but I still get this window.
How can I get rid of it?

I was able to find the following answer and it's working perfectly for me:
To avoid getting console windows for chromedriver, open the file
Python\Lib\site-packages\selenium\webdriver\common\service.py
and change
self.process = subprocess.Popen(cmd, env=self.env, close_fds=platform.system() != 'Windows', stdout=self.log_file, stderr=self.log_file, stdin=PIPE)
To:
self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE ,stderr=PIPE, shell=False, creationflags=0x08000000)

I had a similar issue, I'd like to share how I fixed it. First I'll describe the context:
-- My script worked well, it opened chrome windows normally (not headless).
-- I used pyinstaller (with the onefile and noconsole commands).
-- The EXE worked but every time it opened a chrome window, it opened a console window as well, I don't remember what the window said, but it wasn't an error.
-- I tried the solution that Ahmed post, and it worked that day.
-- Next day I tried the EXE in various computers, and the problem came back.
-- I posponed that problem since it wasn't a fatal error and there were more important issues to resolve in my app. So while I was trying to fix another issue, I found this answer: https://stackoverflow.com/a/56839122/13988982.
-- Basically it said that changing the order of the commands that you use when you run pyinstaller, actually affects how the EXE file is packaged. (I'm not sure why).
-- I ran: pyinstaller --add-binary "chromedriver.exe;." --noconsole --onefile myApp.py
And that finally made the console window not to show anymore.
Hope this is useful for anyone.

Related

Python app not working after using pyinstaller but doesn't give any errors

So I made an app using python and kvlang, and I was trying to get all the files into a one standalone "exe" file. I needed to include the ".kv" file and my main script. I was using pyinstaller and wrote this command:
pyinstaller --onefile -w --icon=download.ico --add-data filefinder.kv;. filefinder.py
And it all went well - no errors or anything but when I launch the app I just get a quick flash of a white window and then it closes. I have determined that the error must be because of some issue with the ".kv" file but I am not able to fix it cause there's no errors, Nothing! I checked and the app works with the "onedir" option but I need to make it smaller in size. I also tried the "auto-py-to-exe" but it gives the same result. I am happy to provide any more info should you need it to help me resolve this issue. Cheers!
Additional info:
System: Windows 10 pro
Python: 3.9.1
kivy: 2.0.0
Pyinstaller: 4.2
Not sure why it doesn't work, but run the exe in command prompt and then when it fails the error message will not disappear.
Add lots of logs to your application, these can be print statements, as those will always end up on stdout.
i.e. on the first entrypoint, print("Running main")
When you call your first function:
print('calling function_name()')
Once that has finished
print('function_name() complete')
And so on and so forth until you find where exactly the program stops functioning.
Start -> cmd -> navigate to your file using cd -> type in the name of the exe to run it.

Console deactivation damages programme execution

Just when I thought I am 100% done with my app, turns out I was wrong. The app uses APScheduler to run several events at the same time and until now had been tested only in development mode, by bundling it in a single .exe file using Pyinstaller with the console visible for debugging purposes.
Very pleased I can finally remove the console window to distribute it to others, I was surprised to see that removing the console appears to mess up how APScheduler works. The times the tasks should be performed are wrong. Instead of the next time a task should run, it sets the current time, and no scheduled events are being executed. I have tried dragging it into the cmd to see if it shows any errors there, but it just stays blank. Everything else seems to be working fine, the GUI, the system tray code, the SQL tables, etc..
Briefly, the code I use is:
pyinstaller --add-data images.png;. --onefile --icon=trayicon.ico -w script.py
I also tried --windowed command. Funnily enough, --noconsole shows as an unrecognised argument.
I tried making it into a .pyw file but the same problem is observed.
Can anyone perhaps explain:
What actually happens on a deeper level when one removes the console/window from the executable? This might set me on the right path to figuring out the mess up with APScheduler.
Is there a way to just hide the console/window in the background, instead of removing it entirely?
PS: I am using Windows 10, Anaconda 3, Python 3.7.6, Spyder IDE
SOLUTION:
Still not sure why (the lack of) console messes up the programme, but if anyone encounters similar problem in the future, I fixed it by creating the .exe with a window, and HIDING the console window using the following command at the very start of my code:
import win32gui
win32gui.ShowWindow(win32gui.GetForegroundWindow(), win32con.SW_HIDE)

Pyinstaller opens command prompt

I'm still a beginner with coding so maybe this question will be trivial for some of you. My apologies in advance.
For a project, i made a webcrawler using python and selenium, with an user interface made with Tkinter. I use a chromedriver to open chrome for the webcrawling part.
I converted my Tkinter file with pyinstaller into an executable file. When doing so, i typed the following flags: --onefile -w
I was told that the latter one prevents opening a command prompt when running the file, however, when i run the executable, my pc opens the command prompt for the chromedriver. How can i fix this?
-w will prevent the creation of a console for the main exe, not for the execution of the chromedriver.
You should have a look at ChromeDriver console application hide thread for removing the console created by chromedriver

Pyinstaller --onefile mode, how to write a message to console before unpacking

I have an .exe I'm packaging using pyinstaller and it works like a charm. However when the .exe is clicked, I have to wait nearly 10 seconds while staring at a blank console window for the actual application to start.
From my research I've surmised that this is because of --onefile, and all the various files packaged in need to be unpacked before any code is run. I'm not concerned about the 10 second wait, but new users often need support because they think the program isn't working (reasonably so).
My ask is incredibly simple, but I can't figure out a way to do it: Is there anyway to get pyinstaller to run a tiny script BEFORE UNPACKING to just post a status blurb to the console, so users know that it's working?
As far as I know it is currently not possible to display custom messages before unpacking to let the user know the application is working. Source
There are a few workarounds to let the user know the program is working.
Display the console window
Displaying the console window after launching the application will output the status of the PyInstaller Bootloader while it is being opened.
To display the console, use theĀ --console flag (or edit the .spec file to containĀ console = True) when bundling your application
Enable debug mode
To enable debug mode, use the --debug flag (or edit the .spec file to contain debug = True)
Example
exe = EXE(pyz,
//...
debug=True,
console=True )
You could always wrap your program into 7zip installer. You can add a quick shell script to say "Loading..." before running your main program or you could just edit config.txt to do the same.
How do I make a self extract and running installer

Making a PyInstaller exe do both command-line and windowed

I am writing a Python program that can be used both on the command-line, and as an interactive window. (Is that a bad idea?) If command-line arguments are supplied, it executes a task, then prints "success" or "failure". Otherwise, it launches an interactive window.
PyInstaller doesn't seem to be built to support this. I have two non-optimal options:
Use --console mode: The command-line works great, but if I double-click the exe to show the interactive window, it also shows a console window that I don't want
Use --noconsole mode: There's no console popup, but no output shows when using the command-line.
It seems I either need a way to not pop-up the console in --console mode, or to show print output in --noconsole mode. If neither of those options work, I may need to make a separate command-line version of the program.
Any advice?
This is not a perfect solution, but this workaround did the job for me:
Build gui app in --noconsole --one file mode like this:
pyinstaller --noconsole --onefile hello.py
When you double click on the app from windows it will launch normally (without the console).
Now to see the output, navigate to the executable from the command line and type:
hello.exe | more
The "| more" should send the print statements to the console.
This is a problem with Windows (not PyInstaller), which requires the subsystem to be specified as either CONSOLE or WINDOWS at compilation-time.
https://github.com/pyinstaller/pyinstaller/issues/6244#issuecomment-927131015
The recommended solution is to split your app (eg hello) into two distinct versions:
hellow.exe for the GUI version (windowed) and
hello.exe for the CLI version (console)
In theory, you could also add a wrapper .exe that switches between the two actual binaries above, depending on how it's called..

Categories

Resources