Windows console prompt doesn't show up after an unhandled exception - python

Feeling really stupid right now. I opened a python file in my Windows console and the file raised an error (such as TypeError, AttributeError, etc.) and the console won't work anymore so I have to close it and open a new window everytime I get an error. There should be a shortcut or something to exit but Ctrl+C doesn't work. I have Windows 10 and Python 3.6.
When I run my file in the console happens this:
C:\Users\...path...>python my_file.py
Traceback (most recent call last):
File "C:\Users\...path...\my_file.py", line 74,
...stuff...
AttributeError: my error
And after this I can't do anything. If someone could help.

On an unhandled exception, a Python program normally quits, and you get the console prompt back. If yours doesn't, it means that it hangs instead.
If Ctrl-C doesn't work, you can force-kill a Windows console program with Ctrl-Break.
But you really should find out why it hangs, as it's not normal. My guess is you're swallowing all exceptions somewhere, e.g. with an unqualified except: which is strongly discouraged exactly for this reason.

May be because of bug/error your program become freeze. Please try to use exception handling.
An example :
try:
your_codes()
except Exception as ex:
print(ex)
This is just an example of exception handling. You can do it in much better (but complicated!) approach.
You can read this doc.

Related

PyCares - Fatal Python error: PyImport_GetModuleDict: no module dictionary

I've been trying to get a script to run on a Windows server. It runs fine on my Mac, but keeps throwing an error at the end of the script. The output from the script is as expected and completes entirely, then this error is thrown:
Fatal Python error: PyImport_GetModuleDict: no module dictionary
Current thread 0x00001880 (most recent call first):
File "C:\Python37\lib\site-packages\pycares\__init__.py", line 387 in (lambda)
Currently I'm running Python 3.6.5 and getting this error but I've tried it up to Python 3.7.5 and get the same error. This seems like a Windows specific issue with Pycares since the same script runs fine on my Mac machine. Since the script executes correctly, even on windows, this seems like it is an error thrown in the cleanup of the script.
Any ideas on a cause or a workaround?
Edit: I've found this Python bug https://bugs.python.org/issue26153 that seems to say it's due to a race condition and warnings in the del function, but I still get the issue even when running Python 3.7

get stacktrace of a python program for debugging

In order to get the stacktrace of a python program, I am trying to follow this example. In the article, the author invokes the gdb as follows. However, the python version of my environment is python 3.4.4. When I type
python3.4-dbg testmyplotlib2.py &
The error message is python3.4-dbg: command not found. What's the right way to get stacktrace by using gdb.
What OS are you on? It looks like you need to install python3.4-dbg. If you are on Linux, you will need to enter:
sudo apt-get install python3.4-dbg
GDB is excellent program for debugging, but if printing traceback is the only reason you are installing GDB, do not do it, that is waay overkill. You can just import traceback and
use traceback.format_stack() to get an array of calls that lead to the location in program
use traceback.print_stack() to print it to command line
use print traceback.format_exc() to print what lead to the current exceptions (works in except clasuse)

Exception in thread pydevd.CommandThread (most likely raised during interpreter shutdown) - PyDev

I've made a single-threaded Python program in PyDev, and after the program executes and reports that it's done, I get the following error message:
Exception in thread pydevd.CommandThread (most likely raised during interpreter shutdown):
It doesn't affect how my program runs, but it would be nice to have it not show up. I've run the program on the command line, and it doesn't produce any error messages like this.
Any suggestions?
Apparently it was the way I was writing to a file using the built-in json library.
I don't know why or if it was wrong, but I stopped the error by using
file.write(json.dumps(dict, indent=4))
instead of
json.dump(dict, file, indent=4)
I'm not sure why this was producing the error, but changing this made it stop. If anyone can let me know why, that would be great.

(Python, IDLE, Windows) Pressing Stack Viewer exits all IDLE windows

I am running Python 3.1.2 with IDLE 3.1.2 on Windows 7. When I try to use the Stack Viewer, blue text and a new window briefly appear before all open IDLE windows exit (I don't have time to read the text or new window). This is the first time I have used Stack Viewer.
Is this normal behavior? How can I get the Stack Viewer to stay open?
Thanks for your help,
Alex
This IDLE bug (3 series only) was fixed 30Jan11. The fix is in 3.1.4 and 3.2.
From the documentation, the stack viewer shows the stack traceback of the last exception. So maybe in your case, you are trying to open the stack viewer, without any exception haven taken place?
>>> a
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
a
NameError: name 'a' is not defined
If you type 'a' and press ENTER, an exception occurs. Now try opening the stack viewer?
I don't have Windows so I can't help you with hands-on experience, but I would do the following:
see what is the exact command that runs when you click on the menu item for Idle (I think you can right-click and see its properties, or at least you could on earlier versions of Windows)
open a command prompt, and run the command that opens Idle
open the stack viewer and then note the traceback that (should!) appear in the command prompt window
report said traceback here :)
BTW, in my installation (neither Windows nor Python 3.x), an attempt to open the stack viewer without a traceback produces the following message box:
Title: No stack trace
Message: There is no stack trace yet.
(sys.last_traceback is not defined)

NetBeans and Python

When I run some python code in NetBeans, which raises an error, the output in NetBeans just gives an error message and no further information, such as line number. Is there any way to fix that?
If you can, I would run your script outside of NetBeans either with the built-in editor (IDLE) or just run it from the command line. That should give you a traceback with the error and lineno
NetBeans has issues with debugging, as other posts suggest.
added solution is debugging if don't have any compiling error

Categories