3

How can i remove this black command prompt screen from the desktop when my python script is running?

I made the service.py script into exe using python 2 exe. All worked fine but when the .exe is running i have a fixed command prompt which i do not want to show.

enter image description here

service.py:

#!/usr/bin/env python
import os
import ctypes
from subprocess import Popen, PIPE

def Mbox(title, text, style):
  ctypes.windll.user32.MessageBoxA(0, text, title, style)

python_check = os.path.exists("C:/Python27/python.exe")
g_check = os.path.exists("C:/dev_appserver.py")

if python_check & g_check:
  p0 = Popen(['C:/Python27/python.exe', 'C:/dev_appserver.py', '--host', '0.0.0.0', '--port', '8080', 'C:/application'], stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True)
  out, err = p0.communicate("n\n")

else:
  Mbox('Notice', 'Launching service failed please make sure C:\Python27\python.exe and C:/dev_appserver.py', 0)

setup.py:

from distutils.core import setup
import py2exe
setup(console=['service.py'])
  • 1
    I agree with the answer from @Trimax, but i'd like to recommend pyinstaller as it is more simple and has more support. For a simple one file, no console.... pyinstaller service.py --noconsole --onefile –  Jul 17 '14 at 10:16
  • 1
    @dev247 Does pyinstaller supports Python 3.3+ ? – Trimax Jul 17 '14 at 10:53
  • 1
    @Trimax No PyInstaller and py2exe both only support up to Python 2.7. See this [StackOverflow](http://stackoverflow.com/questions/16770267/how-can-i-turn-a-python-3-3-script-into-executable-file-i-found-pyinstaller-and) entry, basically you would have to use cx_freeze for that. –  Jul 17 '14 at 10:59
  • 1
    Additionally you can see the [support listings here](http://docs.python-guide.org/en/latest/shipping/freezing/) –  Jul 17 '14 at 11:01
  • 2
    But, I'm using py2exe 0.9.2 with Python 3.4 now and works fine. In pypi: https://pypi.python.org/pypi/py2exe/0.9.2.0 – Trimax Jul 17 '14 at 11:14
  • Someone needs to update that freezing list as it's no longer correct. py2exe works with Python 3 now and has since at least May, 2014 – Mike Driscoll Jul 18 '14 at 14:48

2 Answers2

4

I just saw this from here:
https://github.com/PySimpleGUI/PySimpleGUI/issues/200#issuecomment-732483328

You can install this library: pip install pywin32
Then, you can add these lines at the top of your project:

import win32gui, win32con

hide = win32gui.GetForegroundWindow()
win32gui.ShowWindow(hide, win32con.SW_HIDE)
TechEdd_YT
  • 95
  • 5
1

In the setup.py file you must write windows instead console.

Like this:

windows=["service.py"],
    options={"py2exe":
       {"optimize": 2,
          "bundle_files": 0,
          "ascii": 0}}
Trimax
  • 2,413
  • 7
  • 35
  • 59
  • When i did this: http://stackoverflow.com/a/24803525/285594 , without changing console to windows like you have suggested. Also worked same like windows. –  Jul 17 '14 at 12:39
  • 1
    It's because services in windows don't need a command console. – Trimax Jul 17 '14 at 12:48