Home > OS >  How to run python code from powershell with no command prompt showing?
How to run python code from powershell with no command prompt showing?

Time:04-10

I would like to be able to run a python script from a .ps1 file without the terminal window showing up to run the script. I would think that there is some extra command I can put at the end to make it run windowless, but I am not sure what it is.

The only code I have in the ps1 file is to execute the python script by linking the path.

I do not want to run the code some way else, it has to be from a ps1 script since I will be adding to it eventually to have more features. I also would rather not have to change the file to a .pyw if possible.

E:/script.py

*Note - I ran the file from a different drive this time, but I want it to be from E:/ like I have in my code.

What I don't want to show up

CodePudding user response:

Start-Process python -ArgumentList "python-script-file-path" -NoNewWindow

Lots of excellent info here.

CodePudding user response:

In a comment you state:

im using run (WIN R) and this command: powershell -w h iex (E:\a.ps1)

  • -w h is short for -WindowStyle Hidden

  • iex is short for Invoke-Expression, which is not needed here - and should generally be avoided: see this answer.

Note: While this does make the console window that the PowerShell session itself runs in invisible, the window becomes invisible only after briefly showing first.

  • To avoid this brief flashing of the console window, powershell.exe must be launched indirectly, namely via a GUI-subsystem application that launches it with a hidden window; the bottom section of this answer discusses options.

  • Also note that supporting this directly in the future is planned, but will likely only be implemented for the CLI of PowerShell (Core) 7 , pwsh.exe - see GitHub issue #3028.


With that out of the way:

  • Console applications (including .ps1 scripts) invoked directly from a hidden PowerShell session run hidden too, because they run directly in the hidden console window.

  • By contrast, any GUI applications or applications of either type invoked via Start-Process run visibly.

    • While you can generally use Start-Process -WindowStyle Hidden to run GUI applications invisibly as well, this, unfortunately, does not seem to work with .pyw files, i.e. pythonw.exe-launched scripts, which still run visibly.[1]

By default, .py files invoked directly execute via py.exe, which is a console application.

Strangely, your screen shot suggests that it is not in your case.

py.exe is normally just a launcher for the true console Python executable (allowing you to switch between v2 and v3 versions of Python with py -2 ... and py -3 ...), python.exe, so as a workaround you can try the following (use a full path to python.exe, if needed):

  • Modify your E:\a.ps1 script to explicitly invoke your .py script with python.exe:
python.exe E:\script.py
  • Then launch your invisible PowerShell session as before, except with improved syntax:
# Short for:
#   powershell.exe -WindowStyle Hidden -File E:\a.ps1
powershell -w h -f E:\a.ps1

If that doesn't help, consider reinstalling Python.


[1] I'm guessing this is because the windows potentially created by the .pyw scripts themselves are unrelated to the (invisible) main window of the pythonw.exe GUI-subsystem executable.

  • Related