0

I made a Python3 script that uses Webbot as a screenscraper. The script opens chrome, goes to a website and logs in.

While trying to automate this further and make it run when I log into Windows 10, I made a BAT file to run the python script. Although no matter what I try as far as methods of trying to execute the script I get a terminal window, similar to a node.js application.

enter image description here

Currently my BAT file looks like this

start "" python C:\Users\nicho\PycharmProjects\Startup\D2L.py
exit

How can I not have this pop up? If I try closing the window, it closes the chrome browser too! This doesn't happen when running from PyCharm.

Thanks.

Goggies
  • 33
  • 4
  • Either rename the script to "D2L.pyw" and run it directly, which relies on the .pyw file association being configured to run scripts instead of edit them, or create a shortcut that runs the script via pyw.exe (e.g. if the launcher is installed for all users, the target command line would be `C:\Windows\pyw.exe "C:\Users\nicho\PycharmProjects\Startup\D2L.py"`). By using the [py launcher](https://docs.python.org/3/using/windows.html#python-launcher-for-windows), your script can start with a shebang line to run in a particular installed version or virtual environment. – Eryk Sun Oct 05 '20 at 06:53
  • How do you want to execute the bat? By clicking? – x3l51 Oct 05 '20 at 07:08
  • @anonjnr Yes just by clicking – Goggies Oct 05 '20 at 07:29
  • @ErykSun So I tried renaming the script using .pyw and when I try running the BAT again or just running the D2L.pyw by double clicking it I still get the console/terminal. Any ideas what I'm doing wrong? – Goggies Oct 05 '20 at 07:34
  • @Goggies, of course running a batch script will create a console window, since cmd.exe is a console application. If .pyw files are set up to open by running them instead of editing them, they should be associated with either pyw.exe or pythonw.exe, which are not console applications. Maybe .pyw is associated incorrectly to py.exe or python.exe instead. Try using a shortcut instead that explicitly runs the script via pyw.exe. If you still get a console window, then something in the script is executing a console application. – Eryk Sun Oct 05 '20 at 07:45

1 Answers1

1

By clicking you will have a console which executes the batch file, which then executes python.

Renaming the script to .pyw or starting it with pythonw.exe in the first place only surpresses output from python. A console will be opened anyway.

You have to use a helper script; I found this:

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "C:\path\yourBat.bat" & Chr(34), 0
Set WshShell = Nothing

Save it as batchLauncher.vbs.

Your batch should look something like:

pythonw.exe "C:\Users\nicho\PycharmProjects\Startup\D2L.py"

Now execute batchLauncher.vbsvia double clicking and it should show: Nothing.

x3l51
  • 693
  • 4
  • 19
  • 1
    The point of renaming to .pyw and relying on the file association, or creating a shortcut that runs the script explicitly via pyw.exe, was to eliminate the otherwise unnecessary batch script. – Eryk Sun Oct 05 '20 at 07:47
  • I understand this. Yet OP wanted to know how to execute a batch file silently. – x3l51 Oct 05 '20 at 09:05
  • Okay, that assumes VBS files are associated with wscript.exe (no console) and not cscript.exe (console). With Python, one could use a PYW script (hopefully associated with pyw.exe or pythonw.exe) that runs a batch script with a windowless console session via `sys.exit(subprocess.call(batch_file_path, creationflags=subprocess.CREATE_NO_WINDOW))`. It's also possible to use a hidden window, like the VBS script does: `sys.exit(subprocess.call(batch_file_path, startupinfo=subprocess.STARTUPINFO(dwFlags=subprocess.STARTF_USESHOWWINDOW)))`. – Eryk Sun Oct 05 '20 at 10:12
  • @anonjnr Thanks for the help, it still does the console. But I think this comes down to an issue with Webbot not python. I gave up as I have found no solution and this is way too hard to do for a simple python script, not worth it. – Goggies Oct 05 '20 at 21:14