Python 模块子进程没有属性“STARTF_USESHOWWINDOW”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4703983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:03:58  来源:igfitidea点击:

Module subprocess has no attribute 'STARTF_USESHOWWINDOW'

pythonpython-3.xsubprocess

提问by Rhys

Hi Stack Overflow users,

嗨堆栈溢出用户,

I've encountered a frustrating problem, can't find the answer to it.

我遇到了一个令人沮丧的问题,找不到答案。

Yesterday I was trying to find a way to HIDE a subprocess.Popen. So for example, if i was opening the cmd. I would like it to be hidden, permanently.

昨天我试图找到一种隐藏 subprocess.Popen 的方法。例如,如果我打开 cmd。我希望它被永久隐藏。

I found this code:

我找到了这个代码:

kwargs = {}
if subprocess.mswindows:
     su = subprocess.STARTUPINFO()
     su.dwFlags |= subprocess.STARTF_USESHOWWINDOW
     su.wShowWindow = subprocess.SW_HIDE
     kwargs['startupinfo'] = su 
subprocess.Popen("cmd.exe", **kwargs)

It worked like a charm!

它就像一个魅力!

But today, for reasons I don't need to get into, I had to reinstall python 3 (32bit)

但是今天,由于我不需要进入的原因,我不得不重新安装python 3(32位)

Now, when I run my program I get this error:

现在,当我运行我的程序时,出现此错误:

Traceback (most recent call last):
  File "C:\Python31\hello.py", line 7, in <module>
    su.dwFlags |= subprocess.STARTF_USESHOWWINDOW
AttributeError: 'module' object has no attribute 'STARTF_USESHOWWINDOW'

I'm using 32bit, python3.1.3 ... just like before.

我正在使用 32 位,python3.1.3 ......就像以前一样。

If you have any clues/alternatives PLEASE post, thanks.

如果您有任何线索/替代方案,请发布,谢谢。

NOTE: I am looking for a SHORT method to hide the app, not like two pages of code please

注意:我正在寻找一种简短的方法来隐藏应用程序,而不是像两页代码那样

采纳答案by Rhys

python 3.1.3 > and 2.7

python 3.1.3 > 和 2.7

import subprocess
import sys

params = dict()
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
params['startupinfo'] = startupinfo

p = subprocess.Popen("cmd.exe", **params)

回答by TryPyPy

I had misread the question, sorry. You might have something shadowing either the subprocessor _subprocessmodule. If it's an install glitch, try removing and reinstalling Python 3.

我误读了这个问题,抱歉。您可能有一些东西遮蔽了subprocess_subprocess模块。如果是安装故障,请尝试删除并重新安装 Python 3。

回答by Lennart Regebro

Either the reinstall went wrong or you created a module called subprocess.py and forgot it. :)

要么重新安装出错,要么您创建了一个名为 subprocess.py 的模块并忘记了它。:)

Try the following:

请尝试以下操作:

import subprocess
print(subprocess.__file__)

That should give you the path to your current Windows installations subprocess module, ie.

这应该为您提供当前 Windows 安装子进程模块的路径,即。

C:\Python31\Lib\subprocess.pyc

If it instead says

如果它改为说

C:\PYthon31\subprocess.py

It's importing a module you created. (You may want to consider notputting your Python files in the Python directory, as in your example above. Having a separate directory for each project is a better idea, and might mean you don't have to install Python so often. ;) )

它正在导入您创建的模块。(您可能要考虑不要将 Python 文件放在 Python 目录中,如上面的示例所示。为每个项目设置一个单独的目录是一个更好的主意,这可能意味着您不必经常安装 Python。;) )

回答by ladivadesigne

You can recreate or check the described problem in your Python installation:

您可以在 Python 安装中重新创建或检查所描述的问题:

import subprocess
subprocess.STARTF_USESHOWWINDOW

If the problem persists you should receive error message ending with line like this:

如果问题仍然存在,您应该收到以这样的行结尾的错误消息:

AttributeError: 'module' object has no attribute 'STARTF_USESHOWWINDOW'

Possible solution of the problem is to import in your code old library by this way:

问题的可能解决方案是通过这种方式导入您的代码旧库:

import subprocess
import _subprocess

And later use it only for these two problematic properties:

稍后仅将其用于这两个有问题的属性:

# do not show window
info = subprocess.STARTUPINFO()
info.dwFlags = _subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = _subprocess.SW_HIDE

That's it. Simple and functional - without any uninstall/install of Python or reverting back to the old builds.

就是这样。简单实用 - 无需卸载/安装 Python 或恢复到旧版本。