7

Im trying to start a wxPython app that (I have converted to an exe with py2exe) from a process that is running in the background.

The problem is that when the gui app opens, so does a console window (c:\windows\system\cmd.exe)

I had a look at this question where Alex Martelli suggests setting the creationflags paramater of Popen to 0x08000000 but this hasn't solved my problem.

Also I wonder if there is a better way of running a process in the background, at the moment I just changed the extension of the script to pyw and since it doesn't have a GUI then it isn't visible...

This is line that calls the subprocess

    subprocess.Popen(args="%s"%comPort,bufsize=0,
                     executable="myFrozen_WxpythonApp.exe",
                     creationflags=0x08000000, shell=False)

py2exe script

...

options = {'py2exe': {'compressed': 3,
                          'optimize': 2,
                          'excludes': excludes,
                          'packages': packages,
                          'dll_excludes': dll_excludes,
                          'bundle_files': 1,
                          'dist_dir': 'dist',
                          'xref': False,
                          'skip_archive': False,
                          'ascii': False,
                          #'packages': packages,
                        'custom_boot_script': '',
                         }
                }  

      setup(options=options, windows=["app.pyw"], zipfile=None, data_files=data_files)

Update:

As I explained in my answer to this question the problem was in the subprocess.Popen call.

The first string in the args parameter should be the name of the executable, the executable name can then be followed by any commands or data that needs to be passed to the subprocess.

Community
  • 1
  • 1
volting
  • 16,773
  • 7
  • 36
  • 54
  • Please put a newline or two in your code block. – robert Dec 04 '10 at 23:18
  • That's unrelated to subprocess and the console window of the main script. It's `WxpythonApp`'s console window popping up (and it should still pop up when you started it alone). –  Dec 04 '10 at 23:21
  • @delnan: I wish it were that simple. The windows `parameter` was set when converting to an exe with `py2exe` and the console window does not open if I manually execute the app, i.e. double click it.. – volting Dec 04 '10 at 23:24
  • Ya... as much as it is annoying! – volting Dec 04 '10 at 23:35
  • Could you show us the relevant parts of the py2exe script? – Ryan Ginstrom Dec 05 '10 at 00:04
  • The `windows=["scrupt.py"]` argument to `setup()` is the documented way to suppress the console window according to the [author](http://www.velocityreviews.com/forums/showpost.php?p=1768275&postcount=2). – martineau Dec 05 '10 at 02:59
  • @martineau: I very much doubt my problem is caused by how I converted to an exe, and any way as you can see I have used the `windows` paramater... Also as I mentioned in a previous comment, if I manually execute the exe then no console window opens, the console window only opens when the app is called with `subprocess.Popen` – volting Dec 05 '10 at 12:00
  • @volting: Yes, since you seemed to be doing the `setup()` correctly and the .exe worked properly standalone, I strongly suspected the problem had something to do with the `subprocess.Popen()` call. Unfortunately you don't show what was in `data` which made it difficult for anyone else to determine that was the issue was. – martineau Dec 05 '10 at 16:59
  • @martineau: I only realized that I wasn't showing what was is in `data` after I realized it was important. Typical... :) – volting Dec 05 '10 at 17:10
  • You can try to use windows' `start` utility, it very often helps to avoid the `cmd` window. – ulidtko Dec 05 '10 at 12:53
  • FWIW you also need the `creationflags=0x08000000` to prevent the window pop up, if that wasn't clear. – congusbongus Feb 12 '13 at 04:56
  • @congusbongus does that creation flag have a macro definition for it? – kjgregory Jan 21 '15 at 02:25
  • @kjgregory `CREATE_NO_WINDOW`, according to the linked answer. – congusbongus Jan 21 '15 at 03:18

1 Answers1

3

I figured out what I was doing wrong after reading the documentation for subprocess.Popen

The first string in the args parameter should be the name of the executable. I didn't include the name of the executable as I thought that was taken care of with the executable paramater.

volting
  • 16,773
  • 7
  • 36
  • 54
  • Ah, very good. Sounds like you've answered your own question. I suggest you update your question with the solution you found. BTW, the docs **are** most definitely misleading regarding the `args` parameter when they say it "... can be explicitly set by using the executable argument" which seems to imply it can be left out of the `args`. – martineau Dec 05 '10 at 16:49
  • So what can the executable para do? when i set the executable name in **args para**, and the executable path in executable para. It didn't work. – Samuel Jul 30 '12 at 02:22
  • @Samuel: I think the way it works is that `Popen()` will use the first argument in `args` as the program to execute unless you also provide the optional `executable` argument in which case that value will override it. In either case the contents of entire `args` parameter is passed to program that actually gets run as its command-line whether or not the first argument of it is what was used to start it or not. – martineau Nov 08 '12 at 20:52