0

I have a Python program which I compiled into a .exe file using PyInstaller. When you open the .exe, there should be no console, no command prompt or whatever. It should run the Python program in the background completely silently.

Is it possible to put something in the Python script so that it does not open a command prompt and executes silently?

DDS
  • 2,340
  • 16
  • 34
vdvaxel
  • 667
  • 1
  • 14
  • 41

3 Answers3

1

Yes, on windows you can create a bat-file like this:

start /B your_file.exe

add this code to your app:

import ctypes
import os
import win32process

hwnd = ctypes.windll.kernel32.GetConsoleWindow()      

    if hwnd != 0:      
        ctypes.windll.user32.ShowWindow(hwnd, 0)      
        ctypes.windll.kernel32.CloseHandle(hwnd)
        _, pid = win32process.GetWindowThreadProcessId(hwnd)
        os.system('taskkill /PID ' + str(pid) + ' /f')
DDS
  • 2,340
  • 16
  • 34
  • Is it start /B or start /b? I used start /b before but will it do/is it the same as start /B? – vdvaxel Jul 04 '18 at 08:11
  • I created a .bat file with `start /B myfile.exe` and I double clicked the .bat file, but it still opens a command prompt. I want it to be completely in the background. Is this possible? – vdvaxel Jul 04 '18 at 08:13
  • I use this method and works (not opening the cmd console), now you have to tell your python not to show its CLI. – DDS Jul 04 '18 at 08:15
  • How do I do this? – vdvaxel Jul 04 '18 at 08:16
  • Updated answer. From this: https://stackoverflow.com/questions/1689015/run-python-script-without-windows-console-appearing – DDS Jul 04 '18 at 08:19
1

Save this one line of text as file invisible.vbs:

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

To run any program or batch file invisibly, use it like this:

wscript.exe "C:\Wherever\invisible.vbs" "C:\Some Other Place\MyBatchFile.bat"

To also be able to pass-on/relay a list of arguments use only two double quotes

CreateObject("Wscript.Shell").Run "" & WScript.Arguments(0) & "", 0, False

Example: Invisible.vbs "Kill.vbs ME.exe"

Nordle
  • 2,915
  • 3
  • 16
  • 34
  • Thank you for your answer, but I want to use my exe file for this. Is it still possible? – vdvaxel Jul 04 '18 at 08:10
  • Sorry I misread what you needed, try the following: Save this one line of text as file invisible.vbs: `CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False` To run any program or batch file invisibly, use it like this: `wscript.exe "C:\Wherever\invisible.vbs" "C:\Some Other Place\MyBatchFile.bat"` To also be able to pass-on/relay a list of arguments use only two double quotes `CreateObject("Wscript.Shell").Run "" & WScript.Arguments(0) & "", 0, False` Example: Invisible.vbs "Kill.vbs ME.exe" I'll replace this as an answer. – Nordle Jul 04 '18 at 08:16
1

You can use the --noconsole option, if this doesn't work change the spec file setting "console" to False.

m.rp
  • 718
  • 8
  • 22