11

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:\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.

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

Rhys
  • 4,926
  • 14
  • 41
  • 64
  • Are you sure you are running the same code? – user225312 Jan 16 '11 at 06:35
  • same code yes, i have tried to un/reinstall many times ... it would be helpful if someone could try run the above code on py3 so that I can confirm that this code should be working and I have a serious problem – Rhys Jan 16 '11 at 10:33
  • Hi, I have the same problem with Python 2.7. The problem appeared after upgrade from 2.6 to 2.7. I load the right module (C:\Python27\Lib\subprocess.pyc). Do you have any idea what might be wrong? –  Jan 23 '11 at 18:06

4 Answers4

11

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.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
4

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

Try the following:

import subprocess
print(subprocess.__file__)

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

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 not putting 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. ;) )

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • mm well subprocess module IS working ... a simple subprocess.Popen works perfectly fine. The code you suggested: ..... print(subprocess.__file__) ..... returns: C:\Python31\lib\subprocess.py – Rhys Jan 16 '11 at 08:33
  • @Rhys: Then I'd suggest you delete C:\Python31 and do it again. – Lennart Regebro Jan 16 '11 at 08:35
  • ok I'll try put it in a different directory or something. I've tried to reinstall a number of times – Rhys Jan 16 '11 at 08:39
  • @Rhys: Did you delete C:\Python31 completely first? – Lennart Regebro Jan 16 '11 at 08:41
  • i uninstalled if from control panel and removed the leftover folder. By the sound of the responses i'm getting the code should defiantly be working so at least I've narrowed it down to a technical error – Rhys Jan 16 '11 at 09:38
  • Thank you, for you response Lennart. Its seems I have solved the problem myself. The Python 3.1.2 executes the subprocess.STARTF_USESHOWWINDOW perfectly. Python 3.1.3 fails, responding with an AttributeError – Rhys Jan 17 '11 at 09:49
  • @Rhys: Aha. Turns out you are supposed to import it from win32process. http://www.gossamer-threads.com/lists/python/python/848138 http://svn.python.org/view/python/tags/r27/Lib/subprocess.py?r1=79064&r2=82504 – Lennart Regebro Jan 17 '11 at 13:38
1

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

TryPyPy
  • 6,214
  • 5
  • 35
  • 63
  • 1
    mm well subprocess module IS working ... a simple subprocess.Popen works perfectly fine. I've tried to reinstall a number of times, tried a reg clean too – Rhys Jan 16 '11 at 08:36
  • 1
    Thank you, for you response TryPyPy. Its seems I have solved the problem myself. The Python 3.1.2 executes the subprocess.STARTF_USESHOWWINDOW perfectly. Python 3.1.3 fails, responding with an AttributeError – Rhys Jan 17 '11 at 09:47
1

python 3.1.3 > and 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)
Rhys
  • 4,926
  • 14
  • 41
  • 64