7

I am trying to call an executable at startup, which will call another executable itself. For the first part, I simply added the path to the executable to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, which works, my executable is called at startup.

The latter contains, among others, these lines :

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
proc = subprocess.Popen(command, startupinfo=startupinfo)

However, a command shell (cmd) pops when the computer starts. Everything works fine, but it is visible instead of hidden... So basically, how do I hide this command shell ?

When using ProcessExplorer, I have the following hierarchy :

+ System
|_ Interrupts
|_ smss.exe
    |_ some processes...
+ explorer.exe
    |_ some processes...
|_ MYSCRIPT.EXE

Here is the sequence I try to achieve :

  • I create an ISO file containing all the Python executable I want to run on the VM. One of them (master.exe) calls the others.
  • I create a VM which automatically mount the latter
  • The VM, which was prepared, has a scheduled task which calls D:\master.exe
  • master.exe (among other tasks which are not our concern here) adds the value D:\myscript.exe to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
  • The VM reboots
  • D:\myscript.exe is run (and it works fine and as attended), but it does run in a command prompt, which I would like to be invisible.
  • 1
    As a general rule, to hide a process window, I run it via vbscript(.run method). But looks like, python offers you a native way. :-) – anishsane Jan 21 '13 at 14:17
  • related: [How do I hide the console when I use os.system() or subprocess.call()?](http://stackoverflow.com/q/7006238/4279) – jfs May 22 '14 at 00:47

5 Answers5

5

This is a typical problem Python-programmers encounter - and therefore, a solution is offered by Python itself. It has been asked on SO many times, e.g., here, but for you, the problem is a little more complicated.

It's all about whether you use python.exe or pythonw.exe to run your script. For the first one, a console is opened, for the second it's not.

As you use compiled scripts, you have to tell "the compiler" which version you want to use. Assuming you are using py2exe, you can have a look at this post on SO. Here it is explained in detail how to proceed.

Community
  • 1
  • 1
Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56
  • This is only valid for the console window involved with the python-process(that shows you debug information etc...) It isn't related to opening a console-related subprocess with Popen. Tested many times already. The only thing you can do is work with the SHELL=True flag with the disadvantage you won't get any control over exceptions/errors coming from the subprocess. You won't get any information at all. For example if the subprocess isn't loaded due to the system can't find the exe on disk the system will not tell you. Not very handy. – Jomme Nov 30 '16 at 23:38
1

The console window that is being opened probably belongs to the Python process running your script. Show us the entry in the registry running your script.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
  • 2
    @NNzz What's the value of `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run` running your first executable? Please describe exactly what types of executables you have; are they Python scripts or compiled Python scripts or some combination? – Piotr Dobrogost Jan 21 '13 at 11:23
  • They are Python scripts that I compiled into an executable. The value of the key is `D:\myscript.exe`, which is the actual location of the executable I want to run. I will add some information to my post to be more precise. –  Jan 21 '13 at 12:00
0

If you run a console program, Windows will create a console window. "python.exe" is a console program.

If you don't want a console window, you can run your Python script with "pythonw.exe" rather than "python.exe".

user9876
  • 10,954
  • 6
  • 44
  • 66
0

You can compile it to exe format. When I encounter that problem, I used py2exe to compile python file to invisible executible.

All you have to do, is to change setup.py file(used to compile), from

setup(console=['__main__.py'], options={"py2exe":{"includes":["sip"]}})

to

setup(windows=['__main__.py'], options={"py2exe":{"includes":["sip"]}})
Stanislav
  • 27,441
  • 9
  • 87
  • 82
Jiwon
  • 1,074
  • 1
  • 11
  • 27
0

I had the same issue and I used Pyinstaller.

Pyinstaller is a smart cross platform tool to compile .py file into standalone executable.

Install it via:

pip install pyinstaller (more information here)

Use the following command to hide the console (to make your script a process):

pyinstaller yourfilename.py -F --windowed

(with "-F" flag you'll get a single .exe file and with "--windowed" flag the console will be hidden)

Federico Rubbi
  • 714
  • 3
  • 16