1

In any python code I write that gets an error, it will show the error but the window will disappear right away and i can't see the error. It makes fixing codes really difficult. can anyone help me? (i have python 2.7 installed and with my programs i type them in notepad and save them as a .py file)

3
  • What IDE are you using? How are you running your program? Commented Apr 29, 2014 at 2:40
  • @yoyo311 Obviously he's simply double-clicking the .py file. Commented Apr 29, 2014 at 2:40
  • You could try using an IDE like pyscripter or spyder, which would also give you the benefit of syntax-highlighting etc Commented Apr 29, 2014 at 2:46

4 Answers 4

3

Wrap your main code body in an exception handler and if an exception occurs, display it and wait for user input:

import traceback

try:
    <main code>
except Exception:
    print traceback.format_exc()
    raw_input("Press return to exit")
Sign up to request clarification or add additional context in comments.

Comments

1

Open a command prompt and cd to the directory where your .py file is and type the name of the file there to run it.

6 Comments

This only works if your computer recognizes .py as python file. If not you have to type python file.py, and if this does not work you first have to set the python.exe location in PATH environment variable (assuming windows).
@yoyo311 Of course. But I'm quite certain his computer recognizes .py files from the description of the problem.
I can run the code fine, it just won't display any errors the code gets. it will show the error for a split second and the window will close.
@mdlp0716 this will fix that. Try it.
@Blorgbeard how exactly do you do this? im not exactly sure what it means to cd to the directory where my .py files are
|
0

Open a command prompt, cd into the directory where the file is, and then do the following:

Redirect the standard error to a file as such:

python myscript.py > errorlog.txt 2>&1 

Test:

myscript.py:

import time

time.sleep(2)

raise ValueError ('This should be redirected')

Running:

$ python myscript.py > errorlog.txt 2>&1
$

errorlog.txt after running:

Traceback (most recent call last):
  File "myscript.py", line 5, in <module>
    raise ValueError ('This should be redirected')
ValueError: This should be redirected

2 Comments

If OP was running his script from the command-line, he wouldn't have a problem anyway. He's double-clicking it in windows, so he can't redirect stderr
Edited, to fit, added an information to the top like @ooga
0

I highly recommend using an IDE instead of notepad. It will make writing and debugging code much easier for you.

Here's a screenshot of pyscripter showing me exactly which line caused my error.

You can see how much easier it is to see what's going on.

debugging python screenshot

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.