Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
815 views
in Technique[技术] by (71.8m points)

python - Module subprocess has no attribute 'STARTF_USESHOWWINDOW'

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.

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)

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

Traceback (most recent call last):
  File "C:Python31hello.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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...