I created a python file where an event loop is running.
During this event loop, pressing certain hotkeys causes interaction with another app.
If I open the file and run it through PyCharm, it works.
If I run the file in a terminal or as a .py file, the code exits prematurely for some reason so I have to run it with the python -i command or add input() at the end of the code so that the event loop keeps running for the hotkeys to work. Is there a reason why that happens?
Now I would like to autostart this file every time Windows runs. In theory it should be easy:
Change it to .pyw and run it with pythonw.
However, if I do that and click on the file to execute, it doesn't work. No idea what is happening in the background.
If I run the file in a terminal like this: python main.pyw
the script is executed in the terminal, I can see it and the hotkeys work.
Why is .pyw not working for me here? What other alternative do I have to run the file in the background so that I can always use the hotkeys whenever I boot Windows?
I have a little python script that takes a single filename as a command line argument and writes a converted file. Not rocket science, it's 10s of lines long.
I can run that on windows from a cmd prompt simply by typing the name of the script. So for example:
C:\> CD \wheremyscriptis
C:\wheremyscriptis> myscript.py
and it runs fine. Without any arguments it spits out a little Usage message. Quite conventional.
Now we're using Powershell more and more and the first thing I notice in powershell is it won't run at all without an explicit directory, so in the above example I'd need to type .\myscript.py.
That's odd but you could get used to it, not a crisis.
But what happens now is the script runs in another window, which flashes up and disappears before youc an read the usage message.
Given the behavior is inconsistent across these contexts, what can we do inside the script to make the powershell context more usable. Is there a way to detect if we're running in some window that powershell threw up (which is itself weird) and then if so, pause before exiting to give a user a chance to read the usage message before the window disappears?
Long story short: pythonw.exe does nothing, python.exe accepts nothing (which one should I use?)
test.py:
print "a"
CMD window:
C:\path>pythonw.exe test.py
<BLANK LINE>
C:\path>
C:\path>python.exe test.py
File "C:\path\test.py", line 7
print "a"
^
SyntaxError: invalid syntax
C:\path>
Please tell me what I'm doing terrible wrong.
To summarize and complement the existing answers:
python.exe is a console (terminal) application for launching CLI-type scripts (console applications).
Unless run from an existing console window, python.exe opens a new console window.
Standard streams sys.stdin, sys.stdout and sys.stderr are connected to the console window.
Execution is synchronous when launched from a cmd.exe or PowerShell console window: See eryksun's 1st comment below.
If a new console window was created, it stays open until the script terminates.
When invoked from an existing console window, the prompt is blocked until the script terminates.
pythonw.exe is a GUI app for launching GUI/no-UI-at-all scripts.
NO console window is opened.
Execution is asynchronous:
When invoked from a console window, the script is merely launched and the prompt returns right away, whether the script is still running or not.
Standard streams sys.stdin, sys.stdout and sys.stderr are NOT available.
Caution: Unless you take extra steps, this has potentially unexpected side effects:
Unhandled exceptions cause the script to abort silently.
In Python 2.x, simply trying to use print() can cause that to happen (in 3.x, print() simply has no effect).
To prevent that from within your script, and to learn more, see this answer of mine.
Ad-hoc, you can use output redirection:Thanks, #handle.
pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt
(from PowerShell:
cmd /c pythonw.exe yourScript.pyw 1>stdout.txt 2>stderr.txt) to capture stdout and stderr output in files.
If you're confident that use of print() is the only reason your script fails silently with pythonw.exe, and you're not interested in stdout output, use #handle's command from the comments:
pythonw.exe yourScript.pyw 1>NUL 2>&1
Caveat: This output redirection technique does not work when invoking *.pyw scripts directly (as opposed to by passing the script file path to pythonw.exe). See eryksun's 2nd comment and its follow-ups below.
You can control which of the executables runs your script by default - such as when opened from Explorer - by choosing the right filename extension:
*.py files are by default associated (invoked) with python.exe
*.pyw files are by default associated (invoked) with pythonw.exe
If you don't want a terminal window to pop up when you run your program, use pythonw.exe;
Otherwise, use python.exe
Regarding the syntax error: print is now a function in 3.x
So use instead:
print("a")
See here: http://docs.python.org/using/windows.html
pythonw.exe "This suppresses the terminal window on startup."
If you're going to call a python script from some other process (say, from the command line), use pythonw.exe. Otherwise, your user will continuously see a cmd window launching the python process. It'll still run your script just the same, but it won't intrude on the user experience.
An example might be sending an email; python.exe will pop up a CLI window, send the email, then close the window. It'll appear as a quick flash, and can be considered somewhat annoying. pythonw.exe avoids this, but still sends the email.
I was struggling to get this to work for a while. Once you change the extension to .pyw, make sure that you open properties of the file and direct the "open with" path to pythonw.exe.
In my experience the pythonw.exe is faster at least with using pygame.
I wrote this bat file to run a program i wrote in python
#echo off
python C:\Project\myProg\main.py
pause
but nothing happens, when i click it twice just open closes nothing runs.
If I open a cmd and type just python C:\Project\myProg\main.py it works and runs.
I tried to Run as Administrator, but I get the same result.
Any ideas?
So I am trying to debug some Python code that I am running, because for some reason when I run the code in a command line it works perfectly, but when I make it a .py file and run it that way it fails.
The output is in a Windows Commander window, and it disappears before I can read it.
Is there a way to see what this output is?
Open a windows commander, and then go to the dir where a.py is, run "python a.py", it will not disappear!