If I run the python script file from IDLE or Windows command prompt, I am able to view the error message.
Script file:
print(3/0)
input()
Error:
Traceback (most recent call last):
File "...\TEST.py", line 1, in <module>
print(3/0)
ZeroDivisionError: division by zero
But if I run the file by double clicking on it, the window just closes and I do not know what the error is. How can I see it?
I am running Python 3.4.
If you just double-click the file, once you hit an error, it terminates the program, thus closing the console window. To see the errors, you should run it from the command prompt.
This is how its supposed to work.
When you "doubleclick" the .py file windows sees an executable file and invokes a cmd-shell and runs your python executable within (even though its only a text file, its considered as an executable - thats how you have set it in your system - this can be changed, say to make a doubleclick simply open the file in a text editor of your choice, like Notepad or Notepad++ or Python's default IDLE editor). Since division by 0 is an error, the cmd-shell is killed as soon as the error is seen in the .py file - your .py file is treated as an executable, much like how an .exe application crash doesn't wait for you before its killed.
If you do not want lose the window and wish to see the error, then you already seem to know what to do - run it by invoking python from a cmd-shell manually, or better still, use the built-in IDLE editor (Press F5 to run your script from an IDLE editor)
Related
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'm trying to run an executable file called CDQ.exe (which is supposed to create a text file) from within a python script. I'm using os.system(CDQ.exe) to run it. When I start the python script, the program freezes upon the os.system() call. I also don't get the text file. When I opened the task manager, I found CDQ was still running, and it kept running until I ended it.
When I try to run the CDQ.exe from the windows file browser by clicking it twice, the program appears for a couple of seconds in the task manager, then it closes, and my text file appears.
The executable file was created from c++ qt, and I'm using windows.
Does anyone know what's going on?
So I mistakenly overwrote a .py file. I had run the original version of the file in the Spyder console yesterday and to my surprise, the console runs the original script when I give it the command to run the file with the file name even though the .py file does not have the code anymore. Is there any way I can get the console to show me the code that it's running? Or any other way to recover the file?
Thanks
I created a script in python and I need it to run in way that the python code is not visible. I tried different methods like implementing nodeJS and PHP, but to no avail. So, I thought of using a .bat file to run the python file. I created a bat file for the python script, but on clicking the batch file all I got was an error saying that a module was missing in the code. The code works perfectly when I run it in Spyder and on the Anaconda Prompt. So, I thought of running the file from a batch file via the Anaconda Prompt. The code in the batch file is below:
%windir%\System32\cmd.exe /K ""C:\Program Files\Anaconda3\Scripts\activate.bat" "C:\Program Files\Anaconda3""
python myscript.py
pause
The .bat file now opens a command prompt in the python environment, but does not run the python file. I tried altering the code, but it was always the same result. If I type:
python myscript.py
into the next line in the command prompt, the python file gets executed. I need to find a way for the file to be executed without typing anything in the prompt. The python file contains GUI from tkinter and the shebang line has the python version (3.7.4). I work with Windows 10.
I tried this code in the batch file as well,
"C:\Program Files\Anaconda3\python.exe" "C:\Users\Sathya\Desktop\myscript.py"
pause
All this did was open and close the command prompt in the blink of an eye. I tried to run the bat file a few more times, but got the same result. But, this code worked on a different file (also with tkinter GUI) myscript_test.py and this one gave the GUI window correctly. I don't have a clue as to why it worked on the second one and not the first one.
Is it possible to make a program in Python that, when run, does not actually open any window (including command prompt)?
For example, opening the program would appear to do nothing, but in reality, the program is running in the background somewhere.
Thanks!
Are you running the python program by double clicking *.py file in Windows?
Then, rename the *.py file to *.pyw.
Run it with pythonw.exe instead of python.exe.