如何执行控制台EXE,而不产生新的控制台窗口? [英] How to execute console EXEs without spawning a new console window?
问题描述
我有一个Python脚本,通常从Windows PowerShell调用,有一些命令行参数。我想以.exe格式分发此脚本,并且我想根据控制台保持相同的用户界面,以供其使用。
- 用户打开Windows Powershell。
-
用户从shell调用myscript.exe程序:
myscript.exe argument1 argument2 argument3
-
程序在同一个控制台中执行,并将其输出写入同一个控制台。
实际上,我有一个myscript.exe程序,它当然从PowerShell获取参数,但不幸的是,在调用产生的另一个控制台中执行程序。
如何避免此行为并将所有内容保留在同一个控制台中?
编辑:
我已遵循 http://www.py2exe.org/index.cgi/Tutorial上的教程a>。
对于我的setup.py构建文件,我使用了:
-
console
关键字(提供myscript.py作为列表中唯一的元素) -
zipfile
关键字(设为True) -
选项关键字与
optimize:2
,bundle_files:1
和compressed:True
。
编译工作正常,程序执行它应该做的。唯一不希望的事情是打开专用控制台,而不是在同一控制台中执行。
EDIT2:这正是我的setup.py代码。
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,
)
键入:
python setup.py
在Windows Powershell
EDIT3:
这在Windows 8.1上已经完成: / p>
- Python 3.4.2
- py2exe 0.9.2.2
- Powershell 4.0
最后的可执行文件也在没有任何Python安装的Windows 10系统上测试过,它工作,但显示相同的控制台生成行为。
似乎我已经找到一个答案, 此答案,由< a href =http://stackoverflow.com/a/32682345/3789324>此答案。此问题也已关联到此其他问题。一个常见原因产生了两个不同但相关的问题。
它是由.exe文件名引起的。切换到其他文件名,停止UAC请求管理员权限并在同一个shell中执行软件。
名称是:
< project_name_under_NDA> _update.exe
但切换到
try.exe
它有效。
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.
- The user opens the Windows Powershell.
The user calls the myscript.exe program from the shell:
myscript.exe argument1 argument2 argument3
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 withoptimize: 2
,bundle_files: 1
, andcompressed: 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.
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.
这篇关于如何执行控制台EXE,而不产生新的控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!