3

I'm making an application that uses Python's socket library to communicate data across an LAN. The code that is responsible for this has its own executable file. But when it's called a command windows is shown on the desktop.

How can I run this code without having a shell popping up ?

Details: The program only handles the networking; there is no reason for the shell popping up (I never use print(), etc. in the code). Is there a way to stop the shell from being displayed which I am unaware of? The networking program needs to remain separate from the rest of the program so that, if necessary, the user can call it themselves.

The program was converted from python to an windows executable through the cx_Freeze cx_Freeze, in case that matters)

4
  • @eryksun could you please post as an answer and include a short description to help future viewers and avoid link decay? If you don't feel like it, I always could, but I think you deserve credit for your answer. May 29, 2014 at 0:25
  • @eryksun I take it back. I don't know if I could figure out how to explain this. Help! May 29, 2014 at 0:33
  • @eryksun I don't want an in-depth explanation, just a (brief) one of how and why it works and how to use it. I program as a hobby, and am by no means an expert. May 29, 2014 at 1:16
  • @eryksun Update: I've figured out how to use subprocess (for some reason, I was thinking it was part of os, which is what was giving me trouble, but all I get is TypeError: 'float' object is not iterable. May 29, 2014 at 1:28

2 Answers 2

1

The answer submitted by @Kyllopardium may be exactly what you need. There's no reason to create a console application if you never intend to use the console interface.

That said, given that the networking program also needs to run as a standalone application, you may need to build a separate executable for a console interface. If you prefer to have a single console program, you can instead use the subprocess module to hide the console window when launched from the main application.

Configure an instance of STARTUPINFO to hide the application window and pass this to the Popen constructor. You can also pass this parameter to the subprocess convenience functions, call, check_call, and check_output. For example:

import subprocess

exe = r'\path\to\networking_program.exe'
args = [exe, 'param1', '...', 'paramN']

if hasattr(subprocess, 'STARTUPINFO'):
# Windows
    si = subprocess.STARTUPINFO()
    si.dwFlags |= subprocess.STARTF_USESHOWWINDOW   
    # The following is the initialized default, but
    # setting it explicitly is self-documenting.
    si.wShowWindow = subprocess.SW_HIDE 
else: 
# POSIX
    si = None  

try:    
    subprocess.check_call(args, startupinfo=si)
    # check_call waits for the process to exit.
    # If the return code is non-zero it raises the 
    # following exception:
except subprocess.CalledProcessError as e:
    handle_error(e)

Note that the program is run directly instead of via the cmd.exe shell. So the window that gets hidden is the program's own console window, not a "cmd shell" window. This window is hosted in a separate process (e.g. conhost.exe in Windows 7+). Multiple applications can share the same console, such as when a parent process (e.g. python.exe) starts a child process and waits for it to exit. If the parent process doesn't have a console (e.g. pythonw.exe), then Windows creates a new console for the child.

3
  • Ah, I was passing each argument separately instead of in a table. This is a beautifully made program and an excellent explanation. As the solution itself was @Kyllopardiun 's I feel that I must accept his answer, but +1 for sure. EDIT: Oops, or not. I don't have the rep. May 29, 2014 at 19:36
  • subprocess tries but can't really paper over all of the many differences between POSIX and Windows. I used args as a list because that's what POSIX requires, not Windows. When given a list on Windows, it uses subprocess.list2cmdline to convert the list into a command-line string. This isn't just a simple join of the items since it needs to add quoting and escaping according to the rules used by Microsoft's C runtime.
    – Eryk Sun
    May 30, 2014 at 0:32
  • Ah, I should have mentioned that the program is Windows-specific. Still, nice to know how to make it cross-platform. May 30, 2014 at 16:12
0
from cx_Freeze import main
sys.argv = [sys.argv[0], '--target-dir', '%s'%DISTFOLDER, '--base', 'Win32GUI', entrypoint ]
main()

You’ll need to put in your target directory for DISTFOLDER and your entry point script for entrypoint.

The console-less part is as simple the –base Win32GUI tag.

or:

import sys
from cx_Freeze import setup, Executable

base = None
if (sys.platform == "win32"):
    base = "Win32GUI"    # Tells the build script to hide the console.

They do pretty much the same thing.

3
  • Then possibly 2 executables need to be created -- both GUI and console mode -- since it also needs to run as a standalone application.
    – Eryk Sun
    May 28, 2014 at 22:46
  • Personally, I would go with 2. And if the GUI one receives let's say the --console in its arguments, then make a subprocess to call the console mode, or the opposite. I do think it's a reasonable choice.
    – Mansueli
    May 28, 2014 at 22:54
  • After your snippet, I would assume I would add the setup() function? May 29, 2014 at 0:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.