Daniele Faugiana
Daniele Faugiana

Reputation: 994

How to execute console EXEs without spawning a new console window?

I have a Python script which is usually called from the Windows PowerShell, with some command line arguments. I want to distribute this script in a .exe format, and I want to keep the same "user interface", based on the console, for its usage.

  1. The user opens the Windows Powershell.
  2. The user calls the myscript.exe program from the shell:

    myscript.exe argument1 argument2 argument3
    
  3. The program executes in the same console and writes its output in the same console.

Actually I have a myscript.exe program, which of course gets the arguments from the PowerShell, but, unfortunately, executes the program in another console which is spawned at the call.

How can I avoid this behaviour and keep everything in the same console?

EDIT: I have followed the tutorial on http://www.py2exe.org/index.cgi/Tutorial.

For my setup.py build file I used:

The compilation works ok, and the program does what it is supposed to do. The only undesirable thing is the opening of a dedicated console instead of executing in the same console.

EDIT2: Here is exactly my setup.py code.

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True, 'optimize': 2}},
    console = [{'script': "myscript.py"}],
    zipfile = None,
)

I simply call the script typing:

python setup.py 

in Windows Powershell

EDIT3: This has been done on Windows 8.1 with:

The final executable has also been tested on a Windows 10 system without any Python installation; it worked but showed the same console-spawning behaviour.

Upvotes: 1

Views: 595

Answers (2)

Daniele Faugiana
Daniele Faugiana

Reputation: 994

It seems that I have found an answer by myself with the help of this answer, linked by this answer. The issue was also connected to this other question. A common cause generated two different but related problems.

It is caused by the .exe file name. Switching to a different file name, stops the UAC to ask for admin privileges and executes the software in the same shell.

The name was:

<project_name_under_NDA>_update.exe

But switching to

try.exe

It worked.

Upvotes: 2

Martin Evans
Martin Evans

Reputation: 46779

Hopefully the following might help. This works fine for me both in a standard Windows Command Prompt, and whilst using the Windows PowerShell:

test.py

import sys

print "Arguments passed:"

for arg in sys.argv:
    print '  {}'.format(arg)

setup.py

from distutils.core import setup
import py2exe

setup(
    zipfile = None,
    options = {"py2exe": {"bundle_files": 1}},
    console = [r'test.py'])

It is created using:

python setup.py py2exe

Here is an example execution:

PS E:\test> .\test.exe hello world
Arguments passed:
  E:\test\test.exe
  hello
  world

If this still produces a separate shell, are there any environment variables of PowerShell settings that could be altering your result?

I have tested this in Python 2.7.9 |Anaconda 2.2.0 (32-bit) and py2exe 0.6.9.

Upvotes: 0

Related Questions