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

回眸只為那壹抹淺笑 提交于 2019-12-11 12:22:12

问题


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 console keyword (providing myscript.py as the only element in the list)
  • The zipfile keyword (Set to True)
  • The options keyword with optimize: 2, bundle_files: 1, and compressed: True.

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:

  • Python 3.4.2
  • py2exe 0.9.2.2
  • Powershell 4.0

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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/32681189/how-to-execute-console-exes-without-spawning-a-new-console-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!