How do I use a .bat file to start a .pyw script without cmd popping up?

Viewed 935

I want to use a .bat file to run a python script without a command prompt showing up. I've read that saving the script as .pyw should do the trick but somehow command prompt keeps showing up.

My .bat file looks like this:

@C:\Users\path\to\pythonw3.8.exe C:\Users\path\to\script.pyw

The python script does nothing but creates a simple shelve file:

import os, shelve

os.chdir(r'C:\Users\Sean\Desktop\Coding\Project-Updatable_Multi-Clipboard')
success = shelve.open('success')

When I double-click the .bat file the python script itself works as intended but for some reason, a command prompt shows up for a split second. I've tried exchanging "pythonw3.8.exe" in the .bat file with "pythonw3.exe" and "pythonw.exe" but I get the same behaviour as with plain "python.exe".

Do I have to modify the .bat file or the .pyw script in order to run the script without the command prompt popping up?

2 Answers

As @Squashman pointed out .bat files are interpreted by cmd.exe. As a result a

batch file which will always open a console window

regardless of whether a .pyw script or a .py script is run or not.

As @aschipfl explained creating a shortcut (.lnk file) like this:

C:\Users\path\to\pythonw3.8.exe C:\Users\path\to\script.pyw

is an easy solution to launch a .pyw script without a command window opening.

I think that you can use Echo command at start your batch code:

@echo off
@C:\Users\path\to\pythonw3.8.exe C:\Users\path\to\script.pyw
Related